Skip to content

Commit

Permalink
Update file utils
Browse files Browse the repository at this point in the history
  • Loading branch information
billthefarmer committed Jan 25, 2018
1 parent 36c3a52 commit 9dff10a
Showing 1 changed file with 39 additions and 28 deletions.
67 changes: 39 additions & 28 deletions src/main/java/org/billthefarmer/editor/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,35 @@ public static String fileProviderPath(Uri uri)
return path.toString();
}

/**
* @param uri The Uri to match.
* @return The file path from the Uri.
* @author billthefarmer
*/
public static String guessExternalPath(Uri uri)
{
List<String> list = uri.getPathSegments();
List<String> segments =
list.subList(1, list.size());

StringBuilder path = new StringBuilder();
path.append(Environment.getExternalStorageDirectory());
for (String segment: segments)
{
path.append(File.separator);
path.append(segment);
}

if (BuildConfig.DEBUG)
Log.d(TAG, "Path " + path.toString());

File file = new File(path.toString());
if (file.exists())
return path.toString();

return null;
}

/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
Expand Down Expand Up @@ -301,16 +330,13 @@ public static String getDataColumn(Context context, Uri uri,
if (BuildConfig.DEBUG)
DatabaseUtils.dumpCursor(cursor);

final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
final int column_index = cursor.getColumnIndex(column);
if (column_index >= 0)
return cursor.getString(column_index);
}
}

catch (Exception e)
{
if (BuildConfig.DEBUG)
Log.e(TAG, "getDataColumn", e);
}
catch (Exception e) {}

finally
{
Expand Down Expand Up @@ -427,34 +453,19 @@ else if ("audio".equals(type))
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme()))
{
if (BuildConfig.DEBUG)
{
Cursor cursor = null;
try
{
cursor = context.getContentResolver()
.query(uri, null, null, null, null);
if (cursor != null && cursor.moveToFirst())
DatabaseUtils.dumpCursor(cursor);
}

catch (Exception e) {}

finally
{
if (cursor != null)
cursor.close();
}
}

// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();

// Return FileProvider path
else if (isFileProvider(uri))
if (isFileProvider(uri))
return fileProviderPath(uri);

// Guess external path
String path = guessExternalPath(uri);
if (path != null)
return path;

return getDataColumn(context, uri, null, null);
}
// File
Expand Down

0 comments on commit 9dff10a

Please sign in to comment.