Skip to content

Commit 6b5de39

Browse files
author
Ron Radtke
committed
Fixing the issue with action_view_intent
1 parent 7d91ccb commit 6b5de39

File tree

5 files changed

+64
-110
lines changed

5 files changed

+64
-110
lines changed

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtil.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,13 @@ public void run() {
113113
@ReactMethod
114114
public void actionViewIntent(String path, String mime, @Nullable String chooserTitle, final Promise promise) {
115115
try {
116-
Uri uriForFile = FileProvider.getUriForFile(this.getReactApplicationContext(),
117-
this.getReactApplicationContext().getPackageName() + ".provider", new File(path));
118-
116+
Uri uriForFile = null;
117+
if (!ReactNativeBlobUtilUtils.isContentUri(path)) {
118+
uriForFile = FileProvider.getUriForFile(this.getReactApplicationContext(),
119+
this.getReactApplicationContext().getPackageName() + ".provider", new File(path));
120+
} else {
121+
uriForFile = Uri.parse(path);
122+
}
119123
Intent intent = new Intent(Intent.ACTION_VIEW);
120124
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
121125
// Create the intent with data and type

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFS.java

Lines changed: 7 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
import android.os.Build;
88
import android.os.Environment;
99
import android.os.StatFs;
10-
import android.os.SystemClock;
1110
import android.util.Base64;
1211

13-
import com.ReactNativeBlobUtil.Utils.PathResolver;
1412
import com.facebook.react.bridge.Arguments;
1513
import com.facebook.react.bridge.Callback;
1614
import com.facebook.react.bridge.Promise;
@@ -20,13 +18,17 @@
2018
import com.facebook.react.bridge.WritableMap;
2119
import com.facebook.react.modules.core.DeviceEventManagerModule;
2220

23-
import java.io.*;
24-
import java.nio.charset.Charset;
21+
import java.io.File;
22+
import java.io.FileInputStream;
23+
import java.io.FileNotFoundException;
24+
import java.io.FileOutputStream;
25+
import java.io.IOException;
26+
import java.io.InputStream;
27+
import java.io.OutputStream;
2528
import java.security.MessageDigest;
2629
import java.util.ArrayList;
2730
import java.util.HashMap;
2831
import java.util.Map;
29-
import java.util.UUID;
3032

3133
class ReactNativeBlobUtilFS {
3234

@@ -329,106 +331,6 @@ static String getTmpPath(String taskId) {
329331
return ReactNativeBlobUtil.RCTContext.getFilesDir() + "/ReactNativeBlobUtilTmp_" + taskId;
330332
}
331333

332-
/**
333-
* Create a file stream for read
334-
*
335-
* @param path File stream target path
336-
* @param encoding File stream decoder, should be one of `base64`, `utf8`, `ascii`
337-
* @param bufferSize Buffer size of read stream, default to 4096 (4095 when encode is `base64`)
338-
*/
339-
void readStream(String path, String encoding, int bufferSize, int tick, final String streamId) {
340-
String resolved = ReactNativeBlobUtilUtils.normalizePath(path);
341-
if (resolved != null)
342-
path = resolved;
343-
344-
try {
345-
int chunkSize = encoding.equalsIgnoreCase("base64") ? 4095 : 4096;
346-
if (bufferSize > 0)
347-
chunkSize = bufferSize;
348-
349-
InputStream fs;
350-
351-
if (resolved != null && path.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET)) {
352-
fs = ReactNativeBlobUtil.RCTContext.getAssets().open(path.replace(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET, ""));
353-
}
354-
// fix issue 287
355-
else if (resolved == null) {
356-
fs = ReactNativeBlobUtil.RCTContext.getContentResolver().openInputStream(Uri.parse(path));
357-
} else {
358-
fs = new FileInputStream(new File(path));
359-
}
360-
361-
int cursor = 0;
362-
boolean error = false;
363-
364-
if (encoding.equalsIgnoreCase("utf8")) {
365-
InputStreamReader isr = new InputStreamReader(fs, Charset.forName("UTF-8"));
366-
BufferedReader reader = new BufferedReader(isr, chunkSize);
367-
char[] buffer = new char[chunkSize];
368-
// read chunks of the string
369-
while (reader.read(buffer, 0, chunkSize) != -1) {
370-
String chunk = new String(buffer);
371-
emitStreamEvent(streamId, "data", chunk);
372-
if (tick > 0)
373-
SystemClock.sleep(tick);
374-
}
375-
376-
reader.close();
377-
isr.close();
378-
} else if (encoding.equalsIgnoreCase("ascii")) {
379-
byte[] buffer = new byte[chunkSize];
380-
while ((cursor = fs.read(buffer)) != -1) {
381-
WritableArray chunk = Arguments.createArray();
382-
for (int i = 0; i < cursor; i++) {
383-
chunk.pushInt((int) buffer[i]);
384-
}
385-
emitStreamEvent(streamId, "data", chunk);
386-
if (tick > 0)
387-
SystemClock.sleep(tick);
388-
}
389-
} else if (encoding.equalsIgnoreCase("base64")) {
390-
byte[] buffer = new byte[chunkSize];
391-
while ((cursor = fs.read(buffer)) != -1) {
392-
if (cursor < chunkSize) {
393-
byte[] copy = new byte[cursor];
394-
System.arraycopy(buffer, 0, copy, 0, cursor);
395-
emitStreamEvent(streamId, "data", Base64.encodeToString(copy, Base64.NO_WRAP));
396-
} else
397-
emitStreamEvent(streamId, "data", Base64.encodeToString(buffer, Base64.NO_WRAP));
398-
if (tick > 0)
399-
SystemClock.sleep(tick);
400-
}
401-
} else {
402-
emitStreamEvent(
403-
streamId,
404-
"error",
405-
"EINVAL",
406-
"Unrecognized encoding `" + encoding + "`, should be one of `base64`, `utf8`, `ascii`"
407-
);
408-
error = true;
409-
}
410-
411-
if (!error)
412-
emitStreamEvent(streamId, "end", "");
413-
fs.close();
414-
415-
} catch (FileNotFoundException err) {
416-
emitStreamEvent(
417-
streamId,
418-
"error",
419-
"ENOENT",
420-
"No such file '" + path + "'"
421-
);
422-
} catch (Exception err) {
423-
emitStreamEvent(
424-
streamId,
425-
"error",
426-
"EUNSPECIFIED",
427-
"Failed to convert data to " + encoding + " encoded string. This might be because this encoding cannot be used for this data."
428-
);
429-
err.printStackTrace();
430-
}
431-
}
432334

433335
/**
434336
* Unlink file at path

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilMediaCollection.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
import android.os.Environment;
99
import android.os.ParcelFileDescriptor;
1010
import android.provider.MediaStore;
11+
import android.util.Base64;
1112

1213
import com.ReactNativeBlobUtil.Utils.FileDescription;
14+
import com.facebook.react.bridge.Arguments;
1315
import com.facebook.react.bridge.Promise;
16+
import com.facebook.react.bridge.WritableArray;
1417

1518
import java.io.File;
1619
import java.io.FileInputStream;
@@ -72,7 +75,7 @@ public static Uri createNewMediaFile(FileDescription file, MediaType mt) {
7275

7376
ContentValues fileDetails = new ContentValues();
7477
String relativePath = getRelativePath(mt);
75-
String mimeType = getRelativePath(mt);
78+
String mimeType = file.mimeType;
7679

7780
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
7881
fileDetails.put(MediaStore.MediaColumns.DATE_ADDED, System.currentTimeMillis() / 1000);
@@ -222,4 +225,45 @@ public static void copyToInternal(Uri contenturi, String destpath, Promise promi
222225

223226
promise.resolve("");
224227
}
228+
229+
public void getBlob(Uri contentUri, String encoding, Promise promise) {
230+
Context appCtx = ReactNativeBlobUtil.RCTContext.getApplicationContext();
231+
ContentResolver resolver = appCtx.getContentResolver();
232+
try {
233+
InputStream in = resolver.openInputStream(contentUri);
234+
int length = 0;
235+
236+
length = in.available();
237+
238+
byte[] bytes = new byte[length];
239+
int bytesRead = in.read(bytes);
240+
in.close();
241+
242+
if (bytesRead < length) {
243+
promise.reject("EUNSPECIFIED", "Read only " + bytesRead + " bytes of " + length);
244+
return;
245+
}
246+
247+
switch (encoding.toLowerCase()) {
248+
case "base64":
249+
promise.resolve(Base64.encodeToString(bytes, Base64.NO_WRAP));
250+
break;
251+
case "ascii":
252+
WritableArray asciiResult = Arguments.createArray();
253+
for (byte b : bytes) {
254+
asciiResult.pushInt((int) b);
255+
}
256+
promise.resolve(asciiResult);
257+
break;
258+
case "utf8":
259+
promise.resolve(new String(bytes));
260+
break;
261+
default:
262+
promise.resolve(new String(bytes));
263+
break;
264+
}
265+
} catch (IOException ioException) {
266+
ioException.printStackTrace();
267+
}
268+
}
225269
}

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,8 @@ public static String normalizePath(String path) {
142142
public static boolean isAsset(String path) {
143143
return path != null && path.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET);
144144
}
145+
146+
public static boolean isContentUri(String path) {
147+
return path != null && path.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX_CONTENT);
148+
}
145149
}

android/src/main/java/com/ReactNativeBlobUtil/Utils/MimeType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static String getExtensionFromMimeTypeOrFileName(String mimeType, String
5757
}
5858

5959
/**
60-
* Some file types return no mime type on older API levels. This function adds compatibility accross API levels.
60+
* Some file types return no mime type on older API levels. This function adds compatibility across API levels.
6161
*/
6262
public static String getMimeTypeFromExtension(String fileExtension) {
6363
if (fileExtension.equals("bin")) return BINARY_FILE;

0 commit comments

Comments
 (0)