BitmapFactory.decodeStream out-of-memory-trotz des reduzierten stichprobenumfangs

Ich habe gelesen, dass viele related posts bezüglich Speicherzuordnung Probleme mit der Decodierung von bitmaps, aber bin immer noch nicht finden die Lösung für das folgende problem auch nach der Verwendung der zur Verfügung gestellte code in der offiziellen website.

Hier ist mein code:

public static Bitmap decodeSampledBitmapFromResource(InputStream inputStream, int reqWidth, int reqHeight) {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len;
    try {
        while ((len = inputStream.read(buffer)) > -1) {
        baos.write(buffer, 0, len);
        }
        baos.flush();
        InputStream is1 = new ByteArrayInputStream(baos.toByteArray());
        InputStream is2 = new ByteArrayInputStream(baos.toByteArray());

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is1, null, options);

        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        options.inPurgeable = true;
        options.inInputShareable = true;
        options.inJustDecodeBounds = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        return BitmapFactory.decodeStream(is2, null, options);

    } catch (Exception e) {
        e.printStackTrace();

        return null;
    }
}

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    //Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        //Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        //Choose the smallest ratio as inSampleSize value, this will guarantee
        //a final image with both dimensions larger than or equal to the
        //requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }


    return inSampleSize;
}

bitmap = decodeSampledBitmapFromResource(inputStream, 600, 600); 

Bin ich immer "Out of memory" - Fehlermeldung auf einem 3250016 - byte-Zuordnung" in dieser Zeile:

return BitmapFactory.decodeStream(is2, null, options);

Es scheint mir, dass 3,2 MB ist klein genug, um zugeordnet werden. Wohin gehe ich falsch? Wie kann ich dieses Problem lösen?

BEARBEITEN

Nach einem Blick in diese Lösung HIER von N-Joy, es funktioniert gut mit der Benötigten Größe 300, aber meine gewünschte Größe ist 800, so bin ich noch immer der Fehler.

InformationsquelleAutor der Frage Goofy | 2013-03-06

Schreibe einen Kommentar