Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android capture photo putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photoFile)); data is null

 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 = Environment.getExternalStoragePublicDirectory(
            "MyImages");
    storageDir.mkdirs();
  //  File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
    File image = File.createTempFile(
        imageFileName,  /* prefix */
        ".jpg",         /* suffix */
        storageDir      /* directory */
    );

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

static final int REQUEST_TAKE_PHOTO = 1;

private void dispatchTakePictureIntent() {
     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
         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) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photoFile));
            //takePictureIntent = getIntent().putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {  
        if(data != null) {
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(photo);
        }
    }  
}

main.xml

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.59"
    android:text="File Uri" />


<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_weight="0.52"
    android:maxHeight="@dimen/max_image_height"
    android:src="@drawable/ic_launcher" />

Hi, I want to take photo and save to file "MyImages" which is I create, and i want to view the photo imageview. I can take photo and save it but i cant view with imageview. putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photoFile)); data is null because of Uri.fromFile(photoFile). Please Help!

like image 906
Merve KAYA Avatar asked Nov 10 '22 18:11

Merve KAYA


1 Answers

If you specified MediaStore.EXTRA_OUTPUT, the image taken will be written to that path, and no data will given to onActivityResult. You can read the image from what you specified.

See another solved same question here: Android Camera : data intent returns null

like image 177
Deeptimay Routray Avatar answered Nov 15 '22 06:11

Deeptimay Routray