Skip to content

Commit

Permalink
Protect against crashes in URI to file mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
bpellin committed Mar 18, 2016
1 parent 93c8623 commit 8657267
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions app/src/main/java/com/keepassdroid/utils/UriUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,28 +90,36 @@ public static Uri translate(Context ctx, Uri uri) {
if (EmptyUtils.isNullOrEmpty(scheme)) { return uri; }

String filepath = null;
// Use content resolver to try and find the file
if (scheme.equalsIgnoreCase("content")) {
Cursor cursor = ctx.getContentResolver().query(uri, new String[] {android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
cursor.moveToFirst();

filepath = cursor.getString(0);
cursor.close();
try {
// Use content resolver to try and find the file
if (scheme.equalsIgnoreCase("content")) {
Cursor cursor = ctx.getContentResolver().query(uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
cursor.moveToFirst();

if (cursor != null) {
filepath = cursor.getString(0);
cursor.close();


if (!isValidFilePath(filepath)) {
filepath = null;
if (!isValidFilePath(filepath)) {
filepath = null;
}
}
}
}

// Try using the URI path as a straight file
if (EmptyUtils.isNullOrEmpty(filepath)) {
filepath = uri.getEncodedPath();
if (!isValidFilePath(filepath)) {
filepath = null;
// Try using the URI path as a straight file
if (EmptyUtils.isNullOrEmpty(filepath)) {
filepath = uri.getEncodedPath();
if (!isValidFilePath(filepath)) {
filepath = null;
}
}
}
// Fall back to URI if this fails.
catch (Exception e) {
filepath = null;
}

// Update the file to a file URI
if (!EmptyUtils.isNullOrEmpty(filepath)) {
Expand Down

0 comments on commit 8657267

Please sign in to comment.