I am using camera to capture image and set the returned bitmap into an imageview but it is crashing on OnePlus devices. I checked on few htc devices and it is working perfectly. I checked for solutions and they said to check for request code but I am already doing it. What can be the problem here?
Here is the code
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1);
}
});
and onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == -1) {
image = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Must compress the Image to reduce image size to make upload easy
image.compress(Bitmap.CompressFormat.PNG, 50, stream);
byte[] byte_arr = stream.toByteArray();
// Encode Image to String
encodedString = Base64.encodeToString(byte_arr, 0);
Uri selectedImageUri = data.getData();
img_path = getRealPathFromURI(selectedImageUri);
// Uri tempUri = getImageUri(getApplicationContext(), image);
imageView.setImageBitmap(image);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
}
}
and the stack trace of error
STACK_TRACE=java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.madhours/com.madhours.activities.ActivitySignUp}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getPath()' on a null object reference at android.app.ActivityThread.deliverResults(ActivityThread.java:3659) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3702) at android.app.ActivityThread.access$1300(ActivityThread.java:155) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) 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:905) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getPath()' on a null object reference at com.madhours.activities.ActivitySignUp.getRealPathFromURI(ActivitySignUp.java:134) at com.madhours.activities.ActivitySignUp.onActivityResult(ActivitySignUp.java:116) at android.app.Activity.dispatchActivityResult(Activity.java:6218) at android.app.ActivityThread.deliverResults(ActivityThread.java:3655) ... 10 more java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getPath()' on a null object reference at com.madhours.activities.ActivitySignUp.getRealPathFromURI(ActivitySignUp.java:134) at com.madhours.activities.ActivitySignUp.onActivityResult(ActivitySignUp.java:116) at android.app.Activity.dispatchActivityResult(Activity.java:6218) at android.app.ActivityThread.deliverResults(ActivityThread.java:3655) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3702) at android.app.ActivityThread.access$1300(ActivityThread.java:155) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) 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:905) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)