Skip to content

Commit 7fa26be

Browse files
author
Ron Radtke
committed
fixing documentation and some promises
1 parent 976734b commit 7fa26be

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import android.util.SparseArray;
1010

1111
import androidx.annotation.NonNull;
12+
import androidx.annotation.RequiresApi;
1213
import androidx.core.content.FileProvider;
1314

1415
import com.ReactNativeBlobUtil.Utils.FileDescription;
@@ -436,9 +437,11 @@ public void createMediaFile(ReadableMap filedata, String mt, Promise promise) {
436437
else promise.reject("ReactNativeBlobUtil.createMediaFile", "File could not be created");
437438
}
438439

440+
@RequiresApi(api = Build.VERSION_CODES.Q)
439441
@ReactMethod
440442
public void writeToMediaFile(String fileUri, String path, Promise promise) {
441-
ReactNativeBlobUtilMediaCollection.writeToMediaFile(Uri.parse(fileUri), path, promise);
443+
boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(Uri.parse(fileUri), path, promise);
444+
if(res) promise.resolve("Success");
442445
}
443446

444447
@ReactMethod
@@ -451,6 +454,7 @@ public void getBlob(String contentUri, String encoding, Promise promise) {
451454
ReactNativeBlobUtilMediaCollection.getBlob(Uri.parse(contentUri), encoding, promise);
452455
}
453456

457+
@RequiresApi(api = Build.VERSION_CODES.Q)
454458
@ReactMethod
455459
public void copyToMediaStore(ReadableMap filedata, String mt, String path, Promise promise) {
456460
if (!(filedata.hasKey("name") && filedata.hasKey("parentFolder") && filedata.hasKey("mimeType"))) {
@@ -470,7 +474,9 @@ public void copyToMediaStore(ReadableMap filedata, String mt, String path, Promi
470474
return;
471475
}
472476

473-
ReactNativeBlobUtilMediaCollection.writeToMediaFile(fileuri, path, promise);
477+
boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(fileuri, path, promise);
478+
479+
if(res) promise.resolve(fileuri);
474480
}
475481

476482
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import android.provider.MediaStore;
1111
import android.util.Base64;
1212

13+
import androidx.annotation.RequiresApi;
14+
1315
import com.ReactNativeBlobUtil.Utils.FileDescription;
1416
import com.facebook.react.bridge.Arguments;
1517
import com.facebook.react.bridge.Promise;
@@ -103,7 +105,8 @@ public static Uri createNewMediaFile(FileDescription file, MediaType mt) {
103105
return null;
104106
}
105107

106-
public static void writeToMediaFile(Uri fileUri, String data, Promise promise) {
108+
@RequiresApi(api = Build.VERSION_CODES.Q)
109+
public static boolean writeToMediaFile(Uri fileUri, String data, Promise promise) {
107110
try {
108111
Context appCtx = ReactNativeBlobUtil.RCTContext.getApplicationContext();
109112
ContentResolver resolver = appCtx.getContentResolver();
@@ -145,6 +148,8 @@ public static void writeToMediaFile(Uri fileUri, String data, Promise promise) {
145148
descr.close();
146149
} catch (Exception e) {
147150
e.printStackTrace();
151+
promise.reject(new IOException("Failed to get output stream."));
152+
return false;
148153
}
149154

150155
contentValues.clear();
@@ -153,15 +158,17 @@ public static void writeToMediaFile(Uri fileUri, String data, Promise promise) {
153158
stream = resolver.openOutputStream(fileUri);
154159
if (stream == null) {
155160
promise.reject(new IOException("Failed to get output stream."));
161+
return false;
156162
}
157163
} catch (IOException e) {
158164
// Don't leave an orphan entry in the MediaStore
159165
resolver.delete(uri, null, null);
160166
promise.reject(e);
167+
return false;
161168
} finally {
162169
if (stream != null) {
163170
stream.close();
164-
promise.resolve("");
171+
return true;
165172
}
166173
}
167174

index.d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,7 @@ export type Mediatype = "Audio" | "Image" | "Video" | "Download";
786786
export interface MediaCollection {
787787
/**
788788
* Creates a new File in the collection.
789+
* Promise will resolve to content UIR or error message
789790
* @param filedata descriptor for the media store entry
790791
* @param mediatype
791792
* @param path path of the file being copied
@@ -805,19 +806,19 @@ export interface MediaCollection {
805806
* @param uri URI of the destination mediastore file
806807
* @param path Path to the existing file which should be copied
807808
*/
808-
writeToMediafile(uri: string, path: string)
809+
writeToMediafile(uri: string, path: string): Promise<string>
809810

810811
/**
811812
* Copies a file from the mediastore to the apps internal storage
812813
* @param contenturi URI of the mediastore file
813814
* @param destpath Path for the file in the internal storage
814815
*/
815-
copyToInternal(contenturi: string, destpath: string)
816+
copyToInternal(contenturi: string, destpath: string): Promise<string>
816817

817818
/**
818819
* Gets the blob data for a given URI in the mediastore
819820
* @param contenturi
820821
* @param encoding
821822
*/
822-
getBlob(contenturi: string, encoding: string)
823+
getBlob(contenturi: string, encoding: string): Promise<string>
823824
}

0 commit comments

Comments
 (0)