I am trying to get image from camera intent. Its working nice but only in my device (oreo / 8.1.0). I generate unsigned apk and tried to run in other device (pie and lolipop)but its not working.
I search stackoverflow and other sites, but people asked question about "camera intent problem in lolipop". My question is something different (I think).
I tried to check sdk version and apply different code. I.e. as per one answer from stackoverflow I used this code Picasso.get().load(file).into(imageView);
instead of Picasso.get().load(photoURI).into(imageView);
in LOLIPOP but its not working.
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null; // Create the File where the photo should go
try {
photoFile = createImageFile();
tempPhoto = photoFile;
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
photoURI = FileProvider.getUriForFile(this,
"com.package.appname.fileprovider",
photoFile);
tempPhoto = photoFile;
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
Code for createImageFile function:
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;
}
Receiving result here
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
try {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
File file = new File(mCurrentPhotoPath);
Picasso.get().load(file).into(imageView);
// Bitmap bitmap = MediaStore.Images.Media
// .getBitmap(this.getContentResolver(), Uri.fromFile(file));
// if (bitmap != null) {
// imageView.setImageBitmap(bitmap);
// }
} else if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK) {
File file = new File(mCurrentPhotoPath);
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.fromFile(file));
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
}
}
} catch (Exception error) {
error.printStackTrace();
}
}
Can anyone help me to give accurate result in all sdk versions? Please help me. Thanks in advance.
Try this:
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
try {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
Uri uri = intent.getData(); // you need take uri of image
imageView.setImageURI(uri); // you don't need of picasso to load local images
// Picasso.get().load(file).into(imageView);
// Bitmap bitmap = MediaStore.Images.Media
// .getBitmap(this.getContentResolver(), Uri.fromFile(file));
// if (bitmap != null) {
// imageView.setImageBitmap(bitmap);
// }
} else if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK) {
File file = new File(mCurrentPhotoPath);
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.fromFile(file));
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
}
}
} catch (Exception error) {
error.printStackTrace();
}
}