Android内存不足异常-下载图像作为位图

Android内存不足异常-下载图像作为位图,android,android-imageview,android-memory,android-bitmap,Android,Android Imageview,Android Memory,Android Bitmap,我知道这是重复的,但我无法解决问题。 在我的Android应用程序中,我从一个url下载了一个图像,它会因内存不足而崩溃。 我在下载函数的Bitmap.createScaledBitmap行中得到错误 一切都在幕后进行 private Bitmap downloadImage(String url) { Bitmap bitmap = null; InputStream stream = null; BitmapFactory.Option

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

一切都在幕后进行

    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;
}
Logcat

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

请帮助我。

< P>你应该考虑下采样图像,如所示和

也不是直接从Internet上解码位图,而是应该先下载到一个文件。

你需要记住,android在很多类型的设备上运行,所以并不是所有设备都能处理大型图像

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

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

顺便说一句,使用createScaledBitmap将在现有位图的基础上创建一个新位图,因此只有在没有选择的情况下才应该使用它。如果您希望更改已解码位图的大小,可以使用我的小型库,该库使用NDK和JNI进行更改。

您应该使用

在这里,您可以通过以下方式访问图像url:

imageLoader.displayImage(imageurl, imageView, options);

使用通用ImageLoader类。