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?
Your preinsert a uri here:
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
So when you get a Activity.RESULT_OK
just load the taken photo by its known url. Then you can set the path onActivityResult
like below but you need to convert in to Bitmap.
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
// Convert here your uri to bitmap then set it.//
mImageView.setImageBitmap(YOUR_BITMAP);
}
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "WorkingWithPhotosApp");
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.
private void dispatchTakePictureIntent(int actionCode) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = null;
try {
f = setUpPhotoFile();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
} catch (IOException e) {
e.printStackTrace();
f = null;
}
startActivityForResult(takePictureIntent, actionCode);
}
private File setUpPhotoFile() throws IOException {
File f = createImageFile();
return f;
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "IMG_" + timeStamp + "_";
File albumF = getAlbumDir();
File imageF = File.createTempFile(imageFileName, ".jpg", albumF);
return imageF;
}
private File getAlbumDir() {
File storageDir = null;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
storageDir = getAlbumStorageDir(PHOTO_ALBUM_NAME);
if (storageDir != null) {
if (! storageDir.mkdirs()) {
if (! storageDir.exists()){
Log.d("Camera", "failed to create directory");
return null;
}
}
}
} else {
Log.v(getString(R.string.app_name), "External storage is not mounted READ/WRITE.");
}
return storageDir;
}
public File getAlbumStorageDir(String albumName) {
return new File (
Environment.getExternalStorageDirectory()
+ "/dcim/"
+ albumName);
}
Hope this helps!
Try like this :
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Strng imagePath = uriSavedImage.getPath(); // uriSavedImage - make it global
}
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
startActivityForResult(takePictureIntent, actionCode);
Implement this
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri outputUri=FileProvider.getUriForFile(this, AUTHORITY, output);
i.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
else if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN) {
ClipData clip=
ClipData.newUri(getContentResolver(), "A photo", outputUri);
i.setClipData(clip);
i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
else {
List<ResolveInfo> resInfoList=
getPackageManager()
.queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
grantUriPermission(packageName, outputUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
}
try {
startActivityForResult(i, CONTENT_REQUEST);
}
catch (ActivityNotFoundException e) {
Toast.makeText(this, R.string.msg_no_camera, Toast.LENGTH_LONG).show();
finish();
}
So in onActivityResult()
if (requestCode == CONTENT_REQUEST) {
// if (resultCode == RESULT_OK) {
Intent i=new Intent(Intent.ACTION_VIEW);
Uri outputUri=FileProvider.getUriForFile(this, AUTHORITY, output);
i.setDataAndType(outputUri, "image/jpeg");
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Bitmap thumbnail = null;
Bundle extras = i.getExtras();
try {
thumbnail = MediaStore.Images.Media.getBitmap(this.getContentResolver(),outputUri );
} catch (IOException e) {
e.printStackTrace();
}
mImageProfile.setImageBitmap(thumbnail);
saveImage(thumbnail);
}
Where mImageProfile
--> is an ImageView on the View.
Remember, that is according the guide.