i am developing an app in which there is a button which is used to pick image either from gallery or capture image from camera. When i pick an image from gallery there is no error image shows up fine in the end but when i capture image from my camera app crashes.
Following is the error:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=66, result=-1, data=null} to activity
{com.byteshaft.prospectform/com.byteshaft.prospectform.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:4297)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4347)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1557)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:173)
at android.app.ActivityThread.main(ActivityThread.java:6459)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:938)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference
at com.byteshaft.prospectform.MainActivity.onActivityResult(MainActivity.java:400)
at android.app.Activity.dispatchActivityResult(Activity.java:6926)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4293)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4347)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1557)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:173)
at android.app.ActivityThread.main(ActivityThread.java:6459)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:938)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)
My code for picking image from gallery/camera:
public void openImageIntent() {
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String fname = "Prospect-form" + timeStamp;
final File sdImageMainDirectory = new File(storageDir, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
final List<Intent> cameraIntents = new ArrayList<>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for(ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
//Gallery.
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Filesystem.
final Intent fsIntent = new Intent();
fsIntent.setType("*/*");
fsIntent.setAction(Intent.ACTION_GET_CONTENT);
cameraIntents.add(fsIntent);
//Create the Chooser
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
startActivityForResult(chooserIntent, 66);
}
onActivityResult code:`
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == Activity.RESULT_OK)
switch (requestCode){
case 5:
Uri selectedImage = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(MainActivity.this.getContentResolver(), selectedImage);
Bitmap resizedBitMap;
resizedBitMap = Bitmap.createScaledBitmap(bitmap, 100, 100, true);
imageView.setImageBitmap(resizedBitMap);
System.out.println(Helpers.getPath(MainActivity.this, selectedImage));
imagePath = Helpers.getPath(MainActivity.this, selectedImage);
} catch (IOException e) {
Log.i("TAG", "Some exception " + e);
}
break;
}
}`
There are some very useful answers on stackoverflow but i am unable to solve my problem. Only the error comes when i pick capture an image from camera otherwise there is no error at all when i choose image from gallery.
Any suggestions?