2012-03-02 91 views
0

在我的應用程序中,我將表格保存爲圖像。用戶可以刪除一次,他在真實中打開的文件(影像)我用下面的代碼,當用戶點擊「打開」,打開一個圖像。無法使用刪除功能從SD卡移除文件

File directory=new File(extStorageDirectory,File.separator+"myDirectory"+File.separator); 
File fileInDirectory=new File(directory, fileName); 

//I save the opened file's path n "filePath" 
filePath=fileInDirectory.getAbsolutePath(); 
Bitmap bitmap = BitmapFactory.decodeFile(fileInDirectory.getAbsolutePath()); 
ImageView ivv=(ImageView) findViewById(R.id.imageView); 
ivv.setImageBitmap(bitmap); 

//I enable the delete button 
deleteFile.setEnabled(true); 

文件是沒有任何問題打開。而當用戶點擊「刪除」我做了以下內容:

//I create a file using the filePath I saved earlier 
    File file=new File(filePath); 
    file.delete();      

但它不會從SD卡中刪除文件。我已驗證並且filePath是正確的。我也試過DELETEFILE(字符串)功能:

deleteFile(fileNeme); 
//fileName is the name of my file that I save earlier and I've verified it by printing it and there is no problem. 

我已經把

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

在我的清單文件。所以我不認爲這是一個權利問題,因爲我可以從SD卡上讀寫。

有沒有辦法做到這一點?編輯: 這是我的代碼來保存文件。

this.save.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) { 
// TODO Auto-generated method stub 
AlertDialog.Builder alert=new AlertDialog.Builder(LensCalculator.this); 
alert.setTitle("Save"); 
alert.setMessage("Enter file name"); 

final EditText input=new EditText(MyActivity.this); 
alert.setView(input); 

alert.setPositiveButton("OK", new DialogInterface.OnClickListener(){ 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 
    TableView table=(TableView) findViewById(R.id.tableId); 
    table.setDrawingCacheEnabled(true); 
    Bitmap b=table.getDrawingCache(); 
    Canvas canvas=new Canvas(b); 
    canvas.drawBitmap(b, 0f, 175f, null); 
    OutputStream outStream = null; 
    String fileName=input.getText().toString(); 
    File directory =new File(extStorageDirectory+File.separator+"myDirectory"+File.separator); 
    if(!directory.mkdir()) 
      directory.mkdir(); 
    File file = new File(directory, fileName); 
    try { 
     outStream = new FileOutputStream(file); 
     b.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
     FileOutputStream fOut=openFileOutput("public.dat", Context.MODE_PRIVATE|Context.MODE_APPEND); 
     OutputStreamWriter osw=new OutputStreamWriter(fOut); 
     osw.write(fileName+"\n"); 

     osw.close(); 
     fOut.close(); 
     outStream.close(); 
     table.destroyDrawingCache(); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
      e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
      e.printStackTrace(); 
    } 
} 
}); 
+2

檢查'deleteFile()'的返回值---它會告訴你它是否有效。另外,確保文件在刪除時不會打開;這可能會導致刪除失敗。 – 2012-03-02 11:34:17

+0

也有一個deleteOnExit,可能不太嚴格,並允許調度刪除打開的文件 – njzk2 2012-03-02 11:35:55

+0

@DavidGiven我檢查了deleteFile的值,它是「false」。打開文件後關閉所有「流」。所以我不明白我該如何檢查我的文件是否在某處打開:s。 – Anila 2012-03-02 11:46:02

回答

0

試試看:yourFile.delete();

+0

我已經試過了。返回值爲true,但文件保留在SD卡中。 – Anila 2012-03-02 11:50:44

+0

@appserv,他在想什麼? – 2012-03-02 11:51:04

+0

@AndroidKiller謝謝:)但他? :s – Anila 2012-03-02 11:56:18

0

如果路徑是正確的&你有權限&文件沒有再打開它應該工作。這是一個神祕的情況。你有什麼例外嗎?確保你沒有這樣的東西:

catch(Exception e) { 
// Do nothing 
} 

而這一個是一個遙遠的可能性,但無論如何。你如何檢查文件被刪除?請記住,當您將手機連接到計算機時,以及安裝SD卡時,您的應用程序將無法訪問它(因此無法刪除文件)。

另一種遙遠的可能性......嘗試添加:

/*...*/ 
ivv.setImageBitmap(bitmap); 
bitmap.recycle(); 
bitmap = null; 

編輯

我不明白你爲什麼需要這樣一個複雜的代碼,這可能是更簡單:

File file = new File(directory, fileName); 
try { 
    outStream = new FileOutputStream(file); 
    b.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
    FileOutputStream fOut=openFileOutput(fileName, Context.MODE_PRIVATE|Context.MODE_APPEND); 
    OutputStreamWriter osw=new OutputStreamWriter(fOut); 
    osw.write(fileName+"\n"); 

    osw.close(); 
    fOut.close(); 
    outStream.close(); 
    table.destroyDrawingCache(); 
} catch (FileNotFoundException e) { 
    // TODO Auto-generated catch block 
     e.printStackTrace(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
     e.printStackTrace(); 
} 

喜歡分享:

FileOutputStream out = null; 
try { 
     out = new FileOutputStream(directory + fileName); 
     bmp.compress(Bitmap.CompressFormat.PNG, 100, out); 
} catch (Exception e) { 
     e.printStackTrace(); 
} finally { 
     try { 
      if(out != null) { 
       out.close(); 
      } 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
} 
+0

好,所以我沒有Catch(exeception e){}。我試過bitmap.recycle ..和應用程序崩潰引發NullPointerException。我在我的手機上測試我的應用程序,以檢查它是否被刪除,我打開保存我的文件的目錄。所以這可能是問題所在? – Anila 2012-03-02 12:13:32

+0

爲什麼你會得到空指針異常?此時位圖不應該爲空。是的,如果您在檢查後忘記關閉「USB存儲」模式,則可能會出現問題。 – Caner 2012-03-02 12:20:38

+0

實際上,我添加它在刪除功能,如: 位圖bp = ivv.getDrawingCache(); bp.recycle(); bp = null; 我在做什麼有什麼問題? USB存儲模式已關閉。 – Anila 2012-03-02 12:27:15