From c93a1420fdba3bdce6b9ac467ed0106772754080 Mon Sep 17 00:00:00 2001 From: Alexander Khaylo Date: Tue, 9 Jan 2018 16:34:07 +0200 Subject: [PATCH 1/3] Update package.json --- package.json | 3 --- 1 file changed, 3 deletions(-) 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" } From ed5ed9ce41eda792ddf56950dfbe16928965365f Mon Sep 17 00:00:00 2001 From: Alexander Khaylo Date: Wed, 17 Jan 2018 02:13:55 +0200 Subject: [PATCH 2/3] Added options "saveToDir" and "uniqueNames" Added options "saveToDir" and "uniqueNames" (for using same images https://github.com/phuochau/react-native-thumbnail/issues/3 ) ```javascript //Example 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" width:240 } */ }); ``` --- .../com/reactlibrary/RNThumbnailModule.java | 34 ++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) 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); From f42c9df38275398888bb79b377c2b3ecd911e3b4 Mon Sep 17 00:00:00 2001 From: Alexander Khaylo Date: Wed, 17 Jan 2018 02:18:52 +0200 Subject: [PATCH 3/3] Added example using "saveToDir" and "uniqueNames" options --- README.md | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) 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 + } + */ +}); ```