调用Picture.writeToStream()时,Android 4.3中的Native Crash



Android 4.3中引入了回归。以前在Android版本中运行的代码现在会导致本机崩溃,从而关闭进程。

当将大于32kb的图像绘制到由Picture对象记录的画布中时,会发生崩溃,该对象又通过writeToStream()写入流。

当试图写掉一个字符串(我认为这是图像对象的Uri)时,在Skia中发生了崩溃。

I/DEBUG(122):     #00  pc 0001e3bc  /system/lib/libc.so (strlen+72)    
I/DEBUG(122):     #01  pc 000d9858  /system/lib/libskia.so (SkWriter32::writeString(char const*, unsigned int)+256)    
I/DEBUG(122):     #02  pc 00113d68  /system/lib/libskia.so (SkImageRef_ashmem::flatten(SkFlattenableWriteBuffer&) const+44)

下面的程序展示了如何重现这个问题。所需要的只是一个带有id为"button"的按钮的布局。

    public class MainActivity extends Activity {
    static final String IMAGE_FILE = Environment.getExternalStorageDirectory() + "/test.jpg";
    static final String SKIA_FILE = Environment.getExternalStorageDirectory() + "/test.skia";
    private static Bitmap loadBitmap(final String filename) {
        Bitmap bitmap = null;
        FileInputStream is;
        try {
            is = new FileInputStream(filename);
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inInputShareable = true;
            options.inPurgeable = true;
            bitmap = BitmapFactory.decodeFileDescriptor(is.getFD(), null, options);
            is.close();
        } catch (final FileNotFoundException e) {
            e.printStackTrace();
        } catch (final IOException ex) {
            ex.printStackTrace();
        }
        return bitmap;
    }
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(final View v) {
                final Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        // Create a Canvas and begin recording
                        final Picture picture = new Picture();
                        final Canvas canvas = picture.beginRecording(1024, 1024);
                        // De-compress an image from file
                        final Bitmap bitmap = loadBitmap(IMAGE_FILE);
                        // If present draw the image to the canvas and end
                        // recording
                        if (bitmap != null) {
                            canvas.drawBitmap(bitmap, new Matrix(), null);
                        }
                        picture.endRecording();
                        // Write out the Picture object to a Skia File.
                        FileOutputStream os;
                        try {
                            os = new FileOutputStream(SKIA_FILE);
                            picture.writeToStream(os);
                            os.close();
                        } catch (final FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (final IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                };
                new Thread(runnable).start();
            }
        });
    }
}

设置BitmapFactory的两行。需要选项才能获得Skia平坦化代码来写入图像数据(否则会发射图像)。

options.inInputShareable = true;
options.inPurgeable = true;

我知道图片方法writeToStream()createFromStream()已经被弃用,但我不希望这会带来稳定性问题。

我需要把图片对象写下来,因为我想把它从主应用程序传递到服务进程。由于以下原因,我无法使用文档中建议的"将图片绘制成位图"的解决方法:

  1. 在撰写本文时,尚不清楚图片的所需分辨率
  2. 图片对象在恢复后需要通过矩阵进行放大
  3. 保存到非常高分辨率的位图在内存和处理时间方面效率低下

有人知道一种变通方法吗?它可以将图像写入流中,而不会导致崩溃?

我本想把它作为注释添加,但我缺乏声誉。。。

Skia的突破性变化似乎是SkImageRef_ashmem.cpp:的变化

https://code.google.com/p/skia/source/detail?r=4980

flat方法用于检查空URI,如果URI为空,则会将0写入输出流。将null传递给SkFlattenableWriteBuffer::writeString()会导致strlen()崩溃。

最新更新