當我轉換小視頻時,它不會報錯。但是當我轉換更大的視頻時,它會給出下面的錯誤。將視頻轉換爲base64會導致「內存不足錯誤」
FATAL EXCEPTION: main
java.lang.OutOfMemoryError
at android.util.Base64.encode(Base64.java:534)
at android.util.Base64.encode(Base64.java:494)
at android.util.Base64.encodeToString(Base64.java:456)
at gcm.b4deploy.com.operations2.StartedTasksAdapter.videoToBase64(StartedTasksAdapter.java:402)
at gcm.b4deploy.com.operations2.StartedTasksAdapter.access$200(StartedTasksAdapter.java:57)
gcm.b4deploy.com.operations2.StartedTasksAdapter$2$1.fileSelected(StartedTasksAdapter.java:288)
at gcm.b4deploy.com.operations2.FileChooser$1.onItemClick(FileChooser.java:57)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1114)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2937)
at android.widget.AbsListView$1.run(AbsListView.java:3695)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5037)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
at dalvik.system.NativeStart.main(Native Method)
以下是我的videoToBase64方法;
private String videoToBase64(File file) {
String encodedString = null;
InputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
} catch (Exception e) {
// TODO: handle exception
}
byte[] bytes;
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
while ((bytesRead = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
bytes = output.toByteArray();
**encodedString = Base64.encodeToString(bytes, Base64.DEFAULT);**//error is in this line
Log.i("Strng", encodedString);
return encodedString;
}
我看過一些關於這個問題的問題,說我應該分開視頻和部分編碼,但我不知道該怎麼做。
是否有任何想法是什麼是視頻的綁定大小能夠不能得到這outOfMemoryError? 在此先感謝。