我正在创建一个文件以作为电子邮件的附件发送。现在我想在发送电子邮件后删除图像。有没有办法删除文件?
我试过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
文件中
不允许应用程序写入( 删除、修改...)到外部存储,除非是它们的包特定目录。
正如 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;
}
}
Android Context 有以下方法:
public abstract boolean deleteFile (String name)
我相信这将使用上面列出的正确应用程序权限满足您的要求。
递归删除文件的所有子项...
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());
}
此代码将为您提供帮助.. 在 Android 清单中,您必须获得许可才能进行修改..
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
试试这个。
File file = new File(FilePath);
FileUtils.deleteDirectory(file);
来自 Apache Commons
抱歉:由于站点验证,我之前的代码中有错误。
String myFile = "/Name Folder/File.jpg";
String myPath = Environment.getExternalStorageDirectory()+myFile;
File f = new File(myPath);
Boolean deleted = f.delete();
我认为很清楚......首先你必须知道你的文件位置。其次,,,Environment.getExternalStorageDirectory()
是一种获取您的应用程序目录的方法。最后是处理你的文件的类文件......
我在 4.4 上运行的应用程序遇到了类似的问题。我所做的有点像 hack。
我重命名了文件并在我的应用程序中忽略了它们。
IE。
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 my_Path = Environment.getExternalStorageDirectory()+myFile;
File f = new File(my_Path);
Boolean deleted = f.delete();
private boolean deleteFromExternalStorage(File file) {
String fileName = "/Music/";
String myPath= Environment.getExternalStorageDirectory().getAbsolutePath() + fileName;
file = new File(myPath);
System.out.println("fullPath - " + myPath);
if (file.exists() && file.canRead()) {
System.out.println(" Test - ");
file.delete();
return false; // File exists
}
System.out.println(" Test2 - ");
return true; // File not exists
}
您可以按如下方式删除文件:
File file = new File("your sdcard path is here which you want to delete");
file.delete();
if (file.exists()){
file.getCanonicalFile().delete();
if (file.exists()){
deleteFile(file.getName());
}
}
File filedel = new File("/storage/sdcard0/Baahubali.mp3");
boolean deleted1 = filedel.delete();
或者,试试这个:
String del="/storage/sdcard0/Baahubali.mp3";
File filedel2 = new File(del);
boolean deleted1 = filedel2.delete();