繁体   English   中英

将位图保存到文件

[英]Save a bitmap to file

我想从我的资源文件夹中打开一个图像,调整其大小并重新保存图像。 我用这个代码:

private void resizeImage(float ratio) {
    AssetManager assetManager = getAssets();
    InputStream stream = null;
    try {
        stream = assetManager.open("bear.png");
    } catch (IOException e) {
        return;
    }
    Bitmap bitmapOrg = BitmapFactory.decodeStream(stream);

    int width = bitmapOrg.getWidth();
    int height = bitmapOrg.getHeight();

    float scaleWidth = ((float) width) / ratio;
    float scaleHeight = ((float) height) / ratio;

    Matrix aMatrix = new Matrix();
    aMatrix.setSkew(scaleWidth, scaleHeight);

    bitmapOrg = Bitmap.createBitmap(bitmapOrg, 0, 0, bitmapOrg.getWidth(),
            bitmapOrg.getHeight(), aMatrix, false);

}

但是当我启动应用程序时它会崩溃。 这是堆栈跟踪:

12-09 02:36:33.750: ERROR/AndroidRuntime(1939):     at android.graphics.Bitmap.nativeCreate(Native Method)
12-09 02:36:33.750: ERROR/AndroidRuntime(1939):     at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
12-09 02:36:33.750: ERROR/AndroidRuntime(1939):     at android.graphics.Bitmap.createBitmap(Bitmap.java:444)

有人知道为什么会崩溃吗?

assets文件夹位于APK内部,因此不是文件系统中的真实文件夹。 我认为你不能在那里保存任何东西。

暂无
暂无

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

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