diff --git a/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtil.java b/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtil.java index f7798d04c..8d92f5940 100644 --- a/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtil.java +++ b/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtil.java @@ -28,6 +28,7 @@ import com.facebook.react.modules.network.OkHttpClientProvider; import java.io.File; +import java.util.HashMap; import java.util.Map; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; @@ -88,7 +89,11 @@ public String getName() { @Override public Map getConstants() { - return ReactNativeBlobUtilFS.getSystemfolders(this.getReactApplicationContext()); + Map res = new HashMap<>(); + res.putAll(ReactNativeBlobUtilFS.getSystemfolders(this.getReactApplicationContext())); + res.putAll(ReactNativeBlobUtilFS.getLegacySystemfolders(this.getReactApplicationContext())); + + return res; } @ReactMethod @@ -440,7 +445,7 @@ public void createMediaFile(ReadableMap filedata, String mt, Promise promise) { @ReactMethod public void writeToMediaFile(String fileUri, String path, boolean transformFile, Promise promise) { boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(Uri.parse(fileUri), path, transformFile, promise); - if(res) promise.resolve("Success"); + if (res) promise.resolve("Success"); } @RequiresApi(api = Build.VERSION_CODES.Q) @@ -479,7 +484,7 @@ public void copyToMediaStore(ReadableMap filedata, String mt, String path, Promi } boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(fileuri, path, false, promise); - if(res) promise.resolve(fileuri.toString()); + if (res) promise.resolve(fileuri.toString()); } } diff --git a/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFS.java b/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFS.java index 3d59c5a80..76fa4d350 100644 --- a/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFS.java +++ b/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFS.java @@ -9,6 +9,8 @@ import android.os.StatFs; import android.util.Base64; +import androidx.annotation.NonNull; + import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.Promise; @@ -359,6 +361,34 @@ static Map getSystemfolders(ReactApplicationContext ctx) { return res; } + /** + * Static method that returns legacy system folders to JS context (usage of deprecated functions since these retunr different folders) + * + * @param ctx React Native application context + */ + + @NonNull + @SuppressWarnings("deprecation") + static Map getLegacySystemfolders(ReactApplicationContext ctx) { + Map res = new HashMap<>(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) return ReactNativeBlobUtilFS.getSystemfolders(ctx); + + res.put("LegacyDCIMDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath()); + res.put("LegacyPictureDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath()); + res.put("LegacyMusicDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath()); + res.put("LegacyDownloadDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()); + res.put("LegacyMovieDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath()); + res.put("LegacyRingtoneDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES).getAbsolutePath()); + + String state = Environment.getExternalStorageState(); + if (state.equals(Environment.MEDIA_MOUNTED)) { + res.put("LegacySDCardDir", Environment.getExternalStorageDirectory().getAbsolutePath()); + } + + return res; + } + static String getExternalFilesDirPath(ReactApplicationContext ctx, String type) { File dir = ctx.getExternalFilesDir(type); if (dir != null) return dir.getAbsolutePath(); @@ -809,7 +839,7 @@ static void hash(String path, String algorithm, Promise promise) { promise.reject("EINVAL", "Invalid algorithm '" + algorithm + "', must be one of md5, sha1, sha224, sha256, sha384, sha512"); return; } - + path = ReactNativeBlobUtilUtils.normalizePath(path); File file = new File(path); diff --git a/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilMediaCollection.java b/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilMediaCollection.java index f61992f9b..d40970bbf 100644 --- a/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilMediaCollection.java +++ b/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilMediaCollection.java @@ -69,11 +69,11 @@ private static String getRelativePath(MediaType mt, ReactApplicationContext ctx) if (mt == MediaType.Download) return Environment.DIRECTORY_DOWNLOADS; return Environment.DIRECTORY_DOWNLOADS; } else { - if (mt == MediaType.Audio) return ReactNativeBlobUtilFS.getExternalFilesDirPath(ctx, Environment.DIRECTORY_MUSIC); - if (mt == MediaType.Video) return ReactNativeBlobUtilFS.getExternalFilesDirPath(ctx, Environment.DIRECTORY_MOVIES); - if (mt == MediaType.Image) return ReactNativeBlobUtilFS.getExternalFilesDirPath(ctx, Environment.DIRECTORY_PICTURES); - if (mt == MediaType.Download) return ReactNativeBlobUtilFS.getExternalFilesDirPath(ctx, Environment.DIRECTORY_DOWNLOADS); - return ReactNativeBlobUtilFS.getExternalFilesDirPath(ctx, Environment.DIRECTORY_DOWNLOADS); + if (mt == MediaType.Audio) return ReactNativeBlobUtilFS.getLegacySystemfolders(ctx).get("LegacyMusicDir").toString(); + if (mt == MediaType.Video) return ReactNativeBlobUtilFS.getLegacySystemfolders(ctx).get("LegacyMovieDir").toString(); + if (mt == MediaType.Image) return ReactNativeBlobUtilFS.getLegacySystemfolders(ctx).get("LegacyPictureDir").toString(); + if (mt == MediaType.Download) return ReactNativeBlobUtilFS.getLegacySystemfolders(ctx).get("LegacyDownloadDir").toString(); + return ReactNativeBlobUtilFS.getLegacySystemfolders(ctx).get("LegacyDownloadDir").toString(); } } diff --git a/fs.js b/fs.js index 359774dab..301d863f2 100644 --- a/fs.js +++ b/fs.js @@ -24,7 +24,15 @@ const dirs = { SDCardApplicationDir: ReactNativeBlobUtil.SDCardApplicationDir, // Deprecated MainBundleDir: ReactNativeBlobUtil.MainBundleDir, LibraryDir: ReactNativeBlobUtil.LibraryDir, - ApplicationSupportDir: ReactNativeBlobUtil.ApplicationSupportDir + ApplicationSupportDir: ReactNativeBlobUtil.ApplicationSupportDir, + + LegacyPictureDir: ReactNativeBlobUtil.LegacyPictureDir, + LegacyMusicDir: ReactNativeBlobUtil.LegacyMusicDir, + LegacyMovieDir: ReactNativeBlobUtil.LegacyMovieDir, + LegacyDownloadDir: ReactNativeBlobUtil.LegacyDownloadDir, + LegacyDCIMDir: ReactNativeBlobUtil.LegacyDCIMDir, + LegacySDCardDir: ReactNativeBlobUtil.LegacySDCardDir, // Depracated + }; function addCode(code: string, error: Error): Error {