我正在创建一个可以将视频从服务器流式传输到Android移动设备的Android应用程序。我有正确的图像流和工作,但15秒后应用程序崩溃。我设法跟踪到Throwing OutOfMemoryError。我尝试回收位图后,我传递一个接口,将其带到线程显示它但我得到错误"无法重复使用回收的Bitmap"。我不确定如何修复此错误,即使斜倚也会修复它。
int read_count = 1;
long start_time = System.currentTimeMillis();
long timeout = 10000;
boolean timed_out = false;
byte[] data = new byte[size + 1];
while (read_count < size && !timed_out)
{
int len = in.read(data, read_count, size - read_count);
read_count += len;
timed_out = (System.currentTimeMillis() - start_time) >= timeout;
}
data[0] = (byte)0x89;
if (read_count == size)
{
final boolean is_left = (side == 0);
final byte[] tmp = data;
Bitmap Image_data = null;
System.out.println(tmp.length);
if (Listener != null)
{
Image_data = BitmapFactory.decodeByteArray(tmp, 0, tmp.length);
Listener.OnNewImageListenerBitmap(Image_data, is_left);
// this is where i tried recycling it//
}
}
我设法跟踪行的内存错误&#34; byte [] data = new byte [size + 1];&#34;但经过研究后,我得到了由位图引起的印象。
有没有其他人遇到过这个问题,并设法修复它?对此的任何帮助都会很棒:D
由于
LOGCATOUTPUT:
02-10 15:22:15.488 32097-32122/com.google.vrtoolkit.cardboard.samples.treasurehunt I/System.out﹕ 373348
02-10 15:22:16.655 32097-32122/com.google.vrtoolkit.cardboard.samples.treasurehunt I/System.out﹕ 373348
02-10 15:22:17.371 32097-32122/com.google.vrtoolkit.cardboard.samples.treasurehunt I/System.out﹕ 373348
02-10 15:22:18.827 32097-32122/com.google.vrtoolkit.cardboard.samples.treasurehunt I/System.out﹕ 373348
02-10 15:22:29.167 32097-32122/com.google.vrtoolkit.cardboard.samples.treasurehunt I/art﹕ Alloc sticky concurrent mark sweep GC freed 37627(1380KB) AllocSpace objects, 0(0B) LOS objects, 6% free, 114MB/122MB, paused 905us total 7.018ms
02-10 15:22:29.178 32097-32122/com.google.vrtoolkit.cardboard.samples.treasurehunt I/art﹕ Alloc partial concurrent mark sweep GC freed 110(3KB) AllocSpace objects, 2(92MB) LOS objects, 40% free, 22MB/36MB, paused 1.112ms total 11.404ms
02-10 15:22:29.202 32097-32122/com.google.vrtoolkit.cardboard.samples.treasurehunt I/art﹕ Alloc concurrent mark sweep GC freed 141(17KB) AllocSpace objects, 0(0B) LOS objects, 40% free, 22MB/36MB, paused 825us total 23.893ms
02-10 15:22:29.202 32097-32122/com.google.vrtoolkit.cardboard.samples.treasurehunt I/art﹕ Forcing collection of SoftReferences for 1GB allocation
02-10 15:22:29.223 32097-32122/com.google.vrtoolkit.cardboard.samples.treasurehunt I/art﹕ Alloc concurrent mark sweep GC freed 67(2520B) AllocSpace objects, 0(0B) LOS objects, 39% free, 22MB/36MB, paused 2.473ms total 18.440ms
02-10 15:22:29.223 32097-32122/com.google.vrtoolkit.cardboard.samples.treasurehunt E/art﹕ Throwing OutOfMemoryError "Failed to allocate a 1801149826 byte allocation with 15419532 free bytes and 233MB until OOM"
02-10 15:22:29.223 32097-32122/com.google.vrtoolkit.cardboard.samples.treasurehunt E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-1308
Process: com.google.vrtoolkit.cardboard.samples.treasurehunt, PID: 32097
java.lang.OutOfMemoryError: Failed to allocate a 1801149826 byte allocation with 15419532 free bytes and 233MB until OOM
at Socket.ClientThread.run(ClientThread.java:130)
02-10 15:22:29.833 32097-32121/com.google.vrtoolkit.cardboard.samples.treasurehunt I/MainActivity﹕ onRendererShutdown
02-10 15:22:32.793 32097-32122/com.google.vrtoolkit.cardboard.samples.treasurehunt I/Process﹕ Sending signal. PID: 32097 SIG: 9
&#13;
答案 0 :(得分:6)
在Android 5上有类似问题。将图像移动到/ drawable-nodpi而不是/ drawable。
编辑:我认为这与使用当前设备DPI自动缩放图像有关。例如,如果您只有/ drawable-mdpi中的大图像,但您的设备是xxxhdpi,那么Android将获取mdpi中可用的图像并尝试将其缩放以匹配xxxhdpi,这可能会占用太多内存。但是/ drawable-nodpi中的图像没有缩放,它们按原样使用。