Skip to content
This repository has been archived by the owner on Dec 2, 2019. It is now read-only.

Added options "saveToDir" and "uniqueNames" and removed peerDependencies #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
*/
});
```
34 changes: 29 additions & 5 deletions android/src/main/java/com/reactlibrary/RNThumbnailModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
},
"author": "",
"license": "",
"peerDependencies": {
"react-native": "^0.47.0"
},
"dependencies": {
"react-native-thumbnail": "^1.1.0"
}
Expand Down