我在react native中有一个小的native模块,可以在指定时间从视频文件获取缩略图。问题是,当我从本机反应多次调用此方法时,我得到了java.lang.OutOfMemoryError
。
这是java方法:
@ReactMethod
public void get(String filePath, Integer atTimeValue, Promise promise) {
filePath = filePath.replace("file://","");
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(filePath);
Bitmap image = retriever.getFrameAtTime(atTimeValue, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 80, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
try {
// MediaStore.Images.Media.insertImage(reactContext.getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
WritableMap map = Arguments.createMap();
map.putString("path", encoded);
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);
}
}
这是完整的堆栈跟踪:
java.lang.RuntimeException Could not invoke RNThumbnail.get
JavaMethodWrapper.java:383 com.facebook.react.bridge.JavaMethodWrapper.invoke
JavaModuleWrapper.java:158 com.facebook.react.bridge.JavaModuleWrapper.invoke
NativeRunnable.java:-2 com.facebook.react.bridge.queue.NativeRunnable.run
Handler.java:873 android.os.Handler.handleCallback
Handler.java:99 android.os.Handler.dispatchMessage
MessageQueueThreadHandler.java:29 com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage
Looper.java:193 android.os.Looper.loop
MessageQueueThreadImpl.java:192 com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run
Thread.java:764 java.lang.Thread.run
Caused by: java.lang.reflect.InvocationTargetException
Method.java:-2 java.lang.reflect.Method.invoke
JavaMethodWrapper.java:372 com.facebook.react.bridge.JavaMethodWrapper.invoke
JavaModuleWrapper.java:158 com.facebook.react.bridge.JavaModuleWrapper.invoke
NativeRunnable.java:-2 com.facebook.react.bridge.queue.NativeRunnable.run
Handler.java:873 android.os.Handler.handleCallback
Handler.java:99 android.os.Handler.dispatchMessage
MessageQueueThreadHandler.java:29 com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage
Looper.java:193 android.os.Looper.loop
MessageQueueThreadImpl.java:192 com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run
Thread.java:764 java.lang.Thread.run
Caused by: java.lang.OutOfMemoryError Failed to allocate a 88552 byte allocation with 12584 free bytes and 12KB until OOM, max allowed footprint 268435456, growth limit 268435456
StringFactory.java:234 java.lang.StringFactory.newStringFromBytes
StringFactory.java:65 java.lang.StringFactory.newStringFromBytes
Base64.java:456 android.util.Base64.encodeToString
RNThumbnailModule.java:70 me.hauvo.thumbnail.RNThumbnailModule.get
Method.java:-2 java.lang.reflect.Method.invoke
JavaMethodWrapper.java:372 com.facebook.react.bridge.JavaMethodWrapper.invoke
JavaModuleWrapper.java:158 com.facebook.react.bridge.JavaModuleWrapper.invoke
NativeRunnable.java:-2 com.facebook.react.bridge.queue.NativeRunnable.run
Handler.java:873 android.os.Handler.handleCallback
Handler.java:99 android.os.Handler.dispatchMessage
MessageQueueThreadHandler.java:29 com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage
Looper.java:193 android.os.Looper.loop
MessageQueueThreadImpl.java:192 com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run
Thread.java:764 java.lang.Thread.run
有什么办法解决此内存问题吗?