在我的应用程序中,我将表格保存为图像。用户可以在打开后删除文件(图像)。当用户点击“打开”时,我使用以下代码打开图像:
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(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" />
在我的清单文件中。所以我不认为这是权利问题,因为我能够从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();
}
}
});
答案 0 :(得分:0)
试试:yourFile.delete();
答案 1 :(得分:0)
如果路径正确&amp;你有权限&amp;文件未打开然后它应该工作。这是一个神秘的情况。你有任何例外吗?确保你没有这样的东西:
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();
}
}