简体   繁体   中英

Save a bitmap to file

I want to open one image from my assets folder, resize it and resave the image. I use this code:

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

}

But when I start application it crash. This is stack trace:

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)

Does someone know why crash?

The assets folder is located inside the APK, and thus is not a true folder in the File System. I don't think you can save anything there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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