Delete File in SD Card android with string

狂风中的少年 提交于 2019-12-12 10:05:24

问题


I have this string,

Cursor c = db.obtenerDatoEjercicio(selecID);
String stringFoto1 = c.getString(6).toString();

then stringFoto1 is equals "file:///mnt/sdcard/Pictures/neoadnEditGyM/GYM_2.2.jpg"

This file exists.

I want to delete that file on the sd and I used the following code:

String stringFoto1 = "file:///mnt/sdcard/Pictures/neoadnEditGyM/GYM_2.2.jpg"
File archivo1 = new File(stringFoto1);
archivo1.delete();

But this doesn't work, please help.


回答1:


You can try using this:

 String strFile = "/sdcard/Pictures/neoadnEditGyM/GYM_2.2.jpg"  
 File file = new File(strFile);
 boolean deleted = file.delete();

Also, if you are using >1.6 SDK you have to give permission

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

in AndroidManifest.xml file




回答2:


Finally, I used another method.

Because when I saved the photo was with the full path. I put only keep the name and well accessed by "enviroment".

 String folderSD = Environment.getExternalStorageDirectory().toString()+"/Pictures/neoadnEditGyM/";

        Cursor c = db.obtenerDatoEjercicio(selecID);
        String stringFoto1 = c.getString(6).toString();
        String stringFoto2 = c.getString(7).toString();
        File foto1 = new File(folderSD+stringFoto1);
        if (foto1.exists()){
            foto1.delete(); 
            Toast.makeText(this, stringFoto1+" deleted.", Toast.LENGTH_LONG).show();
        }
        File foto2 = new File (folderSD+stringFoto2);
        if(foto2.exists()){
            foto2.delete();
            Toast.makeText(this, stringFoto2+" deleted.", Toast.LENGTH_LONG).show();
        }


来源:https://stackoverflow.com/questions/17880187/delete-file-in-sd-card-android-with-string

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