【问题标题】:Properly loading large image from drawable/assets folder in Android从 Android 中的 drawable/assets 文件夹正确加载大图像
【发布时间】:2013-11-11 05:34:24
【问题描述】:

我有一个来自 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 的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

【问题讨论】:

  • 你有没有从 assets 中获取 inputstrem..
  • drawable-nodpi 文件夹进入 res 文件夹。但既然你在 assets 文件夹中有这个,你可以尝试将 assetManager.open("indoormapimg.png"); 更改为 assetManager.open("drawable-nodpi/indoormapimg.png"); 看看是否有效
  • 是的,我知道 drawable-nodpi 文件夹进入 res 文件夹,即使它在那里,BitmapFactory.decodeResource(getResources(), ResID) 返回 null。即使有inJustBoundsinScaled 选项。
  • @useletters 你的问题有解决方案吗??
  • 我的问题还没有解决方案。 BitmapFactory.decodeResource(getResources(), ResID) 始终返回 null,即使您可以在 res/drawable-nodpi/ 上实际看到文件

标签: android bitmap bitmapfactory


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    • 2022-01-21
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    相关资源
    最近更新 更多