Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Android Feature: Handle Arbitrary Providers #374

Merged
merged 2 commits into from
May 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ You may want to take a look on that page or find issues tagged "trouble shooting
* please provide the version of installed library and RN project.
* a sample code snippet/repository is very helpful to spotting the problem.
* issues which have been tagged as 'needs feedback', will be closed after 2 weeks if receive no feedbacks.
* issues lack of detailed information will be closed without any feedback
40 changes: 40 additions & 0 deletions android/src/main/java/com/RNFetchBlob/Utils/PathResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
import android.provider.MediaStore;
import android.content.ContentUris;
import android.os.Environment;
import android.content.ContentResolver;
import com.RNFetchBlob.RNFetchBlobUtils;
import java.io.File;
import java.io.InputStream;
import java.io.FileOutputStream;

public class PathResolver {
public static String getRealPathFromURI(final Context context, final Uri uri) {
Expand Down Expand Up @@ -59,6 +64,29 @@ else if (isMediaDocument(uri)) {

return getDataColumn(context, contentUri, selection, selectionArgs);
}
// Other Providers
else {
try {
InputStream attachment = context.getContentResolver().openInputStream(uri);
if (attachment != null) {
String filename = getContentName(context.getContentResolver(), uri);
if (filename != null) {
File file = new File(context.getCacheDir(), filename);
FileOutputStream tmp = new FileOutputStream(file);
byte[] buffer = new byte[1024];
while (attachment.read(buffer) > 0) {
tmp.write(buffer);
}
tmp.close();
attachment.close();
return file.getAbsolutePath();
}
}
} catch (Exception e) {
RNFetchBlobUtils.emitWarningEvent(e.toString());
return null;
}
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
Expand All @@ -77,6 +105,18 @@ else if ("file".equalsIgnoreCase(uri.getScheme())) {
return null;
}

private static String getContentName(ContentResolver resolver, Uri uri) {
Cursor cursor = resolver.query(uri, null, null, null, null);
cursor.moveToFirst();
int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
if (nameIndex >= 0) {
String name = cursor.getString(nameIndex);
cursor.close();
return name;
}
return null;
}

/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
Expand Down