2

我有一个大问题,我正在从驻留在 Sdcard 中的视频文件中读取 2^21 个字节,但是当我们在 ByteArrayOutputStream 中写入相同内容时,它会抛出一个异常,因为内存不足。我不知道为什么?请为此提出正确的解决方案。

这是堆栈跟踪。

错误堆栈:

11-03 19:24:23.193: ERROR/AndroidRuntime(28258): FATAL EXCEPTION: main
11-03 19:24:23.193: ERROR/AndroidRuntime(28258): java.lang.OutOfMemoryError
11-03 19:24:23.193: ERROR/AndroidRuntime(28258):     at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:93)
11-03 19:24:23.193: ERROR/AndroidRuntime(28258):     at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:218)
11-03 19:24:23.193: ERROR/AndroidRuntime(28258):     at com.example.TestCryptoActivity.onCreate(TestCryptoActivity.java:42)
11-03 19:24:23.193: ERROR/AndroidRuntime(28258):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-03 19:24:23.193: ERROR/AndroidRuntime(28258):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-03 19:24:23.193: ERROR/AndroidRuntime(28258):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-03 19:24:23.193: ERROR/AndroidRuntime(28258):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-03 19:24:23.193: ERROR/AndroidRuntime(28258):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-03 19:24:23.193: ERROR/AndroidRuntime(28258):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 19:24:23.193: ERROR/AndroidRuntime(28258):     at android.os.Looper.loop(Looper.java:123)
11-03 19:24:23.193: ERROR/AndroidRuntime(28258):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-03 19:24:23.193: ERROR/AndroidRuntime(28258):     at java.lang.reflect.Method.invokeNative(Native Method)
11-03 19:24:23.193: ERROR/AndroidRuntime(28258):     at java.lang.reflect.Method.invoke(Method.java:521)
11-03 19:24:23.193: ERROR/AndroidRuntime(28258):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-03 19:24:23.193: ERROR/AndroidRuntime(28258):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-03 19:24:23.193: ERROR/AndroidRuntime(28258):     at dalvik.system.NativeStart.main(Native Method)

代码:

try {
    is = new FileInputStream("/sdcard/E0022505.mp4");
} catch (FileNotFoundException e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
}

ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
try {
    while ((bytesRead  = is.read(b)) != -1) {
        bos.write(b, 0, bytesRead);//--------This position returns the same error
    }
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

byte[] bytes = bos.toByteArray();
try {
    String byteString = new String(bytes,"UTF-8");
    System.out.println("the bytes array of video:"+byteString);
} catch (UnsupportedEncodingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
4

2 回答 2

6

你在做什么是不实际的,但这样做的方法如下:

File file = new File("/sdcard/E0022505.mp4");
try {
       is = new FileInputStream(file);
} catch (FileNotFoundException e2) {
       e2.printStackTrace();
}
byte[] fileData = new byte[file.length()];
int read = 0;
while(read != fileData.length) {
   read += is.read(fileData, read, fileData.length - read);
}
//fileData now has all of the files bytes (don't forget to close is)
于 2011-11-03T15:15:59.737 回答
1

如果您知道文件将有多大,您可能希望在构造函数中将 bytearrayoutputstream 播种为该大小(或稍大)。我不是 100% 确定细节,但我认为 BAOS 会创建一个新数组,并在每次调用 'expand' 时复制旧数组。其他实现保留在输出时重建的数组列表。无论哪种方式,您都不希望那样(内存使用量翻倍,或更糟)。

new ByteArrayOutputStream(new File(Environment.getExternalStorageDirectory(), "E0022505.mp4").length());
于 2011-11-03T15:05:59.757 回答