0

我知道这是重复的,但我无法解决问题。
在我的 Android 应用程序中,我从一个 url 下载了一个图像,它因内存不足异常而崩溃。我在下载功能行
收到错误。Bitmap.createScaledBitmap

一切都在 doInBackground 中。

    private Bitmap downloadImage(String url) {
        Bitmap bitmap = null;
        InputStream stream = null;
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inSampleSize = 1;

        try {
            stream = getHttpConnection(url);
            bitmap = BitmapFactory.decodeStream(stream, null, bmOptions);

//          add on 12/02/2014
            bitmap = Bitmap.createScaledBitmap( // Out of memory exception
                    bitmap, (int) (bitmap.getWidth() * 0.8),
                    (int) (bitmap.getHeight() * 0.8), true);

//          Adding given image bitmap to byte array for edit spot.
            ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream1);
            byte[] array = stream1.toByteArray();
            AddNewSpotValues.comm_2_picture_path = array;           

            stream.close();
        } catch (IOException e1) {
            Log.e("image download problem IO", e1.getMessage());
            e1.printStackTrace();
        }
        return bitmap;
    }




// Makes HttpURLConnection and returns InputStream
private InputStream getHttpConnection(String urlString)
        throws IOException {
    InputStream stream = null;
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();

    try {
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        httpConnection.setRequestMethod("GET");
        httpConnection.connect();

        if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            stream = httpConnection.getInputStream();
        }
    } catch (Exception ex) {
        Log.e("image download problem", ex.getMessage());
        ex.printStackTrace();
    }
    return stream;
}

日志猫

at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:718)
at android.graphics.Bitmap.createBitmap(Bitmap.java:695)
at android.graphics.Bitmap.createBitmap(Bitmap.java:628)
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:508)
at com.fssd.spot.setting.MyAccount_MySpot.downloadImage(MyAccount_MySpot.java:353)
at com.fssd.spot.setting.MyAccount_MySpot.access$6(MyAccount_MySpot.java:336)
at com.fssd.spot.setting.MyAccount_MySpot$GetDetailOfSelectedSPOT.doInBackground(MyAccount_MySpot.java:315)
at com.fssd.spot.setting.MyAccount_MySpot$GetDetailOfSelectedSPOT.doInBackground(MyAccount_MySpot.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
... 4 more

请帮我。

4

2 回答 2

0

您应该考虑对图像进行下采样,如此此处所示。

此外,您应该考虑先将其下载到文件中,而不是直接从 Internet 解码位图。

您需要记住,android 可以在很多类型的设备上运行,因此并非所有设备都可以处理大图像。

每个应用程序的最小 MB 仍然是 16MB(写在这里),因此如果图像的大小为 WIDTH*HEIGHT,则在解码后通常需要 4*WIDTH*HEIGHT 字节。您可以通过设置位图的类型或使图像的分辨率更小来使其更小。

一种好的方法是将图像下采样到屏幕大小,因为设备应该始终能够显示全屏图像。如果图像没有透明像素并且您不太关心质量,则可以使用 RGB_565 配置而不是默认的ARGB_8888配置,这将使位图仅使用 2*WIDTH*HEIGHT 字节。

顺便说一句,使用 createScaledBitmap 会在您已有的位图之外创建一个新的位图,因此只有在别无选择时才应使用它。如果你想改变已经解码的位图的大小,你可以使用我的小库,它使用 NDK&JNI,在这里

于 2014-02-14T14:34:43.863 回答
0

你应该使用ImageLoader 类

在此您可以通过以下方式访问您的图片网址

imageLoader.displayImage(imageurl, imageView, options);
于 2014-02-14T14:38:00.703 回答