This repository was archived by the owner on Dec 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathRNThumbnailModule.java
More file actions
84 lines (65 loc) · 2.6 KB
/
RNThumbnailModule.java
File metadata and controls
84 lines (65 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package me.hauvo.thumbnail;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import android.media.ThumbnailUtils;
import android.provider.MediaStore;
import android.provider.MediaStore.Video.Thumbnails;
import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;
import android.media.MediaMetadataRetriever;
import android.graphics.Matrix;
import java.util.UUID;
import java.io.File;
import java.io.OutputStream;
import java.io.FileOutputStream;
public class RNThumbnailModule extends ReactContextBaseJavaModule {
private final ReactApplicationContext reactContext;
public RNThumbnailModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}
@Override
public String getName() {
return "RNThumbnail";
}
@ReactMethod
public void get(String filePath, Promise promise) {
filePath = filePath.replace("file://","");
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
retriever.setDataSource(filePath);
Bitmap image = retriever.getFrameAtTime(1000000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
retriever.release();
String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/thumb";
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";
File file = new File(fullPath, fileName);
file.createNewFile();
fOut = new FileOutputStream(file);
// 100 means no compression, the lower you go, the stronger the compression
image.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
// MediaStore.Images.Media.insertImage(reactContext.getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
WritableMap map = Arguments.createMap();
map.putString("path", "file://" + fullPath + '/' + fileName);
map.putDouble("width", image.getWidth());
map.putDouble("height", image.getHeight());
promise.resolve(map);
} catch (Exception e) {
Log.e("E_RNThumnail_ERROR", e.getMessage());
promise.reject("E_RNThumnail_ERROR", e);
}
}
}