Bladeren bron

Android Feature: Handle Arbitrary Providers (#374)

* Update ISSUE_TEMPLATE

* Handle Android content providers
Jon San Miguel 7 jaren geleden
bovenliggende
commit
f345afa27e
2 gewijzigde bestanden met toevoegingen van 41 en 0 verwijderingen
  1. 1
    0
      .github/ISSUE_TEMPLATE
  2. 40
    0
      android/src/main/java/com/RNFetchBlob/Utils/PathResolver.java

+ 1
- 0
.github/ISSUE_TEMPLATE Bestand weergeven

@@ -4,3 +4,4 @@ You may want to take a look on that page or find issues tagged "trouble shooting
4 4
 * please provide the version of installed library and RN project.
5 5
 * a sample code snippet/repository is very helpful to spotting the problem.
6 6
 * issues which have been tagged as 'needs feedback', will be closed after 2 weeks if receive no feedbacks.
7
+* issues lack of detailed information will be closed without any feedback

+ 40
- 0
android/src/main/java/com/RNFetchBlob/Utils/PathResolver.java Bestand weergeven

@@ -8,6 +8,11 @@ import android.provider.DocumentsContract;
8 8
 import android.provider.MediaStore;
9 9
 import android.content.ContentUris;
10 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;
11 16
 
12 17
 public class PathResolver {
13 18
     public static String getRealPathFromURI(final Context context, final Uri uri) {
@@ -59,6 +64,29 @@ public class PathResolver {
59 64
 
60 65
                 return getDataColumn(context, contentUri, selection, selectionArgs);
61 66
             }
67
+            // Other Providers
68
+            else {
69
+                try {
70
+                    InputStream attachment = context.getContentResolver().openInputStream(uri);
71
+                    if (attachment != null) {
72
+                        String filename = getContentName(context.getContentResolver(), uri);
73
+                        if (filename != null) {
74
+                            File file = new File(context.getCacheDir(), filename);
75
+                            FileOutputStream tmp = new FileOutputStream(file);
76
+                            byte[] buffer = new byte[1024];
77
+                            while (attachment.read(buffer) > 0) {
78
+                                tmp.write(buffer);
79
+                            }
80
+                            tmp.close();
81
+                            attachment.close();
82
+                            return file.getAbsolutePath();
83
+                        }
84
+                    }
85
+                } catch (Exception e) {
86
+                    RNFetchBlobUtils.emitWarningEvent(e.toString());
87
+                    return null;
88
+                }
89
+            }
62 90
         }
63 91
         // MediaStore (and general)
64 92
         else if ("content".equalsIgnoreCase(uri.getScheme())) {
@@ -77,6 +105,18 @@ public class PathResolver {
77 105
         return null;
78 106
     }
79 107
 
108
+    private static String getContentName(ContentResolver resolver, Uri uri) {
109
+        Cursor cursor = resolver.query(uri, null, null, null, null);
110
+        cursor.moveToFirst();
111
+        int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
112
+        if (nameIndex >= 0) {
113
+            String name = cursor.getString(nameIndex);
114
+            cursor.close();
115
+            return name;
116
+        }
117
+        return null;
118
+    }
119
+
80 120
     /**
81 121
      * Get the value of the data column for this Uri. This is useful for
82 122
      * MediaStore Uris, and other file-based ContentProviders.