Android Delete Image from SD Card with OnClick

柔情痞子 提交于 2020-01-02 06:33:50

问题


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();


    }
});

回答1:


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();
    }


来源:https://stackoverflow.com/questions/16052393/android-delete-image-from-sd-card-with-onclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!