1

Im developing an android app which selects an image from the gallery in one activity and displays it in another.But when I try to delete the selected image it doesnt delete.I'm passing its uri between the two activies. Many thanks in Advance!!!!

Here's my Code :

ACTIVITY HOMESCREEN

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);

     if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
          Uri uri = data.getData();
          Intent i = new Intent(this, Imageviewer.class);       
          i.putExtra("imgpath", uri.toString());
          startActivity(i);
     }
}

IMAGEVIEWER ACTIVITY :

Uri imageUri;
imageUri = Uri.parse(intent.getStringExtra("imgpath"));
File fdelete = new File(imageUri.toString());

    if (fdelete.exists()) {
         if (fdelete.delete()) {
               System.out.println("file Deleted :" );
         } else {
               System.out.println("file not Deleted :");
         }
    }
Andy Res
  • 15,963
  • 5
  • 60
  • 96
Nishant Kohli
  • 445
  • 6
  • 6
  • In the `ACTIVITY HOMESCREEN` you converted the `imgpath` to string once and then in the imgviewer activity you did the same and you get the `URI` again which is the problem..Do it like this : Get the imgPath first from URI, Convert it to string the send it to the next Activity(`IMAGEVIEWER` in your case) then there is no need to convert it or using it as `Uri`, Just retrieve the path then use it. Also, Check this answer: http://stackoverflow.com/a/10716773/4409113 – ʍѳђઽ૯ท Jan 01 '17 at 21:31

3 Answers3

4

First you must take real path of image:

//getting real path from uri
private String getFilePath(Uri uri) {
    String[] projection = {MediaStore.Images.Media.DATA};

    Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(projection[0]);
        String picturePath = cursor.getString(columnIndex); // returns null
        cursor.close();
        return picturePath;
    }
    return null;
}

Then you can delete this file like below:

Uri imageUri;
imageUri = Uri.parse(intent.getStringExtra("imgpath"));
File fdelete = new File(getFilePath(imageUri));

if (fdelete.exists()) {
     if (fdelete.delete()) {
           System.out.println("file Deleted :" );
     } else {
           System.out.println("file not Deleted :");
     }
}
Axbor Axrorov
  • 2,720
  • 2
  • 17
  • 35
0

Try this : (In the second Activity) ImageViewerActivity in your case.

Intent intent = getIntent();
        String receivedPath = intent.getExtras().getString("imgpath");
        File fdelete = new File(receivedPath);

        if (fdelete.exists()) {
            if (fdelete.delete()) {
                System.out.println("file Deleted :" );
            } else {
                System.out.println("file not Deleted :");
            }
        }

Also you can debug your codes line by line to see and check if the ImgPath is getting correctly or not!

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
0

With kotlin you could do: uri.toFile().delete() and its deleted

Jimmy ALejandro
  • 434
  • 5
  • 11
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 26 '21 at 14:02