我正在学习如何在android中旋转位图,但是我在获取位图的宽度和高度时遇到问题..如何解决?
这是我的代码;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
bitmap = BitmapFactory.decodeFile(filePath, o2);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
if(degree!=0){
Matrix matrix = new Matrix();
matrix.postRotate(degree);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
}
我在 int width = bitmap.getWidth()时遇到错误; int height = bitmap.getHeight();
这是我的logcat
10-25 11:02:51.689: W/dalvikvm(22013): threadid=1: thread exiting with uncaught exception (group=0x40bd7438)
10-25 11:02:51.699: E/AndroidRuntime(22013): FATAL EXCEPTION: main
10-25 11:02:51.699: E/AndroidRuntime(22013): java.lang.NullPointerException
10-25 11:02:51.699: E/AndroidRuntime(22013): at com.mozaik.tangsel.uploadimage.UploadActivity.decodeFile(UploadActivity.java:580)
10-25 11:02:51.699: E/AndroidRuntime(22013): at com.mozaik.tangsel.uploadimage.UploadActivity$5.onClick(UploadActivity.java:216)
10-25 11:02:51.699: E/AndroidRuntime(22013): at android.view.View.performClick(View.java:4084)
10-25 11:02:51.699: E/AndroidRuntime(22013): at android.view.View$PerformClick.run(View.java:16987)
10-25 11:02:51.699: E/AndroidRuntime(22013): at android.os.Handler.handleCallback(Handler.java:615)
10-25 11:02:51.699: E/AndroidRuntime(22013): at android.os.Handler.dispatchMessage(Handler.java:92)
10-25 11:02:51.699: E/AndroidRuntime(22013): at android.os.Looper.loop(Looper.java:137)
10-25 11:02:51.699: E/AndroidRuntime(22013): at android.app.ActivityThread.main(ActivityThread.java:4794)
10-25 11:02:51.699: E/AndroidRuntime(22013): at java.lang.reflect.Method.invokeNative(Native Method)
10-25 11:02:51.699: E/AndroidRuntime(22013): at java.lang.reflect.Method.invoke(Method.java:511)
10-25 11:02:51.699: E/AndroidRuntime(22013): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
10-25 11:02:51.699: E/AndroidRuntime(22013): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
10-25 11:02:51.699: E/AndroidRuntime(22013): at dalvik.system.NativeStart.main(Native Method)
谢谢,对不起我的英文
答案 0 :(得分:1)
我认为您必须在设置insamplesize
之前在JustDecodeBounds中设置选项BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(selectedImagePath, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;
if(imageWidth > imageHeight){
options.inSampleSize = calculateInSampleSize(options,512,256);//<------the size you need if landscape
}else{
options.inSampleSize = calculateInSampleSize(options,256,512);//<------the size you need if portrait
}
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(selectedImagePath,options);
答案 1 :(得分:1)
问题是您的filePath
未指向正确的文件。也就是说,filePath
处没有图像。检查filePath
的值是否正确,这应该得到解决。