0

我有一个来自 assets/drawable-nopdpi 文件夹的相当大的图像。图像大小为 1173x1285、497KB。加载不出来。。

创建

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

我也尝试过使用资源。无济于事..我也尝试过BitmapFactory的inJustBounds真假。还与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

4

1 回答 1

0

事实证明,我正在解码的文件已损坏。我不得不更改原始文件。谢谢你。

于 2013-11-11T06:55:44.907 回答