我正在创建一个文件作为电子邮件的附件发送。现在我想在发送电子邮件后删除这个文件中的图像。有没有办法删除这个文件?
我已经尝试过 myFile.delete();
但它并没有删除这个文件。
我正在使用Android编写代码,所以编程语言是Java,采用通常的Android方式来访问SD卡。我正在 onActivityResult
方法中删除这个文件,当一个 Intent
在发送电子邮件后返回到屏幕上时。
我正在创建一个文件作为电子邮件的附件发送。现在我想在发送电子邮件后删除这个文件中的图像。有没有办法删除这个文件?
我已经尝试过 myFile.delete();
但它并没有删除这个文件。
我正在使用Android编写代码,所以编程语言是Java,采用通常的Android方式来访问SD卡。我正在 onActivityResult
方法中删除这个文件,当一个 Intent
在发送电子邮件后返回到屏幕上时。
File file = new File(selectedFilePath);
boolean deleted = file.delete();
selectedFilePath是您想要删除的文件的路径 - 例如:
/sdcard/YourCustomDirectory/ExampleFile.mp3
如果您正在使用1.6 SDK以上的版本,则还需授予权限。
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
在 AndroidManifest.xml
文件中
ContentResolver
。 - user25应用程序不允许在外部存储器上(删除、修改...)写入,除非是它们的特定于包的目录。
正如Android文档所述:
"应用程序不得被允许写入到次要外部存储设备,除了由合成权限允许的它们特定于包的目录。"
然而,存在一个可恶的解决方法(请参见下面的代码)。 在三星Galaxy S4上进行了测试,但是这个修复程序并不能在所有设备上工作。另外,我不会指望这个解决方法在未来版本的Android中可用。
您可以在此处了解更多关于解决方法的信息。解决方法源代码来自此网站。
public class MediaFileFunctions
{
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static boolean deleteViaContentProvider(Context context, String fullname)
{
Uri uri=getFileUri(context,fullname);
if (uri==null)
{
return false;
}
try
{
ContentResolver resolver=context.getContentResolver();
// change type to image, otherwise nothing will be deleted
ContentValues contentValues = new ContentValues();
int media_type = 1;
contentValues.put("media_type", media_type);
resolver.update(uri, contentValues, null, null);
return resolver.delete(uri, null, null) > 0;
}
catch (Throwable e)
{
return false;
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private static Uri getFileUri(Context context, String fullname)
{
// Note: check outside this class whether the OS version is >= 11
Uri uri = null;
Cursor cursor = null;
ContentResolver contentResolver = null;
try
{
contentResolver=context.getContentResolver();
if (contentResolver == null)
return null;
uri=MediaStore.Files.getContentUri("external");
String[] projection = new String[2];
projection[0] = "_id";
projection[1] = "_data";
String selection = "_data = ? "; // this avoids SQL injection
String[] selectionParams = new String[1];
selectionParams[0] = fullname;
String sortOrder = "_id";
cursor=contentResolver.query(uri, projection, selection, selectionParams, sortOrder);
if (cursor!=null)
{
try
{
if (cursor.getCount() > 0) // file present!
{
cursor.moveToFirst();
int dataColumn=cursor.getColumnIndex("_data");
String s = cursor.getString(dataColumn);
if (!s.equals(fullname))
return null;
int idColumn = cursor.getColumnIndex("_id");
long id = cursor.getLong(idColumn);
uri= MediaStore.Files.getContentUri("external",id);
}
else // file isn't in the media database!
{
ContentValues contentValues=new ContentValues();
contentValues.put("_data",fullname);
uri = MediaStore.Files.getContentUri("external");
uri = contentResolver.insert(uri,contentValues);
}
}
catch (Throwable e)
{
uri = null;
}
finally
{
cursor.close();
}
}
}
catch (Throwable e)
{
uri=null;
}
return uri;
}
}
false
并且不会删除文件 :-( - SpikatrixAndroid的Context类有以下方法:
public abstract boolean deleteFile (String name)
我相信只要应用程序权限如上所列,这将实现你想要的功能。
context.deleteFile(filename);
- Lisandro递归删除文件的所有子级...
public static void DeleteRecursive(File fileOrDirectory) {
if (fileOrDirectory.isDirectory()) {
for (File child : fileOrDirectory.listFiles()) {
DeleteRecursive(child);
}
}
fileOrDirectory.delete();
}
File file = new File(photoPath);
file.delete();
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(photoPath))));
public static boolean deleteDirectory(File path) {
// TODO Auto-generated method stub
if( path.exists() ) {
File[] files = path.listFiles();
for(int i=0; i<files.length; i++) {
if(files[i].isDirectory()) {
deleteDirectory(files[i]);
}
else {
files[i].delete();
}
}
}
return(path.delete());
}
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
file.exists:true file.isDirectory:true file.canRead:false file.canWrite:false
- ilw试一下这个。
File file = new File(FilePath);
FileUtils.deleteDirectory(file);
来自Apache Commons
我曾经遇到一个与4.4版本相关的应用程序问题。我的做法是一种巧妙的方法。
我将文件重命名并在我的应用程序中忽略它们。
例如:
File sdcard = Environment.getExternalStorageDirectory();
File from = new File(sdcard,"/ecatAgent/"+fileV);
File to = new File(sdcard,"/ecatAgent/"+"Delete");
from.renameTo(to);
抱歉:由于网站验证,我的代码之前存在错误。
String myFile = "/Name Folder/File.jpg";
String myPath = Environment.getExternalStorageDirectory()+myFile;
File f = new File(myPath);
Boolean deleted = f.delete();
Environment.getExternalStorageDirectory()
是一个获取你的应用程序目录的方法。
最后,使用 File 类来处理你的文件...