Skip to content

Commit

Permalink
#679 - Improved realpath util method (#697)
Browse files Browse the repository at this point in the history
  • Loading branch information
jddeep authored and vyankatesh24 committed Apr 14, 2019
1 parent 4b5f621 commit 7c19b57
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
24 changes: 24 additions & 0 deletions app/src/main/java/swati4star/createpdf/util/RealPathUtil.java
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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;
}
}

0 comments on commit 7c19b57

Please sign in to comment.