繁体   English   中英

从Android中的drawable / assets文件夹正确加载大图像

[英]Properly loading large image from drawable/assets folder in Android

我从assets / drawable-nopdpi文件夹中获得了一个相当大的图像。 图片大小为1173x1285,497KB。 它没有加载..

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));

方法

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

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

我也尝试过参考资料。 无济于事。我也尝试了inJustBounds true和false的BitmapFactory。 也可以使用inScaled

我似乎无法加载任何图像,我的日志始终返回null。

编辑:

我也在努力

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));

返回Bitmap: null BitmapImage: null

另外,LogCat显示--- decoder->decode returned false

原来,我正在解码的文件已损坏。 我不得不更改原始文件。 谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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