From c97b708b0f5ca399b6c0ee88f0ff7c9491df3bab Mon Sep 17 00:00:00 2001 From: misaochan Date: Fri, 25 May 2018 22:46:50 +1000 Subject: [PATCH] Transfer createCopyPath to FileUtils as a static method --- .../fr/free/nrw/commons/upload/FileUtils.java | 18 ++++++++++++++++++ .../free/nrw/commons/upload/ShareActivity.java | 18 +++++++----------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/upload/FileUtils.java b/app/src/main/java/fr/free/nrw/commons/upload/FileUtils.java index 612b86458..b5be2b664 100644 --- a/app/src/main/java/fr/free/nrw/commons/upload/FileUtils.java +++ b/app/src/main/java/fr/free/nrw/commons/upload/FileUtils.java @@ -26,6 +26,24 @@ import java.util.Date; import timber.log.Timber; public class FileUtils { + /** + * In older devices getPath() may fail depending on the source URI. Creating and using a copy of the file seems to work instead. + * @return path of copy + */ + @Nullable + static String createCopyPath(ParcelFileDescriptor descriptor) { + try { + String copyPath = Environment.getExternalStorageDirectory().toString() + "/CommonsApp/" + new Date().getTime() + ".jpg"; + File newFile = new File(Environment.getExternalStorageDirectory().toString() + "/CommonsApp"); + newFile.mkdir(); + FileUtils.copy(descriptor.getFileDescriptor(), copyPath); + Timber.d("Filepath (copied): %s", copyPath); + return copyPath; + } catch (IOException e) { + Timber.e(e); + return null; + } + } /** * Get a file path from a Uri. This will get the the path for Storage Access diff --git a/app/src/main/java/fr/free/nrw/commons/upload/ShareActivity.java b/app/src/main/java/fr/free/nrw/commons/upload/ShareActivity.java index dd6631fa2..3f6f1392d 100644 --- a/app/src/main/java/fr/free/nrw/commons/upload/ShareActivity.java +++ b/app/src/main/java/fr/free/nrw/commons/upload/ShareActivity.java @@ -492,31 +492,27 @@ public class ShareActivity return snackbar; } + /** + * Gets file path from media URI. + * In older devices getPath() may fail depending on the source URI, creating and using a copy of the file seems to work instead. + * @return file path of media + */ @Nullable private String getPathOfMediaOrCopy() { String filePath = FileUtils.getPath(getApplicationContext(), mediaUri); Timber.d("Filepath: " + filePath); if (filePath == null) { - // in older devices getPath() may fail depending on the source URI - // creating and using a copy of the file seems to work instead. - // TODO: there might be a more proper solution than this String copyPath = null; try { ParcelFileDescriptor descriptor = getContentResolver().openFileDescriptor(mediaUri, "r"); if (descriptor != null) { boolean useExtStorage = prefs.getBoolean("useExternalStorage", true); if (useExtStorage) { - copyPath = Environment.getExternalStorageDirectory().toString() + "/CommonsApp/" + new Date().getTime() + ".jpg"; - File newFile = new File(Environment.getExternalStorageDirectory().toString() + "/CommonsApp"); - newFile.mkdir(); - FileUtils.copy(descriptor.getFileDescriptor(), copyPath); - Timber.d("Filepath (copied): %s", copyPath); + copyPath = FileUtils.createCopyPath(descriptor); return copyPath; } copyPath = getApplicationContext().getCacheDir().getAbsolutePath() + "/" + new Date().getTime() + ".jpg"; - FileUtils.copy( - descriptor.getFileDescriptor(), - copyPath); + FileUtils.copy(descriptor.getFileDescriptor(), copyPath); Timber.d("Filepath (copied): %s", copyPath); return copyPath; }