mirror of
https://github.com/status-im/react-native-camera-roll.git
synced 2026-08-31 05:11:07 +00:00
feat(lib): save photos or videos to an album
* add option to specify album in saveToCameraRoll and move the optional type param to options * check platform before setting default value for group types to prevent exception * adjust typings * update invariant message * format code * extract new implementation to function to avoid breaking change * format code * add missing spaces * fix(lib): add accidentally removed savedphotos back to the enum to prevent crash * chore(lib): formatting * chore(lib): add doc for the new save method
This commit is contained in:
@@ -100,8 +100,8 @@ public class CameraRollModule extends ReactContextBaseJavaModule {
|
||||
* @param promise to be resolved or rejected
|
||||
*/
|
||||
@ReactMethod
|
||||
public void saveToCameraRoll(String uri, String type, Promise promise) {
|
||||
new SaveToCameraRoll(getReactApplicationContext(), Uri.parse(uri), promise)
|
||||
public void saveToCameraRoll(String uri, ReadableMap options, Promise promise) {
|
||||
new SaveToCameraRoll(getReactApplicationContext(), Uri.parse(uri), options, promise)
|
||||
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
|
||||
@@ -110,12 +110,14 @@ public class CameraRollModule extends ReactContextBaseJavaModule {
|
||||
private final Context mContext;
|
||||
private final Uri mUri;
|
||||
private final Promise mPromise;
|
||||
private final ReadableMap mOptions;
|
||||
|
||||
public SaveToCameraRoll(ReactContext context, Uri uri, Promise promise) {
|
||||
public SaveToCameraRoll(ReactContext context, Uri uri, ReadableMap options, Promise promise) {
|
||||
super(context);
|
||||
mContext = context;
|
||||
mUri = uri;
|
||||
mPromise = promise;
|
||||
mOptions = options;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -123,8 +125,25 @@ public class CameraRollModule extends ReactContextBaseJavaModule {
|
||||
File source = new File(mUri.getPath());
|
||||
FileChannel input = null, output = null;
|
||||
try {
|
||||
File exportDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
|
||||
exportDir.mkdirs();
|
||||
File environment;
|
||||
if ("mov".equals(mOptions.getString("type"))) {
|
||||
environment = Environment.getExternalStoragePublicDirectory(
|
||||
Environment.DIRECTORY_MOVIES);
|
||||
} else {
|
||||
environment = Environment.getExternalStoragePublicDirectory(
|
||||
Environment.DIRECTORY_PICTURES);
|
||||
}
|
||||
File exportDir;
|
||||
if (!"".equals(mOptions.getString("album"))) {
|
||||
exportDir = new File(environment, mOptions.getString("album"));
|
||||
if (!exportDir.exists() && !exportDir.mkdirs()) {
|
||||
mPromise.reject(ERROR_UNABLE_TO_LOAD, "Album Directory not created. Did you request WRITE_EXTERNAL_STORAGE?");
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
exportDir = environment;
|
||||
}
|
||||
|
||||
if (!exportDir.isDirectory()) {
|
||||
mPromise.reject(ERROR_UNABLE_TO_LOAD, "External media storage directory not available");
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user