Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么在重新使用位图时总是出现异常? 背景_Android_Bitmap_Android 3.0 Honeycomb_Reusability - Fatal编程技术网

Android 为什么在重新使用位图时总是出现异常? 背景

Android 为什么在重新使用位图时总是出现异常? 背景,android,bitmap,android-3.0-honeycomb,reusability,Android,Bitmap,Android 3.0 Honeycomb,Reusability,从API 11开始,您可以在解码新位图时重新使用位图,这样解码器就不需要重新创建全新的大型对象 这是使用类似以下代码的方式完成的(摘自): 其优点非常明显:在某些情况下使用更少的内存,对GC的压力更小,并且性能更好,因为您不需要创建更多的大型对象 唯一的问题是两个映像必须具有相同的大小和配置 问题 即使代码在项目本身(在res文件夹中)的资源中工作得很好,但在处理放入内部存储器的图像文件时,我似乎总是会遇到下一个错误: java.lang.IllegalArgumentException: Pr

从API 11开始,您可以在解码新位图时重新使用位图,这样解码器就不需要重新创建全新的大型对象

这是使用类似以下代码的方式完成的(摘自):

其优点非常明显:在某些情况下使用更少的内存,对GC的压力更小,并且性能更好,因为您不需要创建更多的大型对象

唯一的问题是两个映像必须具有相同的大小和配置

问题 即使代码在项目本身(在res文件夹中)的资源中工作得很好,但在处理放入内部存储器的图像文件时,我似乎总是会遇到下一个错误:

java.lang.IllegalArgumentException: Problem decoding into existing bitmap
我尝试了位图选项的多个不同标志:

bitmapOptions.inPurgeable = true;
bitmapOptions.inInputShareable = true;
bitmapOptions.inMutable = true;
bitmapOptions.inScaled = false;
bitmapOptions.inSampleSize = 1;
bitmapOptions.inPreferredConfig = Config.RGB_565; //i've set the created bitmap to be of this type too, of course
我还尝试了BitmapFactory的decodeFile和decodeStream

下面是一个示例代码,说明存在问题(基于我所写的示例):

问题 为什么我不断地得到这个错误,我怎样才能修复它


我发现了一些类似的问题,但没有一个答案。

使用webP文件时,这个很酷的技巧似乎有问题

我只需要使用jpg或png

bitmapOptions.inPurgeable = true;
bitmapOptions.inInputShareable = true;
bitmapOptions.inMutable = true;
bitmapOptions.inScaled = false;
bitmapOptions.inSampleSize = 1;
bitmapOptions.inPreferredConfig = Config.RGB_565; //i've set the created bitmap to be of this type too, of course
@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bitmap_allocation);

    final int[] imageIDs = { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f };

    final CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox);
    final TextView durationTextview = (TextView) findViewById(R.id.loadDuration);
    final ImageView imageview = (ImageView) findViewById(R.id.imageview);

    // Create bitmap to be re-used, based on the size of one of the bitmaps
    mBitmapOptions = new BitmapFactory.Options();
    mBitmapOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.drawable.a, mBitmapOptions);
    mCurrentBitmap = Bitmap.createBitmap(mBitmapOptions.outWidth, mBitmapOptions.outHeight, Bitmap.Config.ARGB_8888);
    mBitmapOptions.inJustDecodeBounds = false;
    mBitmapOptions.inBitmap = mCurrentBitmap;
    mBitmapOptions.inSampleSize = 1;
    mBitmapOptions.inPreferredConfig = Config.ARGB_8888;
    BitmapFactory.decodeResource(getResources(), R.drawable.a, mBitmapOptions);
    imageview.setImageBitmap(mCurrentBitmap);

    // When the user clicks on the image, load the next one in the list
    imageview.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(final View v) {
            mCurrentIndex = (mCurrentIndex + 1) % imageIDs.length;
            Options bitmapOptions = new Options();
            bitmapOptions.inPreferredConfig = Config.ARGB_8888;
            if (checkbox.isChecked()) {
                // Re-use the bitmap by using BitmapOptions.inBitmap
                bitmapOptions = mBitmapOptions;
                bitmapOptions.inBitmap = mCurrentBitmap;
            }
            final long startTime = System.currentTimeMillis();
            //
            File tempFile = null;
            try {
                tempFile = File.createTempFile("temp", ".webp", getApplicationContext().getCacheDir());
                FileOutputStream fileOutputStream;
                final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imageIDs[mCurrentIndex]);
                bitmap.compress(CompressFormat.WEBP, 100, fileOutputStream = new FileOutputStream(tempFile));
                fileOutputStream.flush();
                fileOutputStream.close();
            final InputStream inputStream = new FileInputStream(tempFile);
            mCurrentBitmap = BitmapFactory.decodeStream(inputStream,null,bitmapOptions);
            inputStream.close();
            } catch (final IOException e1) {
                e1.printStackTrace();
            }
            imageview.setImageBitmap(mCurrentBitmap);

            // One way you can see the difference between reusing and not is through the
            // timing reported here. But you can also see a huge impact in the garbage
            // collector if you look at logcat with and without reuse. Avoiding garbage
            // collection when possible, especially for large items like bitmaps,
            // is always a good idea.
            durationTextview.setText("Load took " + (System.currentTimeMillis() - startTime));
        }
    });
}