2012-03-02 92 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