0
我想從我的資產文件夾中打開一個圖像,調整其大小並重新保存圖像。我使用此代碼:將位圖保存到文件
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)
有人知道爲什麼崩潰嗎?
只是一個快速的想法......如果你用最後一行代替這個,會發生什麼:final Bitmap newBitmap = Bitmap.createBitmap(bitmapOrg,0,bitmapOrg.getWidth(),bitmapOrg.getHeight(),aMatrix,false ); – dbryson
仍然有相同的堆棧跟蹤 –