简体   繁体   中英

Properly loading large image from drawable/assets folder in Android

I have a fairly large image from assets/drawable-nopdpi folder. Image size is 1173x1285, 497KB. It doesn't load..

onCreate

    AssetManager assetManager = this.getAssets();
    Bitmap bitmap = null;
    InputStream is = null;
    try {
        is = assetManager.open("indoormapimg.png");
        bitmap = decodeSampledBitmapFromResource(is, 100, 100); //trying 100x100
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



    Log.d("tag", String.valueOf("Bitmap: " + bitmap));

Methods

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

    if (height > reqHeight || width > reqWidth)
        if (width > height)
            inSampleSize = Math.round((float)height / (float)reqHeight);
        else
            inSampleSize = Math.round((float)width / (float)reqWidth);

    return inSampleSize;
}

and

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

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(is, null, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeStream(is, null, options);
}

I've also tried with Resources. No avail.. I've also tried BitmapFactory with inJustBounds both true and false. Also with inScaled

I can't seem to load any image, my Log always returns null..

EDIT:

I'm also trying

Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), ResID);

        BitmapFactory.Options  opts = new BitmapFactory.Options();
        opts.inDither = true;
        opts.inPreferredConfig = Bitmap.Config.RGB_565;
        opts.inScaled = false;


        Bitmap bitmapImage = BitmapFactory.decodeResource(this.getResources(), ResID, opts); 

        Log.d("tag", String.valueOf("Bitmap: " + bitmap + " BitmapImage: " + bitmapImage));

Returns Bitmap: null BitmapImage: null

Also, LogCat shows --- decoder->decode returned false

It turns out, the file I was decoding was corrupted. I had to change the raw file. Thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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