From c8b9acbc40881d595eb1534cb496220d49433807 Mon Sep 17 00:00:00 2001 From: Galeen <2math@mail.bg> Date: Wed, 3 Jul 2019 15:39:42 +0300 Subject: [PATCH] fix on get file path from uri --- .../base_library/utils/files/FileUtils.java | 169 ++++++++++++++++-- 1 file changed, 158 insertions(+), 11 deletions(-) diff --git a/core_library/src/main/java/com/futurist_labs/android/base_library/utils/files/FileUtils.java b/core_library/src/main/java/com/futurist_labs/android/base_library/utils/files/FileUtils.java index 436c625..818c9fe 100644 --- a/core_library/src/main/java/com/futurist_labs/android/base_library/utils/files/FileUtils.java +++ b/core_library/src/main/java/com/futurist_labs/android/base_library/utils/files/FileUtils.java @@ -32,12 +32,19 @@ import android.os.Environment; import android.provider.DocumentsContract; import android.provider.MediaStore; +import android.provider.OpenableColumns; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.text.TextUtils; import android.util.Log; import android.webkit.MimeTypeMap; +import java.io.BufferedOutputStream; import java.io.File; import java.io.FileFilter; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; import java.text.DecimalFormat; import java.util.Comparator; @@ -272,7 +279,6 @@ public static String getDataColumn(Context context, Uri uri, String selection, * * @param context The context. * @param uri The Uri to query. - * @see #isLocal(String) * @see #getFile(Context, Uri) * @author paulburke */ @@ -319,18 +325,42 @@ else if (isDownloadsDocument(uri)) { if (id.startsWith("raw:")) { return id.replaceFirst("raw:", ""); } - try { - final Uri contentUri = ContentUris.withAppendedId( - Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); - return getDataColumn(context, contentUri, null, null); - } catch (NumberFormatException e) { - return null; + String[] contentUriPrefixesToTry = new String[]{ + "content://downloads/public_downloads", + "content://downloads/my_downloads", + "content://downloads/all_downloads" + }; + + for (String contentUriPrefix : contentUriPrefixesToTry) { + Uri contentUri = ContentUris.withAppendedId(Uri.parse(contentUriPrefix), Long.valueOf(id)); + try { + String path = getDataColumn(context, contentUri, null, null); + if (path != null) { + return path; + } + } catch (Exception e) { + if(DEBUG){ + e.printStackTrace(); + } + } } + String path = getDataColumn(context, uri, null, null); + if (path != null) { + return path; + } + + // path could not be retrieved using ContentResolver, therefore copy file to accessible cache using streams + String fileName = getFileName(context, uri); + File cacheDir = getDocumentCacheDir(context); + File file = generateFileName(fileName, cacheDir); + String destinationPath = null; + if (file != null) { + destinationPath = file.getAbsolutePath(); + saveFileFromUri(context, uri, destinationPath); + } + + return destinationPath; } -// final Uri contentUri = ContentUris.withAppendedId( -// Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); -// -// return getDataColumn(context, contentUri, null, null); } // MediaProvider else if (isMediaDocument(uri)) { @@ -572,4 +602,121 @@ public static Intent createGetImageIntent() { intent.setType("image/*"); return intent; } + + private static void saveFileFromUri(Context context, Uri uri, String destinationPath) { + InputStream is = null; + BufferedOutputStream bos = null; + try { + is = context.getContentResolver().openInputStream(uri); + bos = new BufferedOutputStream(new FileOutputStream(destinationPath, false)); + byte[] buf = new byte[1024]; + is.read(buf); + do { + bos.write(buf); + } while (is.read(buf) != -1); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (is != null) is.close(); + if (bos != null) bos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + public static String getFileName(@NonNull Context context, Uri uri) { + String mimeType = context.getContentResolver().getType(uri); + String filename = null; + + if (mimeType == null && context != null) { + String path = getPath(context, uri); + if (path == null) { + filename = getName(uri.toString()); + } else { + File file = new File(path); + filename = file.getName(); + } + } else { + Cursor returnCursor = context.getContentResolver().query(uri, null, + null, null, null); + if (returnCursor != null) { + int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); + returnCursor.moveToFirst(); + filename = returnCursor.getString(nameIndex); + returnCursor.close(); + } + } + + return filename; + } + + public static String getName(String filename) { + if (filename == null) { + return null; + } + int index = filename.lastIndexOf('/'); + return filename.substring(index + 1); + } + + public static File getDocumentCacheDir(@NonNull Context context) { + File dir = new File(context.getCacheDir(), "documents"); + if (!dir.exists()) { + dir.mkdirs(); + } + logDir(context.getCacheDir()); + logDir(dir); + + return dir; + } + + private static void logDir(File dir) { + if(!DEBUG) return; + Log.d(TAG, "Dir=" + dir); + File[] files = dir.listFiles(); + for (File file : files) { + Log.d(TAG, "File=" + file.getPath()); + } + } + + @Nullable + public static File generateFileName(@Nullable String name, File directory) { + if (name == null) { + return null; + } + + File file = new File(directory, name); + + if (file.exists()) { + String fileName = name; + String extension = ""; + int dotIndex = name.lastIndexOf('.'); + if (dotIndex > 0) { + fileName = name.substring(0, dotIndex); + extension = name.substring(dotIndex); + } + + int index = 0; + + while (file.exists()) { + index++; + name = fileName + '(' + index + ')' + extension; + file = new File(directory, name); + } + } + + try { + if (!file.createNewFile()) { + return null; + } + } catch (IOException e) { + Log.w(TAG, e); + return null; + } + + logDir(directory); + + return file; + } } \ No newline at end of file