I am trying to take a photo and to save it to a custom location-
public void SavePhoto(View view){
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "WorkingWithPhotosApp");
imagesFolder.mkdirs();
File image = new File(imagesFolder, "QR_" + timeStamp + ".png");
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(imageIntent, REQUEST_IMAGE_CAPTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(data==null){
Toast.makeText(MainActivity.this, "Data is null", Toast.LENGTH_SHORT).show();
}
else{
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
ImageView mImageView=(ImageView)findViewById(R.id.imageView);
mImageView.setImageBitmap(imageBitmap);
}
}
}
data is null in onActivityResult(). What did I miss?
Try like this :
Your preinsert a uri here:
So when you get a
Activity.RESULT_OK
just load the taken photo by its known url. Then you can set the pathonActivityResult
like below but you need to convert in to Bitmap.I had the same problem , like you, so I decided implement the solution given here
Then, in my own project, I did that
First : Create the provider in the manifest, the same that the guide says.
Second : Create a class name LegacyCompatCursorWrapper (ps. see the guide or contact with me , I can send the code), you need this class ,in the follow step.
Third : Create this class
LegacyCompatFileProvider
Fourth: Before to do that
Implement this
So in onActivityResult()
Where
mImageProfile
--> is an ImageView on the View. Remember, that is according the guide.I think you need to check first either your directory exists or not. This is how I performed this task. In my case I am creating a folder in default
DCIM
directory.Hope this helps!