2013-07-26 30 views
2

我有這個字符串,刪除文件用繩子

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

然後stringFoto1是等於 「文件:///mnt/sdcard/Pictures/neoadnEditGyM/GYM_2.2.jpg」

此文件存在。

我想刪除的SD該文件,我用下面的代碼:

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

但是,這並不正常工作,請大家幫忙。

回答

2

最後,我用另一種方法。

因爲當我保存照片是完整的路徑。 我只保留名稱,並很好地訪問「環境」。

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

您可以嘗試使用這樣的:,

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

此外,如果你使用的是> 1.6 SDK,你必須給允許

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

AndroidManifest.xml文件

+0

我有使用權限。問題是字符串strFile是 - 「file:///mnt/sdcard/Pictures/neoadnEditGyM/GYM_2.2.jpg」 - 因爲我得到了這個數據庫的字符串。 –

+0

然後嘗試使用方法substring()或replace()來轉換您的原始字符串 –

+0

我認爲在'sdcard ...'之前需要一個前導'/',上面寫着 – tallen