我正在创建一个文件作为电子邮件的附件发送.现在我想在发送电子邮件后删除图像.有没有办法删除文件?
我试过myFile.delete();但它没有删除该文件.
我正在使用Android的这个代码,因此编程语言是使用通常的Android方式访问SD卡的Java.当我在发送电子邮件后返回到屏幕onActivityResult时,我正在删除该方法中的文件Intent.
我试图删除位于路径的文件
/storage/714D-160A/Xender/image/Screenshot_commando.png
Run Code Online (Sandbox Code Playgroud)
到目前为止我做了什么:
try{
String d_path = "/storage/714D-160A/Xender/image/Screenshot_commando.png";
File file = new File(d_path);
file.delete();
}catch(Exception e){
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
并且文件仍在其位置(未删除:()
我也在Manifest文件中获得了许可.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.STORAGE" />
Run Code Online (Sandbox Code Playgroud) 以下是此方法的源注释:
请注意,在Android上,应用程序生命周期不包括VM终止,因此调用此方法将无法确保删除文件.相反,你应该使用最合适的:
* Use a {@code finally} clause to manually invoke {@link #delete}.
* Maintain your own set of files to delete, and process it at an appropriate point
in your application's lifecycle.
* Use the Unix trick of deleting the file as soon as all readers and writers have
opened it. No new readers/writers will be able to access the file, but all existing
ones will still have access until the last one closes the file.
Run Code Online (Sandbox Code Playgroud)
任何人都可以向我解释它中提到的"Unix技巧"是什么以及如何使用它?