Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
85 views
in Technique[技术] by (71.8m points)

android - Delete image from folder "Picture" that actually works nowdays?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well after searching the hell and back, and with a tip from CommonsWare in the comments above I finally managed to come up with a solution for my problem. So in case someone else landed here with the same problem. This is what worked for me.

I replaced this line of code inside of rotatedImage function:

String path = MediaStore.Images.Media.insertImage(this.getContentResolver(), rotatedBitmap, null, null);
return Uri.parse(path);

for this:

byte[] bitmapData = bytes.toByteArray();

try {
    File tempFile = createImageFile();
    FileOutputStream fos = new FileOutputStream(tempFile);
    fos.write(bitmapData);
    fos.flush();
    fos.close();

    Uri newUri = FileProvider.getUriForFile(ChatActivity.this, "com.yourapp.fileprovider", tempFile);

    return newUri;

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;

Here is the createImageFile function:

    private File createImageFile() throws IOException {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,
            ".jpg",
            storageDir
    );

    currentPhotoPath = image.getAbsolutePath();

    return  image;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...