【问题标题】:Cannot remove files from sdcard using delete functions无法使用删除功能从 sdcard 中删除文件
【发布时间】:2012-03-02 11:27:58
【问题描述】:

在我的应用程序中,我将表格保存为图像。用户可以在打开文件(图像)后删除。当用户单击“打开”时,我使用以下代码打开图像:

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

但它不会从 sdcard 中删除文件。我已经验证并且文件路径是正确的。我也试过 deleteFile(String) 函数:

 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" />

在我的清单文件中。所以我认为这不是权利问题,因为我可以从 sdcard 读写。

有没有办法做到这一点?

编辑: 这是我保存文件的代码。

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

【问题讨论】:

  • 检查deleteFile()的返回值——它会告诉你它是否有效。此外,请确保删除文件时该文件未在任何地方打开;这可能会导致删除失败。
  • 还有一个 deleteOnExit 可能不那么严格,允许安排删除打开的文件
  • @DavidGiven 我检查了 deleteFile 值,它是“假的”。打开文件后,我关闭了所有“流”。所以我不知道如何检查我的文件是否在某个地方打开:s.
  • @njzk2 不,在 Android 上这不起作用。它在文档中说“在 Android 上,应用程序生命周期不包括 VM 终止,因此调用此方法不会确保文件被删除。”
  • @njzk2 尝试使用 deleteOnExit() 但在我的情况下不起作用。可能是我用错了。我打开文件后添加了它。没有用所以我尝试在删除功能中添加它但没有效果。

标签: android file-io sd-card


【解决方案1】:

试试看:yourFile.delete();

【讨论】:

  • 我已经试过了。返回值为 true 但文件保留在 sdcard 中。
  • @appserv 他在尝试什么?
  • 你关闭了所有的strems吗?检查一下。
  • @appserv 刚刚再次检查,我正在关闭所有流。
【解决方案2】:

如果路径正确并且您有权限并且文件未打开,那么它应该可以工作。这是一个神秘的情况。你有任何例外吗?确保你没有这样的东西:

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

【讨论】:

  • 好的,所以我没有 Catch(异常 e){}。我试过 bitmap.recycle.. 并且应用程序崩溃引发 NullPointerException。我正在手机上测试我的应用程序,以便检查它是否被删除,我打开保存文件的目录。所以这可能是问题所在?
  • 为什么会出现空指针异常?位图此时不应为空。是的,如果您在检查后忘记关闭“USB 存储”模式,这可能是个问题。
  • 实际上我在删除函数中添加了它,例如:Bitmap bp=ivv.getDrawingCache(); bp.recycle(); bp=空;我在做什么有什么问题吗? USB 存储模式已关闭。
  • 从那里删除它并将其添加到setImageBitmap 下。让我们希望这行得通。如果不是,我不知道出了什么问题:(
  • 不起作用,而是应用程序崩溃,出现以下错误“画布:尝试使用回收的位图 ..”。 :( 似乎没有任何效果
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-23
  • 1970-01-01
  • 2013-08-09
相关资源
最近更新 更多