Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #679 updated realpath util method #697

Merged
merged 7 commits into from Apr 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions app/src/main/java/swati4star/createpdf/util/RealPathUtil.java
Expand Up @@ -37,6 +37,9 @@ public static String getRealPath(Context context, Uri fileUri) {

public static String getRealPathFromURI_API19(final Context context, final Uri uri) {
// DocumentProvider
if (isDriveFile(uri)) {
return null;
}
if (DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
Expand All @@ -47,6 +50,9 @@ public static String getRealPathFromURI_API19(final Context context, final Uri u
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
if ("home".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}

} else if (isDownloadsDocument(uri)) {

Expand All @@ -67,6 +73,10 @@ public static String getRealPathFromURI_API19(final Context context, final Uri u
}
}
}
} else {
StringBuilder path = StringUtils.trimExternal(uri.getPath().substring(1));
path.insert(0, Environment.getExternalStorageDirectory() + "/");
return path.toString();
}
return null;
}
Expand Down Expand Up @@ -113,6 +123,20 @@ public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}

/**
* This function is used to check for a drive file URI.
*
* @param uri
* @return
*/
public static boolean isDriveFile(Uri uri) {
if ("com.google.android.apps.docs.storage".equals(uri.getAuthority()))
return true;
if ("com.google.android.apps.docs.storage.legacy".equals(uri.getAuthority()))
return true;
return false;
}

/**
* @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/swati4star/createpdf/util/StringUtils.java
Expand Up @@ -60,4 +60,18 @@ public static String getDefaultStorageLocation() {
return Environment.getExternalStorageDirectory().getAbsolutePath() +
pdfDirectory;
}

/**
* This function is used to trim the root path name from
* the getPath() method so that it can be replaced by
* the External Storage Directory.
* @param path
* @return
*/
public static StringBuilder trimExternal(String path) {
StringBuilder trimmedPath = new StringBuilder();
int tempPath = path.indexOf('/');
trimmedPath.append(path.substring(tempPath + 1));
return trimmedPath;
}
}