Reputation: 575
i'm learning how to rotate bitmap in android, but i have problem when get width and height of bitmap.. how to fix it ?
this my code;
// 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);
}
i get error at int width = bitmap.getWidth(); int height = bitmap.getHeight();
this my 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)
thanks, sorry for my english
Upvotes: 0
Views: 2340
Reputation: 7708
The problem is that your filePath
does not point to a correct file. That is, there is no image at filePath
. Check the value of filePath
is correct and this should get resolved.
Upvotes: 1
Reputation: 7114
i think you have to set your options inJustDecodeBounds before you can set insamplesize
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);
Upvotes: 1