I searched in the forum for similar questions but I haven't found a correct answer for my code. I have an activity in which user can choose (from a Dialog) if to upload a photo from gallery or to take it with camera. I'd like to show in a CircularImageView the photo chosen by the user. I was able to do it for images picked from gallery, but unfortunately it doesn't work if I take it with camera. This is the layout code of CircularImageView:
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/photo"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:layout_centerInParent="true"
android:src="@mipmap/add_profile_photo_round"
app:civ_border_width="0dp"
app:civ_border_color="#000000" />
And this is the Java code:
TextView take_photo = (TextView) dialog.findViewById(R.id.take_photo);
...
take_photo.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onClick(View v) {
takePhotoFromCamera();
dialog.dismiss();
}
});
pick_gallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
choosePhotoFromGallary();
dialog.dismiss();
}
});
}
public void choosePhotoFromGallary() {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, GALLERY);
}
@RequiresApi(api = Build.VERSION_CODES.M)
private void takePhotoFromCamera() {
if (checkSelfPermission(Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA},
MY_CAMERA_REQUEST_CODE);
}
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == this.RESULT_CANCELED) {
return;
}
if (requestCode == GALLERY) {
if (data != null) {
Uri contentURI = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), contentURI);
image.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
} else if (requestCode == MY_CAMERA_REQUEST_CODE) {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
options);
image.setImageBitmap(bitmap);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}
As I already said, it works with images from gallery but not with captured one. Thank you for your help P.S. sorry if my English is bad
Edit: I tried to modify the code but it still doesn't work
else if (requestCode == MY_CAMERA_REQUEST_CODE) {
try {
ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(fileUri, "r");
assert parcelFileDescriptor != null;
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
image.setImageBitmap(bitmap);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Second edit: I have used this code but it still doesn't work:
ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(Objects.requireNonNull(data.getData()), "r");
assert parcelFileDescriptor != null;
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
image.setImageBitmap(bitmap);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
If it can be useful for other people, I found this code and it works:
Bitmap bmp = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
// convert byte array to Bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0,
byteArray.length);
encodedString = Base64.encodeToString(byteArray, 0);
image.setImageBitmap(bitmap);