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

ImageLoader クラスを使用する必要があります

これで、画像のURLにアクセスできます

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

ここここに示すように、画像のダウンサンプリングを検討する必要があります。

また、ビットマップをインターネットから直接デコードする代わりに、最初にファイルにダウンロードすることを検討する必要があります。

Android は多くの種類のデバイスで実行されるため、すべてのデバイスが大きな画像を処理できるわけではないことを覚えておく必要があります。

アプリあたりの最小限の MB はまだ 16MB (ここに書かれています) であるため、画像のサイズが WIDTH*HEIGHT の場合、デコード後は通常 4*WIDTH*HEIGHT バイトになります。ビットマップのタイプを設定するか、画像の解像度を小さくすることで、小さくすることができます。

良い方法の 1 つは、画像を画面のサイズにダウンサンプリングすることです。これは、デバイスが常に全画面画像を表示できる必要があるためです。画像に透明なピクセルがなく、品質をあまり気にしない場合は、デフォルトのARGB_8888 config の代わりに RGB_565 config を使用できます。これにより、ビットマップは 2*WIDTH*HEIGHT バイトのみを使用します。

ところで、 createScaledBitmap を使用すると、既存のビットマップに加えて新しいビットマップが作成されるため、選択の余地がない場合にのみ使用してください。既にデコードされたビットマップのサイズを変更したい場合は、NDK と JNI を使用してそれを行う私の小さなライブラリを使用できます

于 2014-02-14T14:34:43.863 に答える