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

Commit

Permalink
Merge branch 'issue-287-merged' into uri
Browse files Browse the repository at this point in the history
  • Loading branch information
wkh237 committed Jul 2, 2017
2 parents 40efd14 + 40fefd4 commit 50a4d06
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
29 changes: 23 additions & 6 deletions android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,28 @@ static public void writeFile(String path, ReadableArray data, final boolean appe
* @param promise
*/
static public void readFile(String path, String encoding, final Promise promise ) {
path = normalizePath(path);
String resolved = normalizePath(path);
if(resolved != null)
path = resolved;
try {
byte[] bytes;

if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
if(resolved != null && resolved.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
String assetName = path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, "");
long length = RNFetchBlob.RCTContext.getAssets().openFd(assetName).getLength();
bytes = new byte[(int) length];
InputStream in = RNFetchBlob.RCTContext.getAssets().open(assetName);
in.read(bytes, 0, (int) length);
in.close();
}
// issue 287
else if(resolved == null) {
InputStream in = RNFetchBlob.RCTContext.getContentResolver().openInputStream(Uri.parse(path));
int length = (int) in.available();
bytes = new byte[length];
in.read(bytes);
in.close();
}
else {
File f = new File(path);
int length = (int) f.length();
Expand Down Expand Up @@ -226,17 +236,24 @@ static public String getTmpPath(ReactApplicationContext ctx, String taskId) {
* @param bufferSize Buffer size of read stream, default to 4096 (4095 when encode is `base64`)
*/
public void readStream(String path, String encoding, int bufferSize, int tick, final String streamId) {
path = normalizePath(path);
String resolved = normalizePath(path);
if(resolved != null)
path = resolved;
try {

int chunkSize = encoding.equalsIgnoreCase("base64") ? 4095 : 4096;
if(bufferSize > 0)
chunkSize = bufferSize;

InputStream fs;
if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
fs = RNFetchBlob.RCTContext.getAssets()
.open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));

if(resolved != null && path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
fs = RNFetchBlob.RCTContext.getAssets().open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));

}
// fix issue 287
else if(resolved == null) {
fs = RNFetchBlob.RCTContext.getContentResolver().openInputStream(Uri.parse(path));
}
else {
fs = new FileInputStream(new File(path));
Expand Down
22 changes: 18 additions & 4 deletions android/src/main/java/com/RNFetchBlob/Utils/PathResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,16 @@ else if (isMediaDocument(uri)) {

return getDataColumn(context, contentUri, selection, selectionArgs);
}
else if ("content".equalsIgnoreCase(uri.getScheme())) {

// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();

return getDataColumn(context, uri, null, null);
}
// Other Providers
else {
else{
try {
InputStream attachment = context.getContentResolver().openInputStream(uri);
if (attachment != null) {
Expand Down Expand Up @@ -131,6 +139,7 @@ public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {

Cursor cursor = null;
String result = null;
final String column = "_data";
final String[] projection = {
column
Expand All @@ -141,13 +150,18 @@ public static String getDataColumn(Context context, Uri uri, String selection,
null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
result = cursor.getString(index);
}
} finally {
}
catch (Exception ex) {
ex.printStackTrace();
return null;
}
finally {
if (cursor != null)
cursor.close();
}
return null;
return result;
}


Expand Down

0 comments on commit 50a4d06

Please sign in to comment.