joe doe
joe doe

Reputation: 117

How do I get the Bitmap of the photo taken by calling Intent(MediaStore.ACTION_IMAGE_CAPTURE)?

first I created the unique file path to store photo using date:

File file = new File(Environment.getExternalStoragePublicDirectory( 
  Environment.DIRECTORY_DCIM),"raspberry");
String dateString = new SimpleDateFormat("MM/dd/yyyy_HH:mm:ss").format(new Date());
filePath=new File(file,"SideBySide4_"+dateString+".jpg");

I generated a uri from filePath using FileProvider I have defined in the manifest and xml:

mUri = FileProvider.getUriForFile(this,
            BuildConfig.APPLICATION_ID + ".provider",
            filePath);
mCurrentPhotoPath = "file:" + filePath.getAbsolutePath();//for getting Bitmap in onActivityResult()

Then, I launched the camera Intent and give it the Uri to store the photo taken:

Intent takePictureIntent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mUri);
startActivityForResult(takePictureIntent,REQUEST_TAKE_PHOTO);

Then in onActivityResult(), I try to get the Bitmap:

Uri imageUri=Uri.parse(mCurrentPhotoPath);
            File file=new File(imageUri.getPath());
            try{
                InputStream ims=new FileInputStream(file);
                photoBitmap=BitmapFactory.decodeStream(ims);
            }catch(FileNotFoundException e){
                return;
            }

But photoBitmap is null

Upvotes: 0

Views: 99

Answers (1)

Solution:

You can do something like this:

In your onActivityResult(), After clicking a pic, you get the data in data So:

Bitmap picture = (Bitmap) data.getExtras().get("data");

Can be used.

Hope it's useful.

Upvotes: 1

Related Questions