-2

After capture camera image when i save then not return in activity and crash app but everything is ok in my Samsung mobile but giving this error in redmi phone and others mobile

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
         at com.logiclump.technologies.gigmedico.Home.onActivityResult(Home.java:101)
         at android.app.Activity.dispatchActivityResult(Activity.java:6562)
         at android.app.ActivityThread.deliverResults(ActivityThread.java:3768)

This is my first activity

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST_CODE && resultCode==RESULT_OK)

    {
        Uri uri = data.getData();
        Log.e("Image : ", uri.toString());
        Intent intent = new Intent(this, PictureActivity.class);
        intent.putExtra("imgUrl", uri.toString());
        startActivity(intent);
    }
}

This is second activity where i am getting intent

Bundle bundle = getIntent().getExtras();
if (bundle != null) {
    Log.e("ashish", bundle.getString("imgUrl") + "");
    path = Uri.parse(bundle.getString("imgUrl"));
}

ImageView selfiiii = (ImageView) findViewById(R.id.mySelfie);
selfiiii.setImageURI(path);
yatin deokar
  • 730
  • 11
  • 20
mohd irfan
  • 57
  • 9

1 Answers1

0

Try this

Camera Intent:

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File

        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.example.android.fileprovider",
                    photoFile);

            mTempCameraPhotoFile = photoFile;
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, PHOTO_REQUEST);
        }
    }
}

Save Image File In Storage:

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

In onActivityResult:

if (requestCode == PHOTO_REQUEST && resultCode == RESULT_OK) {
        try {

            String filePath = mTempCameraPhotoFile.getPath();

            Uri uri = Uri.fromFile(mTempCameraPhotoFile);

            Bitmap bitmap = null;

            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
yatin deokar
  • 730
  • 11
  • 20