Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. public class PathUtils {
  2. /**
  3. * To get URI from Path
  4. *
  5. * @param context context
  6. * @param file file
  7. * @return Uri
  8. */
  9. public static Uri getUriFromPath(Context context, File file) {
  10. String filePath = file.getAbsolutePath();
  11. Cursor cursor = context.getContentResolver().query(
  12. MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
  13. new String[]{MediaStore.Images.Media._ID},
  14. MediaStore.Images.Media.DATA + "=? ",
  15. new String[]{filePath}, null);
  16. if (cursor != null && cursor.moveToFirst()) {
  17. int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
  18. cursor.close();
  19. return Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + id);
  20. } else {
  21. if (file.exists()) {
  22. ContentValues values = new ContentValues();
  23. values.put(MediaStore.Images.Media.DATA, filePath);
  24. return context.getContentResolver().insert(
  25. MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
  26. } else {
  27. return null;
  28. }
  29. }
  30. }
  31.  
  32. /**
  33. * Get Path from URI
  34. *
  35. * @param context context
  36. * @param uri uri
  37. * @return String
  38. */
  39. public static String getPathFromUri(final Context context, final Uri uri) {
  40.  
  41. final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
  42.  
  43. // DocumentProvider
  44. if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
  45. // ExternalStorageProvider
  46. if (isExternalStorageDocument(uri)) {
  47. final String docId = DocumentsContract.getDocumentId(uri);
  48. final String[] split = docId.split(":");
  49. final String type = split[0];
  50.  
  51. if ("primary".equalsIgnoreCase(type)) {
  52. return Environment.getExternalStorageDirectory() + "/" + split[1];
  53. }
  54.  
  55. // TODO handle non-primary volumes
  56. }
  57. // DownloadsProvider
  58. else if (isDownloadsDocument(uri)) {
  59.  
  60. final String id = DocumentsContract.getDocumentId(uri);
  61. final Uri contentUri = ContentUris.withAppendedId(
  62. Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
  63.  
  64. return getDataColumn(context, contentUri, null, null);
  65. }
  66. // MediaProvider
  67. else if (isMediaDocument(uri)) {
  68. final String docId = DocumentsContract.getDocumentId(uri);
  69. final String[] split = docId.split(":");
  70. final String type = split[0];
  71.  
  72. Uri contentUri = null;
  73. if ("image".equals(type)) {
  74. contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  75. } else if ("video".equals(type)) {
  76. contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
  77. } else if ("audio".equals(type)) {
  78. contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  79. }
  80.  
  81. final String selection = "_id=?";
  82. final String[] selectionArgs = new String[]{
  83. split[1]
  84. };
  85.  
  86. return getDataColumn(context, contentUri, selection, selectionArgs);
  87. }
  88. }
  89. // MediaStore (and general)
  90. else if ("content".equalsIgnoreCase(uri.getScheme())) {
  91.  
  92. // Return the remote address
  93. if (isGooglePhotosUri(uri))
  94. return uri.getLastPathSegment();
  95.  
  96. return getDataColumn(context, uri, null, null);
  97. }
  98. // File
  99. else if ("file".equalsIgnoreCase(uri.getScheme())) {
  100. return uri.getPath();
  101. }
  102.  
  103. return null;
  104. }
  105.  
  106. private static String getDataColumn(Context context, Uri uri, String selection,
  107. String[] selectionArgs) {
  108.  
  109. final String column = "_data";
  110. final String[] projection = {
  111. column
  112. };
  113.  
  114. try (Cursor cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
  115. null)) {
  116. if (cursor != null && cursor.moveToFirst()) {
  117. final int index = cursor.getColumnIndexOrThrow(column);
  118. return cursor.getString(index);
  119. }
  120. }
  121. return null;
  122. }
  123.  
  124.  
  125. /**
  126. * @param uri The Uri to check.
  127. * @return Whether the Uri authority is ExternalStorageProvider.
  128. */
  129. private static boolean isExternalStorageDocument(Uri uri) {
  130. return "com.android.externalstorage.documents".equals(uri.getAuthority());
  131. }
  132.  
  133. /**
  134. * @param uri The Uri to check.
  135. * @return Whether the Uri authority is DownloadsProvider.
  136. */
  137. private static boolean isDownloadsDocument(Uri uri) {
  138. return "com.android.providers.downloads.documents".equals(uri.getAuthority());
  139. }
  140.  
  141. /**
  142. * @param uri The Uri to check.
  143. * @return Whether the Uri authority is MediaProvider.
  144. */
  145. private static boolean isMediaDocument(Uri uri) {
  146. return "com.android.providers.media.documents".equals(uri.getAuthority());
  147. }
  148.  
  149. /**
  150. * @param uri The Uri to check.
  151. * @return Whether the Uri authority is Google Photos.
  152. */
  153. private static boolean isGooglePhotosUri(Uri uri) {
  154. return "com.google.android.apps.photos.content".equals(uri.getAuthority());
  155. }
  156.  
  157. public static String getFileName(String url) {
  158. return url.substring(url.lastIndexOf('/') + 1);
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement