Does anyone know how to delete an image from gallery, most specifically from folder "Picture" that ACTUALLY works this days? I've tried literally EVERYTHING available here on this site and even from others, but everything is too old and none of them seems to work anymore... My problem is that I have a function on my app to rotate an image selected from the gallery to send as a chat msg. I have to use this function to fix the bug with Samsung devices that doesn't recognize portrait images and put them as landscape. But the problem with this function is that it creates another image on user's gallery, so I want to delete that duplicated file after the app upload it to Firebase. But I just can't do it! I tried everything and none of them was capable to delete the damn file! Please someone give something up to this date please...
Below is my sniped code...
Intent:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, GALLERY_CODE);
On ActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_CODE && resultCode == RESULT_OK && data != null) {
hideBottomSheet();
displayLoadingBar();
Uri photoUri = data.getData();
String imagePath = PathUtil.getRealPathFromURI(ChatActivity.this, photoUri);
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
imageUri = rotatedImage(bitmap, imagePath);
uploadFile(imageUri, imageStorageReference);
}
}
Rotate Image function:
public Uri rotatedImage(Bitmap bitmap, String photoPath) {
ExifInterface exifInterface = null;
try {
exifInterface = new ExifInterface(photoPath);
} catch (IOException e) {
e.printStackTrace();
}
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(270);
break;
default:
}
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(this.getContentResolver(), rotatedBitmap,
null, null);
return Uri.parse(path);
}
As you can see when I get the path inside the rotate function is when the duplicated image is being created, but I don't know how to get the path from a bitmap without creating a new one physically in storage... If you know, that could be a solution as well... But if that is not possible, what I would like is to delete that second image after calling the "uploadFile" function inside the "activityResult". Please help me!!!
question from:
https://stackoverflow.com/questions/65864644/delete-image-from-folder-picture-that-actually-works-nowdays 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…