简体   繁体   English

在边界内解码图像后,在解码具有样本大小的图像时出现内存不足错误

[英]out of memory error while decoding the image with sample size after decoding it within bounds

I am beginner in android. 我是android的初学者。 While decoding the images ( of size 1600x1200 ) from SD card one by one i am getting the below error. 在从SD卡逐个解码图像(大小为1600x1200)时,我得到以下错误。 After decoding the bitmap i have to apply the animation for the imageview to play the image like slide show in full screen. 解码位图后,我必须为imageview应用动画,以全屏播放幻灯片放映等图像。 i am using the below calculation for getting samplesize after decoding the bitmap in bounds. 我在解码边界中的位图后使用以下计算来获取samplesize。

while (true) {
    if (width_tmp / 2 < REQUIRED_WIDTH || height_tmp / 2 < REQUIRED_HEIGHT )
        break;
    width_tmp /= 2;
    height_tmp /= 2;
    scale *= 2;
}

Can anyone please help me to solve this issue. 任何人都可以帮我解决这个问题。

Error:
11-09 13:39:15.100: E/DhcpStateMachine(424): DHCP failed on wlan0: Timed out waiting for DHCP to finish
11-09 13:39:15.300: E/WifiStateMachine(424): IP configuration failed
11-09 13:39:32.840: E/dalvikvm-heap(2511): Out of memory on a 20155408-byte allocation.
11-09 13:39:32.870: E/AndroidRuntime(2511): FATAL EXCEPTION: AsyncTask #1
11-09 13:39:32.870: E/AndroidRuntime(2511): java.lang.RuntimeException: An error occured while executing doInBackground()
11-09 13:39:32.870: E/AndroidRuntime(2511):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
11-09 13:39:32.870: E/AndroidRuntime(2511):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
11-09 13:39:32.870: E/AndroidRuntime(2511):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-09 13:39:32.870: E/AndroidRuntime(2511):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
11-09 13:39:32.870: E/AndroidRuntime(2511):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-09 13:39:32.870: E/AndroidRuntime(2511):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-09 13:39:32.870: E/AndroidRuntime(2511):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-09 13:39:32.870: E/AndroidRuntime(2511):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-09 13:39:32.870: E/AndroidRuntime(2511):     at java.lang.Thread.run(Thread.java:856)
11-09 13:39:32.870: E/AndroidRuntime(2511): Caused by: java.lang.OutOfMemoryError
11-09 13:39:32.870: E/AndroidRuntime(2511):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
11-09 13:39:32.870: E/AndroidRuntime(2511):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:527)
11-09 13:39:32.870: E/AndroidRuntime(2511):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:301)
11-09 13:39:32.870: E/AndroidRuntime(2511):     at com.example.testproject1.Imgloader.loadImageFromSDCard(Imgloader.java:60)
11-09 13:39:32.870: E/AndroidRuntime(2511):     at com.example.testproject1.Imgloader.access$0(Imgloader.java:50)
11-09 13:39:32.870: E/AndroidRuntime(2511):     at com.example.testproject1.Imgloader$SDLoadImageTask.doInBackground(Imgloader.java:177)
11-09 13:39:32.870: E/AndroidRuntime(2511):     at com.example.testproject1.Imgloader$SDLoadImageTask.doInBackground(Imgloader.java:1)
11-09 13:39:32.870: E/AndroidRuntime(2511):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-09 13:39:32.870: E/AndroidRuntime(2511):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-09 13:39:32.870: E/AndroidRuntime(2511):     ... 5 more
11-09 13:39:32.970: E/dalvikvm-heap(2511): Out of memory on a 20155408-byte allocation.
11-09 13:39:46.530: E/DhcpStateMachine(424): DHCP failed on wlan0: Timed out waiting for DHCP to finish
11-09 13:39:46.749: E/WifiStateMachine(424): IP configuration failed
11-09 13:40:18.180: E/DhcpStateMachine(424): DHCP failed on wlan0: Timed out waiting for DHCP to finish
11-09 13:40:18.399: E/WifiStateMachine(424): IP configuration failed
11-09 13:40:18.399: E/WifiStateMachine(424): Failed 10 times, Disabling 3

I used the following method to scale an image based on an empirically determined maximum memory size max_size which in my purposes is 2,000,000. 我使用以下方法根据经验确定的最大内存大小max_size来缩放图像, max_size在我的目的是2,000,000。

private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {

    InputStream inputStream = getContentResolver().openInputStream(
            selectedImage);
    UriHelper uriHelper = new UriHelper(this);
    mSaveFilenameRoot = uriHelper.getFilenameRoot(selectedImage);

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(inputStream, null, o);
    try {
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Find the correct scale value. A power of 2 is best (fastest)
    // however using the scaling that maximizes the size of the image.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;

    // maximum size due to heap size limitations
            // Bytes required based on width x height x 4 bytes per pixel
    int max_size = 2000000;
    while (true) {
        if (((width_tmp / scale) * (height_tmp / scale) * 4) < max_size)
            break;
        scale++;
    }

    // Decode the image at the appropriate scale
    inputStream = getContentResolver().openInputStream(selectedImage);
    o = new BitmapFactory.Options();
    o.inSampleSize = scale;
    o.inPreferredConfig = Bitmap.Config.RGB_565;
    Bitmap tmpBitmap = BitmapFactory.decodeStream(inputStream, null, o);
    try {
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return tmpBitmap;
}

Since you havent mentioned the SDK version that you can targeting, the best performance can be obtained through Render Script 由于您没有提到可以定位的SDK版本,因此可以通过渲染脚本获得最佳性能

A sample of its usage can also be found at http://developer.android.com/guide/topics/renderscript/compute.html 其用法示例也可以在http://developer.android.com/guide/topics/renderscript/compute.html上找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM