I am developing a camera application,my problem is that ,On some devices the images taken by the camera is rotating, when setting it to an imageview ,So that i have searched for a solution ,and i found a solution from this link.camera intent auto rotate to 90 degree. My problem is when i call the method to generate rotated bitmap image ,it shows a NPE on the following line
Bitmap rotatedBitmap = Bitmap.createBitmap( bitencoded, 0, 0, bitencoded.getWidth(),
bitencoded.getHeight(), matrix, true);
Full code
private Bitmap rotateCam()
{
Matrix matrix = new Matrix();
matrix.postRotate(getImageOrientation(picturePath));
Bitmap rotatedBitmap = Bitmap.createBitmap( bitencoded, 0, 0, bitencoded.getWidth(),
bitencoded.getHeight(), matrix, true);
imageView.setImageBitmap(rotatedBitmap);
return rotatedBitmap;
}
LogCat
java.lang.NullPointerException
at project1.me.com.update.ImageUploadActivity.rotateCam(ImageUploadActivity.java:235)
at project1.me.com.update.ImageUploadActivity.onCreate(ImageUploadActivity.java:182)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
at android.app.ActivityThread.access$700(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
at dalvik.system.NativeStart.main(Native Method)
onCreate
byte[] data = picturePath.getBytes("UTF-8");
String base64PicPath = Base64.encodeToString(data, Base64.DEFAULT);
bitencoded= stringToBitMap(base64PicPath);
try
{
rotateCam();
Log.e(" Rotated", "Method called");
}
catch(Exception e)
{
Log.e("Not Rotated","Method not called");
e.printStackTrace();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
getImageOrientation()
public static int getImageOrientation(String imagePath){
int rotate = 0;
try {
File imageFile = new File(imagePath);
ExifInterface exif = new ExifInterface(
imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return rotate;
}
I am a beginner ,i could not find reason for NPE.Can anybode help me out?.Thanx in advance.