Skip to content

Commit c4cad5e

Browse files
committed
add file stream
1 parent 5ef9e14 commit c4cad5e

File tree

1 file changed

+64
-4
lines changed

1 file changed

+64
-4
lines changed

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

+64-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,24 +185,49 @@ 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+
int flag = cursor.getInt(0);
208+
if(DocumentPickerModule.isVirtualFile(flag)) {
209+
try {
210+
InputStream input = DocumentPickerModule.getInputStreamForVirtualFile(contentResolver,uri,contentResolver.getType(uri));
211+
File file = new File(getReactApplicationContext().getCacheDir(),fileName);
212+
OutputStream output = new FileOutputStream(file);
213+
byte[] buffer = new byte[4 * 1024]; // or other buffer size
214+
int read;
215+
while ((read = input.read(buffer)) != -1) {
216+
output.write(buffer, 0, read);
217+
}
218+
output.flush();
219+
map.putString(FIELD_URI, file.getPath());
220+
} catch (IOException e) {
221+
throw new FileNotFoundException();
222+
}
223+
} else {
224+
map.putString(FIELD_URI, uri.toString());
225+
}
226+
} else {
227+
map.putString(FIELD_URI, uri.toString());
193228
}
194229

230+
195231
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
196232
int mimeIndex = cursor.getColumnIndex(DocumentsContract.Document.COLUMN_MIME_TYPE);
197233
if (!cursor.isNull(mimeIndex)) {
@@ -212,4 +248,28 @@ private WritableMap getMetadata(Uri uri) {
212248

213249
return map;
214250
}
251+
252+
private static boolean isVirtualFile(int flags) {
253+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
254+
return (flags & DocumentsContract.Document.FLAG_VIRTUAL_DOCUMENT) != 0;
255+
} else {
256+
return false;
257+
}
258+
}
259+
260+
private static InputStream getInputStreamForVirtualFile(ContentResolver resolver, Uri uri, String mimeTypeFilter)
261+
throws IOException {
262+
263+
String[] openableMimeTypes = resolver.getStreamTypes(uri, mimeTypeFilter);
264+
265+
if (openableMimeTypes == null ||
266+
openableMimeTypes.length < 1) {
267+
throw new FileNotFoundException();
268+
}
269+
270+
return resolver
271+
.openTypedAssetFileDescriptor(uri, openableMimeTypes[0], null)
272+
.createInputStream();
273+
}
274+
215275
}

0 commit comments

Comments
 (0)