Skip to content
This repository was archived by the owner on Dec 2, 2019. It is now read-only.
Open
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
36 changes: 31 additions & 5 deletions android/src/main/java/me/hauvo/thumbnail/RNThumbnailModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
import android.os.Environment;
import android.util.Log;
import android.media.MediaMetadataRetriever;
import android.graphics.Matrix;
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;
Expand All @@ -32,18 +31,39 @@ public RNThumbnailModule(ReactApplicationContext reactContext) {
this.reactContext = reactContext;
}

public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
int width = image.getWidth();
int height = image.getHeight();

float bitmapRatio = (float) width / (float) height;
if (bitmapRatio > 1) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}

return Bitmap.createScaledBitmap(image, width, height, true);
}

@Override
public String getName() {
return "RNThumbnail";
}

@ReactMethod
public void get(String filePath, Promise promise) {
filePath = filePath.replace("file://","");
public void getResized(String filePath, Integer maxSize, Promise promise) {
filePath = filePath.replace("file://", "");
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(filePath);
Bitmap image = retriever.getFrameAtTime(1000000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);

if (maxSize != null) {
// resize the image based on the maxSize parameter
image = getResizedBitmap(image, maxSize);
}

String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/thumb";

try {
Expand All @@ -64,7 +84,8 @@ public void get(String filePath, Promise promise) {
fOut.flush();
fOut.close();

// MediaStore.Images.Media.insertImage(reactContext.getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
// MediaStore.Images.Media.insertImage(reactContext.getContentResolver(),
// file.getAbsolutePath(), file.getName(), file.getName());

WritableMap map = Arguments.createMap();

Expand All @@ -79,4 +100,9 @@ public void get(String filePath, Promise promise) {
promise.reject("E_RNThumnail_ERROR", e);
}
}

@ReactMethod
public void get(String filePath, Promise promise) {
getResized(filePath, null, promise);
}
}