Skip to content

Commit 7c19b57

Browse files
jddeepvyankatesh24
authored andcommitted
#679 - Improved realpath util method (#697)
1 parent 4b5f621 commit 7c19b57

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

app/src/main/java/swati4star/createpdf/util/RealPathUtil.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public static String getRealPath(Context context, Uri fileUri) {
3737

3838
public static String getRealPathFromURI_API19(final Context context, final Uri uri) {
3939
// DocumentProvider
40+
if (isDriveFile(uri)) {
41+
return null;
42+
}
4043
if (DocumentsContract.isDocumentUri(context, uri)) {
4144
// ExternalStorageProvider
4245
if (isExternalStorageDocument(uri)) {
@@ -47,6 +50,9 @@ public static String getRealPathFromURI_API19(final Context context, final Uri u
4750
if ("primary".equalsIgnoreCase(type)) {
4851
return Environment.getExternalStorageDirectory() + "/" + split[1];
4952
}
53+
if ("home".equalsIgnoreCase(type)) {
54+
return Environment.getExternalStorageDirectory() + "/" + split[1];
55+
}
5056

5157
} else if (isDownloadsDocument(uri)) {
5258

@@ -67,6 +73,10 @@ public static String getRealPathFromURI_API19(final Context context, final Uri u
6773
}
6874
}
6975
}
76+
} else {
77+
StringBuilder path = StringUtils.trimExternal(uri.getPath().substring(1));
78+
path.insert(0, Environment.getExternalStorageDirectory() + "/");
79+
return path.toString();
7080
}
7181
return null;
7282
}
@@ -113,6 +123,20 @@ public static boolean isExternalStorageDocument(Uri uri) {
113123
return "com.android.externalstorage.documents".equals(uri.getAuthority());
114124
}
115125

126+
/**
127+
* This function is used to check for a drive file URI.
128+
*
129+
* @param uri
130+
* @return
131+
*/
132+
public static boolean isDriveFile(Uri uri) {
133+
if ("com.google.android.apps.docs.storage".equals(uri.getAuthority()))
134+
return true;
135+
if ("com.google.android.apps.docs.storage.legacy".equals(uri.getAuthority()))
136+
return true;
137+
return false;
138+
}
139+
116140
/**
117141
* @param uri The Uri to check.
118142
* @return Whether the Uri authority is DownloadsProvider.

app/src/main/java/swati4star/createpdf/util/StringUtils.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,18 @@ public static String getDefaultStorageLocation() {
6060
return Environment.getExternalStorageDirectory().getAbsolutePath() +
6161
pdfDirectory;
6262
}
63+
64+
/**
65+
* This function is used to trim the root path name from
66+
* the getPath() method so that it can be replaced by
67+
* the External Storage Directory.
68+
* @param path
69+
* @return
70+
*/
71+
public static StringBuilder trimExternal(String path) {
72+
StringBuilder trimmedPath = new StringBuilder();
73+
int tempPath = path.indexOf('/');
74+
trimmedPath.append(path.substring(tempPath + 1));
75+
return trimmedPath;
76+
}
6377
}

0 commit comments

Comments
 (0)