diff --git a/README.md b/README.md index ddb6e6b..4697f99 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,20 @@ Get thumbnail from local media. Currently, it only supports for video. ```javascript import RNThumbnail from 'react-native-thumbnail'; -RNThumbnail.get(filepath).then((result) => { - console.log(result.path); // thumbnail path -}) + +let options = { + saveToDir: '.app_thumbs', //default "/storage/emulated/0/thumb/" + uniqueNames: false,// default "false", same names +}; +const filePath = "/storage/emulated/0/DCIM/Camera/VID_20180116_202826.mp4"; +RNThumbnail.get(filePath, options).then((result) => { + console.log(filePath, result); + /** result: + { + height: 320 + path:"file:///storage/emulated/0/DCIM/Camera/.app_thumbs/VID_20180116_202826.mp4.jpeg" // thumbnail path + width:240 + } + */ +}); ``` diff --git a/android/src/main/java/com/reactlibrary/RNThumbnailModule.java b/android/src/main/java/com/reactlibrary/RNThumbnailModule.java index d2552dc..2eefbb7 100644 --- a/android/src/main/java/com/reactlibrary/RNThumbnailModule.java +++ b/android/src/main/java/com/reactlibrary/RNThumbnailModule.java @@ -7,6 +7,7 @@ import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.Promise; import com.facebook.react.bridge.Arguments; +import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.WritableMap; import android.media.ThumbnailUtils; import android.provider.MediaStore; @@ -38,23 +39,46 @@ public String getName() { } @ReactMethod - public void get(String filePath, Promise promise) { + public void get(String filePath, ReadableMap options, Promise promise) { + + // saveToDir: '.app_thumbs', // "/storage/emulated/0/.app_thumbs/", + // default "/storage/emulated/0/thumb/" + // uniqueNames: false, // default same names + + boolean uniqueNames = false; + if (options.hasKey("uniqueNames")) { + uniqueNames = options.getBoolean("uniqueNames"); + } + + String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/thumb"; + filePath = filePath.replace("file://",""); MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.setDataSource(filePath); Bitmap image = retriever.getFrameAtTime(1000000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC); - String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/thumb"; - try { + + File original = new File(filePath); + + if (options.hasKey("saveToDir")) { + fullPath = original.getParent().toString() + "/" + options.getString("saveToDir"); + } + File dir = new File(fullPath); if (!dir.exists()) { dir.mkdirs(); } OutputStream fOut = null; - // String fileName = "thumb-" + UUID.randomUUID().toString() + ".jpeg"; - String fileName = "thumb-" + UUID.randomUUID().toString() + ".jpeg"; + + + String fileName = original.getName() + ".jpeg"; + + if (uniqueNames) { + fileName = "thumb-" + UUID.randomUUID().toString() + ".jpeg"; + } + File file = new File(fullPath, fileName); file.createNewFile(); fOut = new FileOutputStream(file); diff --git a/package.json b/package.json index 27f091f..8c602d3 100644 --- a/package.json +++ b/package.json @@ -15,9 +15,6 @@ }, "author": "", "license": "", - "peerDependencies": { - "react-native": "^0.47.0" - }, "dependencies": { "react-native-thumbnail": "^1.1.0" }