CODE:
public void startCameraActivity(){
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(cameraIntent, 1000);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == Activity.RESULT_OK) {
if (requestCode == 1000){
File photo = new File(Environment.getExternalStorageDirectory(), ".camera.jpg");
OutputStream out = null;
String imagePath = data.getData().getPath();
Bitmap image = BitmapFactory.decodeFile(imagePath);
ExifInterface exif = null;
try {
exif = new ExifInterface(imagePath);
int exifOrientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int exifDegree = exifOrientationToDegrees(exifOrientation);
image = rotate(image, exifDegree);
photo.createNewFile();
out = new FileOutputStream(Environment.getExternalStorageDirectory());
image.compress(Bitmap.CompressFormat.PNG, 100, out);
out.close();
} catch (IOException e) {
Toast.makeText(getContext(), "hello error", Toast.LENGTH_LONG).show();
}
returnUri = Uri.fromFile(photo);
}
Glide.with(this)
.load(returnUri)
.into(mImageview);
}
}
It's for rotate image
public int exifOrientationToDegrees(int exifOrientation)
{
if(exifOrientation == ExifInterface.ORIENTATION_ROTATE_90)
{return 90;}
else if(exifOrientation == ExifInterface.ORIENTATION_ROTATE_180)
{return 180;}
else if(exifOrientation == ExifInterface.ORIENTATION_ROTATE_270)
{return 270;}
return 0;
}
public Bitmap rotate(Bitmap bitmap, int degrees)
{
if(degrees != 0 && bitmap != null)
{
Matrix m = new Matrix();
m.setRotate(degrees, (float) bitmap.getWidth() / 2,
(float) bitmap.getHeight() / 2);
try
{
Bitmap converted = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), m, true);
if(bitmap != converted)
{
bitmap.recycle();
bitmap = converted;
}
}
catch(OutOfMemoryError ex)
{
}
}
return bitmap;
}
I made a code that get picture from camera.
But that picture which has came from camera has incorrect direction.
So i searched to rotate that image.
but above code do not works.
Where is problem? How can i fix it?
Android monitor says
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=66536, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/8464 flg=0x1 (has extras) }} to activity {~~!@.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
EDIT full logcat
11-03 11:21:42.060 16104-16104/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.keepair.www.pinair, PID: 16104
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=66536, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/8465 flg=0x1 (has extras) }} to activity {com.keepair.www.pinair/com.keepair.www.pinair.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:4173)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4216)
at android.app.ActivityThread.access$1400(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6117)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
at com.keepair.www.pinair.GreenFragment.onActivityResult(GreenFragment.java:296)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:165)
at android.app.Activity.dispatchActivityResult(Activity.java:6632)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4169)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4216)
at android.app.ActivityThread.access$1400(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6117)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)