我正在創建一個文件作爲附件發送到電子郵件。現在我想在發送電子郵件後刪除圖像。有沒有辦法刪除文件?我試過myFile.delete();
但它沒有刪除文件。如何從SD卡中刪除文件?
我對Android使用此代碼,所以編程語言是使用通常的Android方式訪問SD卡的Java。在發送電子郵件後,當Intent
返回到屏幕時,我正在刪除onActivityResult
方法中的文件。
我正在創建一個文件作爲附件發送到電子郵件。現在我想在發送電子郵件後刪除圖像。有沒有辦法刪除文件?我試過myFile.delete();
但它沒有刪除文件。如何從SD卡中刪除文件?
我對Android使用此代碼,所以編程語言是使用通常的Android方式訪問SD卡的Java。在發送電子郵件後,當Intent
返回到屏幕時,我正在刪除onActivityResult
方法中的文件。
File file = new File(selectedFilePath);
boolean deleted = file.delete();
其中selectedFilePath是要刪除的文件的路徑 - 例如:
/sdcard/YourCustomDirectory/ExampleFile.mp3
我認爲內心的孩子不會被刪除..你必須刪除所有的內心孩子。看到我的答案如下。 – Nepster 2014-05-15 07:56:45
不幸的是,這不適用於Android 4.4+。請參閱下面的答案。 – 2014-05-20 13:44:02
我不明白這是如何工作的許多人。 「刪除」在Android Studio中看起來很灰。 – TheOnlyAnil 2015-06-06 11:18:48
你也有,如果賦予權限您正在使用> 1.6 SDK
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
in AndroidManifest.xml
文件
Android的上下文有以下方法:
public abstract boolean deleteFile (String name)
我相信這會做你想要用正確的應用premissions什麼上面列出。
這應該是正確的答案。 '上下文。DELETEFILE(文件名);' – Lisandro 2015-08-22 22:49:01
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.exists:真 file.isDirectory:真 file.canRead:假 file.canWrite:FALSE' – Andreyua 2017-06-04 14:13:51
遞歸刪除該文件的所有孩子......
public static void DeleteRecursive(File fileOrDirectory)
{
if (fileOrDirectory.isDirectory())
{
for (File child : fileOrDirectory.listFiles())
{
DeleteRecursive(child);
}
}
fileOrDirectory.delete();
}
應用程序是不允許至寫(刪除,修改...)到外部存儲除了他們包特定目錄。
作爲Android文檔狀態:
「應用不得被允許所允許的 合成權限寫入到次級外部存儲設備 ,除了在它們的特定包的目錄」。
然而討厭的解決方法存在(見下面的代碼)。經過三星Galaxy S4測試,但此修補程序不適用於所有設備。此外我不會指望此變通辦法可用於未來版本的Android。
有一個great article explaining (4.4+) external storage permissions change。您可以閱讀more about workaround here。 解決方法源代碼是從this site。
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;
}
}
這對我的作品:(從庫中刪除圖像)
File file = new File(photoPath);
file.delete();
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(photoPath))));
這爲我工作。
String myFile = "/Name Folder/File.jpg";
String my_Path = Environment.getExternalStorageDirectory()+myFile;
File f = new File(my_Path);
Boolean deleted = f.delete();
對不起:由於網站驗證,我的代碼之前有一個錯誤。
String myFile = "/Name Folder/File.jpg";
String myPath = Environment.getExternalStorageDirectory()+myFile;
File f = new File(myPath);
Boolean deleted = f.delete();
我認爲這是明顯的... 首先你必須知道你的文件的位置。 秒,,, Environment.getExternalStorageDirectory()
是誰得到你的應用程序目錄的方法。 最後,處理文件的類文件...
我在4.4上運行的應用程序有類似的問題。我所做的只是一種破解。
我重命名了這些文件,並在我的應用程序中忽略了它們。
即。
File sdcard = Environment.getExternalStorageDirectory();
File from = new File(sdcard,"/ecatAgent/"+fileV);
File to = new File(sdcard,"/ecatAgent/"+"Delete");
from.renameTo(to);
試試這個。
File file = new File(FilePath);
FileUtils.deleteDirectory(file);
從Apache的百科全書
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
}
你需要提供一些關於你的代碼的解釋! :) – 2017-05-13 17:46:09
讀取文件getExternalStorageDirectory +添加「/音樂/」它是我的路徑 - > mypath =/storage/sdcard/Music /。檢查文件是否存在並讀取。刪除如果存在。這是刪除目錄中的所有列表音樂音樂 – 2017-05-15 17:14:48
將相同的答案添加到答案! :) – 2017-05-15 17:23:39
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();
您可以刪除文件如下:
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());
}
}
您需要提供有關該問題的更多信息,例如您正在使用哪種語言,您如何訪問SD卡等。 – Amok 2009-08-08 09:38:12
您是否正在刷新對磁盤的更改? – 2009-08-08 09:47:47
@Amuck我認爲假設他使用Java是安全的,因爲他沒有指定。 – 2009-08-10 04:41:17