我的应用程序正在尝试从矩形的外部存储器调整PNG文件的大小:(例如1000 x x 2000 h)
目标是通过在图像的两侧添加空白空间将其变成256 x 256图像,使其现在为2000 x 2000,然后将其缩小到256x256,从而使其成为正方形而不拉伸原始图像。
我以为我可以聪明并使用ImageView
来执行此操作并添加缩放到屏幕上的图像的预览但我正在使用nullpointer崩溃....
我的调整大小方法
private void resizeImage(String fileName){
ImageView tempImageView = new ImageView(this);
tempImageView.setDrawingCacheEnabled(true);
tempImageView.setLayoutParams(new LayoutParams(256, 256));
tempImageView.setScaleType(ScaleType.CENTER_INSIDE);
Bitmap bm = BitmapFactory.decodeFile(getKingNozzleStorageDirectory()+fileName);
tempImageView.setImageBitmap(bm);
rootView.addView(tempImageView);
tempImageView.buildDrawingCache();
Bitmap resized_bm = tempImageView.getDrawingCache();
FileOutputStream outStream;
try {
outStream = new FileOutputStream(getKingNozzleStorageDirectory() + "new"+fileName );
try {
resized_bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我抓住的例外:
06-03 08:43:50.006:W / System.err(3842):java.lang.NullPointerException 06-03 08:43:50.006:W / System.err(3842):at com.eliddell.kingnozzle.activities.KNDrawingActivity.resizeImage(KNDrawingActivity.java:285) 06-03 08:43:50.006:W / System.err(3842):at com.eliddell.kingnozzle.activities.KNDrawingActivity.onClick(KNDrawingActivity.java:223) 06-03 08:43:50.006:W / System.err(3842):at android.view.View.performClick(View.java:4438)06-03 08:43:50.006: W / System.err(3842):at android.view.View $ PerformClick.run(View.java:18422)06-03 08:43:50.006:W / System.err(3842):at android.os.Handler.handleCallback(Handler.java:733)06-03 08:43:50.006:W / System.err(3842):at android.os.Handler.dispatchMessage(Handler.java:95)06-03 08:43:50.006:W / System.err(3842):at android.os.Looper.loop(Looper.java:136)06-03 08:43:50.006: W / System.err(3842):at android.app.ActivityThread.main(ActivityThread.java:5017)06-03 08:43:50.006:W / System.err(3842):at java.lang.reflect.Method.invokeNative(Native Method)06-03 08:43:50.006:W / System.err(3842):at java.lang.reflect.Method.invoke(Method.java:515)06-03 08:43:50.006: W / System.err(3842):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 06-03 08:43:50.006:W / System.err(3842):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)06-03 08:43:50.006:W / System.err(3842):at dalvik.system.NativeStart.main(原生方法)
如果我注释掉以下行
resized_bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
它没有崩溃导致我相信没有创建resize_bm?
Bitmap resized_bm = tempImageView.getDrawingCache();
显然可能有其他方法可以实现我的画布大小调整,所以这些见解当然是受欢迎的......但是有人知道我做错了吗?
我没有看到任何原生方法将画布填充添加到位图的宽度。
答案 0 :(得分:0)
我认为您尚未在此行中创建Bitmap
:
Bitmap resized_bm = tempImageView.getDrawingCache();
您需要使用ImageView进行更多修改。试试这个:
tempImageView.setDrawingCacheEnabled(true);
// resize and create canvas
tempImageView.measure(MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY));
tempImageView.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
tempImageView.buildDrawingCache(true);
final Bitmap resized_bm = tempImageView.getDrawingCache();
tempImageView.setDrawingCacheEnabled(false);
<强>更新强>
为什么不使用标准方法?
final Bitmap bitmap = Bitmap.createBitmap(256, 256, Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
final Bitmap tmp = BitmapFactory.decodeFile(getKingNozzleStorageDirectory() + fileName);
final int width = (int) (1f * tmp.getWidth() / tmp.getHeight() * 256);
final int height = 256;
final Bitmap scaled = Bitmap.createScaledBitmap(tmp, width, height, false);
final int leftOffset = (bitmap.getWidth() - scaled.getWidth()) / 2;
final int topOffset = 0;
canvas.drawBitmap(scaled, leftOffset, topOffset, null);
<强> UPDATE2:强>
如果你想使用ImageView,可能很奇怪,但这段代码对我有用:
final ImageView tempImageView = new ImageView(getActivity());
final Bitmap tmp = BitmapFactory.decodeFile(LayoutParams.WRAP_CONTENT);
tempImageView.setImageBitmap(tmp);
tempImageView.setDrawingCacheEnabled(true);
// resize and create canvas
tempImageView.measure(MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY));
tempImageView.layout(0, 0, tempImageView.getMeasuredWidth(), tempImageView.getMeasuredHeight());
tempImageView.buildDrawingCache(true);
final Bitmap resized_bm = Bitmap.createBitmap(tempImageView.getDrawingCache());
tempImageView.setDrawingCacheEnabled(false);