Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Delete Image from SD Card with OnClick

I'm trying to simply delete an image from a simple app. I have it so that when you click on the image, it'll bring up an a dialog with the option to delete it. I thought this would just be something simple, but everything I have been trying doesn't seem to be doing anything. Below is my code. Any ideas would be greatly appreciated.

    delete.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
    int id = viewIt.getId();

    File file = new File("file://" + arrPath[id]+".jpg");
    file.delete();


    }
});
like image 231
BossWalrus Avatar asked Jan 13 '23 15:01

BossWalrus


1 Answers

Have you added permission in manifest file ?

Any app that declares the WRITE_EXTERNAL_STORAGE permission is implicitly granted READ_EXTERNAL_STORAGE permission.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>     

Get the path of the file required and delete the file

File file= new File(android.os.Environment.getExternalStorageDirectory()+ "/myfolder/myimage.jpg");
    if(file.exists())
    {
         file.delete();
    }
like image 113
Raghunandan Avatar answered Jan 22 '23 06:01

Raghunandan