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 31, 2018
1 parent 9dff10a commit 1e82033
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 54 deletions.
5 changes: 5 additions & 0 deletions src/main/java/org/billthefarmer/editor/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,11 @@ public void onClick(DialogInterface dialog, int id)
EditText text =
(EditText) ((Dialog) dialog).findViewById(TEXT);
String name = text.getText().toString();

// Ignore empty string
if (name.isEmpty())
return;

file = new File(name);

// Check absolute file
Expand Down
91 changes: 37 additions & 54 deletions src/main/java/org/billthefarmer/editor/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,71 +227,58 @@ public static boolean isGooglePhotosUri(Uri uri)
.equals(uri.getAuthority());
}

/**
* @param uri The Uri to check.
* @return Whether the Uri is a FileProvider file.
* @author billthefarmer
*/
public static boolean isFileProvider(Uri uri)
{
if (BuildConfig.DEBUG)
Log.d(TAG, "Path " + uri.getPath());

List<String> segments = uri.getPathSegments();
return segments.contains("storage") &&
segments.contains("emulated") &&
segments.contains("0");
}

/**
* @param uri The Uri to match.
* @return The file path from the Uri.
* @return The file path from the FileProvider Uri.
* @author billthefarmer
*/
public static String fileProviderPath(Uri uri)
{
List<String> list = uri.getPathSegments();
List<String> segments =
list.subList(list.indexOf("storage"), list.size());
if (BuildConfig.DEBUG)
Log.d(TAG, "Path " + uri.getPath());

StringBuilder path = new StringBuilder();
for (String segment: segments)
List<String> list = uri.getPathSegments();
if (list.contains("storage") &&
list.contains("emulated") &&
list.contains("0"))
{
path.append(File.separator);
path.append(segment);
}
List<String> segments =
list.subList(list.indexOf("storage"), list.size());

if (BuildConfig.DEBUG)
Log.d(TAG, "Path " + path.toString());
for (String segment: segments)
{
path.append(File.separator);
path.append(segment);
}

return path.toString();
}
if (BuildConfig.DEBUG)
Log.d(TAG, "Path " + 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());
File file = new File(path.toString());
if (file.isFile())
return path.toString();
}

StringBuilder path = new StringBuilder();
path.append(Environment.getExternalStorageDirectory());
for (String segment: segments)
if (list.size() > 1)
{
path.append(File.separator);
path.append(segment);
}
List<String> segments =
list.subList(1, list.size());

if (BuildConfig.DEBUG)
Log.d(TAG, "Path " + path.toString());
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();
File file = new File(path.toString());
if (file.isFile())
return path.toString();
}

return null;
}
Expand Down Expand Up @@ -458,11 +445,7 @@ else if ("content".equalsIgnoreCase(uri.getScheme()))
return uri.getLastPathSegment();

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

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

Expand Down

0 comments on commit 1e82033

Please sign in to comment.