Skip to content

Commit 62617a6

Browse files
committed
cache files when files comes from contents provder
1 parent a54dc4f commit 62617a6

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

android/src/main/java/io/github/elyx0/reactnativedocumentpicker/DocumentPickerModule.java

+50-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@
2525
import com.facebook.react.bridge.WritableArray;
2626
import com.facebook.react.bridge.WritableMap;
2727

28+
import java.io.BufferedReader;
29+
import java.io.File;
30+
import java.io.FileInputStream;
31+
import java.io.FileNotFoundException;
32+
import java.io.FileOutputStream;
33+
import java.io.IOException;
34+
import java.io.InputStream;
35+
import java.io.InputStreamReader;
36+
import java.io.OutputStream;
37+
import java.util.logging.Logger;
38+
2839
/**
2940
* @see <a href="https://developer.android.com/guide/topics/providers/document-provider.html">android documentation</a>
3041
*/
@@ -174,22 +185,41 @@ public void onShowActivityResult(int resultCode, Intent data, Promise promise) {
174185
}
175186
}
176187

177-
private WritableMap getMetadata(Uri uri) {
188+
private WritableMap getMetadata(Uri uri) throws FileNotFoundException {
178189
WritableMap map = Arguments.createMap();
179190

180-
map.putString(FIELD_URI, uri.toString());
181191

182192
ContentResolver contentResolver = getReactApplicationContext().getContentResolver();
183193

184194
map.putString(FIELD_TYPE, contentResolver.getType(uri));
185195

186196
Cursor cursor = contentResolver.query(uri, null, null, null, null, null);
187-
188197
try {
189198
if (cursor != null && cursor.moveToFirst()) {
199+
String fileName = "";
190200
int displayNameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
191201
if (!cursor.isNull(displayNameIndex)) {
192-
map.putString(FIELD_NAME, cursor.getString(displayNameIndex));
202+
fileName = cursor.getString(displayNameIndex);
203+
map.putString(FIELD_NAME, fileName);
204+
}
205+
206+
if (uri != null && "content".equals(uri.getScheme())) {
207+
try {
208+
InputStream input = DocumentPickerModule.getInputStreamForVirtualFile(contentResolver,uri,contentResolver.getType(uri));
209+
File file = new File(getReactApplicationContext().getCacheDir(),fileName);
210+
OutputStream output = new FileOutputStream(file);
211+
byte[] buffer = new byte[4 * 1024]; // or other buffer size
212+
int read;
213+
while ((read = input.read(buffer)) != -1) {
214+
output.write(buffer, 0, read);
215+
}
216+
output.flush();
217+
map.putString(FIELD_URI, file.getPath());
218+
} catch (IOException e) {
219+
throw new FileNotFoundException();
220+
}
221+
} else {
222+
map.putString(FIELD_URI, uri.toString());
193223
}
194224

195225
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
@@ -212,4 +242,20 @@ private WritableMap getMetadata(Uri uri) {
212242

213243
return map;
214244
}
245+
246+
private static InputStream getInputStreamForVirtualFile(ContentResolver resolver, Uri uri, String mimeTypeFilter)
247+
throws IOException {
248+
249+
String[] openableMimeTypes = resolver.getStreamTypes(uri, mimeTypeFilter);
250+
251+
if (openableMimeTypes == null ||
252+
openableMimeTypes.length < 1) {
253+
throw new FileNotFoundException();
254+
}
255+
256+
return resolver
257+
.openTypedAssetFileDescriptor(uri, openableMimeTypes[0], null)
258+
.createInputStream();
259+
}
260+
215261
}

0 commit comments

Comments
 (0)