Skip to content

Commit

Permalink
fixing an issue with MediaStore create file for API < Q
Browse files Browse the repository at this point in the history
  • Loading branch information
Ron Radtke authored and Ron Radtke committed Feb 23, 2022
1 parent 71d1c44 commit 7b5fb99
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -432,29 +432,29 @@ public void createMediaFile(ReadableMap filedata, String mt, Promise promise) {
if (mt == null) promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid mediatype");

FileDescription file = new FileDescription(filedata.getString("name"), filedata.getString("mimeType"), filedata.getString("parentFolder"));
Uri res = ReactNativeBlobUtilMediaCollection.createNewMediaFile(file, ReactNativeBlobUtilMediaCollection.MediaType.valueOf(mt));
Uri res = ReactNativeBlobUtilMediaCollection.createNewMediaFile(file, ReactNativeBlobUtilMediaCollection.MediaType.valueOf(mt), this.getReactApplicationContext());
if (res != null) promise.resolve(res.toString());
else promise.reject("ReactNativeBlobUtil.createMediaFile", "File could not be created");
}

@RequiresApi(api = Build.VERSION_CODES.Q)
@ReactMethod
public void writeToMediaFile(String fileUri, String path, Promise promise) {
boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(Uri.parse(fileUri), path, promise);
if(res) promise.resolve("Success");
}

@RequiresApi(api = Build.VERSION_CODES.Q)
@ReactMethod
public void copyToInternal(String contentUri, String destpath, Promise promise) {
ReactNativeBlobUtilMediaCollection.copyToInternal(Uri.parse(contentUri), destpath, promise);
}

@RequiresApi(api = Build.VERSION_CODES.Q)
@ReactMethod
public void getBlob(String contentUri, String encoding, Promise promise) {
ReactNativeBlobUtilMediaCollection.getBlob(Uri.parse(contentUri), encoding, promise);
}

@RequiresApi(api = Build.VERSION_CODES.Q)
@ReactMethod
public void copyToMediaStore(ReadableMap filedata, String mt, String path, Promise promise) {
if (!(filedata.hasKey("name") && filedata.hasKey("parentFolder") && filedata.hasKey("mimeType"))) {
Expand All @@ -471,15 +471,14 @@ public void copyToMediaStore(ReadableMap filedata, String mt, String path, Promi
}

FileDescription file = new FileDescription(filedata.getString("name"), filedata.getString("mimeType"), filedata.getString("parentFolder"));
Uri fileuri = ReactNativeBlobUtilMediaCollection.createNewMediaFile(file, ReactNativeBlobUtilMediaCollection.MediaType.valueOf(mt));
Uri fileuri = ReactNativeBlobUtilMediaCollection.createNewMediaFile(file, ReactNativeBlobUtilMediaCollection.MediaType.valueOf(mt), this.getReactApplicationContext());

if (fileuri == null) {
promise.reject("ReactNativeBlobUtil.createMediaFile", "File could not be created");
return;
}

boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(fileuri, path, promise);

if(res) promise.resolve(fileuri.toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,75 @@ class ReactNativeBlobUtilFS {
this.emitter = ctx.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
}

/**
* Write string with encoding to file (used for mediastore)
*
* @param path Destination file path.
* @param encoding Encoding of the string.
* @param data Array passed from JS context.
*/
static boolean writeFile(String path, String encoding, String data, final boolean append) {
try {
int written;
File f = new File(path);
File dir = f.getParentFile();
if (!f.exists()) {
if (dir != null && !dir.exists()) {
if (!dir.mkdirs() && !dir.exists()) {
return false;
}
}
if (!f.createNewFile()) {
return false;
}
}

// write data from a file
if (encoding.equalsIgnoreCase(ReactNativeBlobUtilConst.DATA_ENCODE_URI)) {
String normalizedData = ReactNativeBlobUtilUtils.normalizePath(data);
File src = new File(normalizedData);
if (!src.exists()) {
return false;
}
byte[] buffer = new byte[10240];
int read;
written = 0;
FileInputStream fin = null;
FileOutputStream fout = null;
try {
fin = new FileInputStream(src);
fout = new FileOutputStream(f, append);
while ((read = fin.read(buffer)) > 0) {
fout.write(buffer, 0, read);
written += read;
}
} finally {
if (fin != null) {
fin.close();
}
if (fout != null) {
fout.close();
}
}
} else {
byte[] bytes = ReactNativeBlobUtilUtils.stringToBytes(data, encoding);
FileOutputStream fout = new FileOutputStream(f, append);
try {
fout.write(bytes);
written = bytes.length;
} finally {
fout.close();
}
}
return true;
} catch (FileNotFoundException e) {
// According to https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
return false;
} catch (Exception e) {
return false;
}
}

/**
* Write string with encoding to file
*
Expand All @@ -57,7 +126,6 @@ static void writeFile(String path, String encoding, String data, final boolean a
int written;
File f = new File(path);
File dir = f.getParentFile();

if (!f.exists()) {
if (dir != null && !dir.exists()) {
if (!dir.mkdirs() && !dir.exists()) {
Expand Down
Loading

0 comments on commit 7b5fb99

Please sign in to comment.