No Description

PathResolver.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package com.RNFetchBlob.Utils;
  2. import android.annotation.TargetApi;
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.net.Uri;
  6. import android.os.Build;
  7. import android.provider.DocumentsContract;
  8. import android.provider.MediaStore;
  9. import android.content.ContentUris;
  10. import android.os.Environment;
  11. import android.content.ContentResolver;
  12. import com.RNFetchBlob.RNFetchBlobUtils;
  13. import java.io.File;
  14. import java.io.InputStream;
  15. import java.io.FileOutputStream;
  16. public class PathResolver {
  17. @TargetApi(19)
  18. public static String getRealPathFromURI(final Context context, final Uri uri) {
  19. final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
  20. // DocumentProvider
  21. if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
  22. // ExternalStorageProvider
  23. if (isExternalStorageDocument(uri)) {
  24. final String docId = DocumentsContract.getDocumentId(uri);
  25. final String[] split = docId.split(":");
  26. final String type = split[0];
  27. if ("primary".equalsIgnoreCase(type)) {
  28. return Environment.getExternalStorageDirectory() + "/" + split[1];
  29. }
  30. // TODO handle non-primary volumes
  31. }
  32. // DownloadsProvider
  33. else if (isDownloadsDocument(uri)) {
  34. try {
  35. final String id = DocumentsContract.getDocumentId(uri);
  36. //Starting with Android O, this "id" is not necessarily a long (row number),
  37. //but might also be a "raw:/some/file/path" URL
  38. if (id != null && id.startsWith("raw:/")) {
  39. Uri rawuri = Uri.parse(id);
  40. String path = rawuri.getPath();
  41. return path;
  42. }
  43. final Uri contentUri = ContentUris.withAppendedId(
  44. Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
  45. return getDataColumn(context, contentUri, null, null);
  46. }
  47. catch (Exception ex) {
  48. //something went wrong, but android should still be able to handle the original uri by returning null here (see readFile(...))
  49. return null;
  50. }
  51. }
  52. // MediaProvider
  53. else if (isMediaDocument(uri)) {
  54. final String docId = DocumentsContract.getDocumentId(uri);
  55. final String[] split = docId.split(":");
  56. final String type = split[0];
  57. Uri contentUri = null;
  58. if ("image".equals(type)) {
  59. contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  60. } else if ("video".equals(type)) {
  61. contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
  62. } else if ("audio".equals(type)) {
  63. contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  64. }
  65. final String selection = "_id=?";
  66. final String[] selectionArgs = new String[] {
  67. split[1]
  68. };
  69. return getDataColumn(context, contentUri, selection, selectionArgs);
  70. }
  71. else if ("content".equalsIgnoreCase(uri.getScheme())) {
  72. // Return the remote address
  73. if (isGooglePhotosUri(uri))
  74. return uri.getLastPathSegment();
  75. return getDataColumn(context, uri, null, null);
  76. }
  77. // Other Providers
  78. else{
  79. try {
  80. InputStream attachment = context.getContentResolver().openInputStream(uri);
  81. if (attachment != null) {
  82. String filename = getContentName(context.getContentResolver(), uri);
  83. if (filename != null) {
  84. File file = new File(context.getCacheDir(), filename);
  85. FileOutputStream tmp = new FileOutputStream(file);
  86. byte[] buffer = new byte[1024];
  87. while (attachment.read(buffer) > 0) {
  88. tmp.write(buffer);
  89. }
  90. tmp.close();
  91. attachment.close();
  92. return file.getAbsolutePath();
  93. }
  94. }
  95. } catch (Exception e) {
  96. RNFetchBlobUtils.emitWarningEvent(e.toString());
  97. return null;
  98. }
  99. }
  100. }
  101. // MediaStore (and general)
  102. else if ("content".equalsIgnoreCase(uri.getScheme())) {
  103. // Return the remote address
  104. if (isGooglePhotosUri(uri))
  105. return uri.getLastPathSegment();
  106. return getDataColumn(context, uri, null, null);
  107. }
  108. // File
  109. else if ("file".equalsIgnoreCase(uri.getScheme())) {
  110. return uri.getPath();
  111. }
  112. return null;
  113. }
  114. private static String getContentName(ContentResolver resolver, Uri uri) {
  115. Cursor cursor = resolver.query(uri, null, null, null, null);
  116. cursor.moveToFirst();
  117. int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
  118. if (nameIndex >= 0) {
  119. String name = cursor.getString(nameIndex);
  120. cursor.close();
  121. return name;
  122. }
  123. return null;
  124. }
  125. /**
  126. * Get the value of the data column for this Uri. This is useful for
  127. * MediaStore Uris, and other file-based ContentProviders.
  128. *
  129. * @param context The context.
  130. * @param uri The Uri to query.
  131. * @param selection (Optional) Filter used in the query.
  132. * @param selectionArgs (Optional) Selection arguments used in the query.
  133. * @return The value of the _data column, which is typically a file path.
  134. */
  135. public static String getDataColumn(Context context, Uri uri, String selection,
  136. String[] selectionArgs) {
  137. Cursor cursor = null;
  138. String result = null;
  139. final String column = "_data";
  140. final String[] projection = {
  141. column
  142. };
  143. try {
  144. cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
  145. null);
  146. if (cursor != null && cursor.moveToFirst()) {
  147. final int index = cursor.getColumnIndexOrThrow(column);
  148. result = cursor.getString(index);
  149. }
  150. }
  151. catch (Exception ex) {
  152. ex.printStackTrace();
  153. return null;
  154. }
  155. finally {
  156. if (cursor != null)
  157. cursor.close();
  158. }
  159. return result;
  160. }
  161. /**
  162. * @param uri The Uri to check.
  163. * @return Whether the Uri authority is ExternalStorageProvider.
  164. */
  165. public static boolean isExternalStorageDocument(Uri uri) {
  166. return "com.android.externalstorage.documents".equals(uri.getAuthority());
  167. }
  168. /**
  169. * @param uri The Uri to check.
  170. * @return Whether the Uri authority is DownloadsProvider.
  171. */
  172. public static boolean isDownloadsDocument(Uri uri) {
  173. return "com.android.providers.downloads.documents".equals(uri.getAuthority());
  174. }
  175. /**
  176. * @param uri The Uri to check.
  177. * @return Whether the Uri authority is MediaProvider.
  178. */
  179. public static boolean isMediaDocument(Uri uri) {
  180. return "com.android.providers.media.documents".equals(uri.getAuthority());
  181. }
  182. /**
  183. * @param uri The Uri to check.
  184. * @return Whether the Uri authority is Google Photos.
  185. */
  186. public static boolean isGooglePhotosUri(Uri uri) {
  187. return "com.google.android.apps.photos.content".equals(uri.getAuthority());
  188. }
  189. }