Jak usunąć plik z karty SD?
Tworzę plik do wysłania jako załącznik do wiadomości e-mail. Teraz chcę usunąć obraz po wysłaniu e-maila. Czy istnieje sposób na usunięcie pliku?
Próbowałem myFile.delete();
ale nie usunął pliku.
Używam tego kodu dla Androida, więc językiem programowania jest Java, używająca zwykłych metod Android, aby uzyskać dostęp do karty SD. Usuwam plik w metodzie onActivityResult
, gdy {[2] } zostanie zwrócony na ekran po wysłaniu wiadomości e-mail.
14 answers
File file = new File(selectedFilePath);
boolean deleted = file.delete();
Gdzie selectedFilePath jest ścieżką pliku, który chcesz usunąć - na przykład:
/sdcard/YourCustomDirectory / ExampleFile.mp3
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-08-10 09:14:48
Również musisz dać pozwolenie, jeśli używasz > 1.6 SDK
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
W AndroidManifest.xml
Pliku
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-23 11:23:57
Zmiana dla Androida 4.4 +
Apps are not allowed to write (Usuń, zmodyfikuj ...) do zewnętrznego przechowywania z wyjątkiem do ich katalogów specyficznych dla pakietu.
Jak stwierdza dokumentacja Androida:
" aplikacje nie mogą zapisywać się do dodatkowej pamięci zewnętrznej urządzeń, z wyjątkiem katalogów specyficznych dla ich pakietów, dozwolonych przez zsyntetyzowane uprawnienia."
Jednak nasty obejście istnieje (patrz kod poniżej) . Testowane na Samsung Galaxy S4, ale ta poprawka nie działa na wszystkich urządzeniach. Ponadto nie liczyłbym na to, że to obejście będzie dostępne w przyszłych wersjach Androida.
Istnieje świetny artykuł wyjaśniający (4.4+) zmianę uprawnień pamięci zewnętrznej .
Możesz przeczytać więcej o obejściu tutaj . Kod źródłowy obejścia pochodzi z tej strony .
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;
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-20 20:59:16
Android Context ma następującą metodę:
public abstract boolean deleteFile (String name)
Wierzę, że to zrobi to, co chcesz z odpowiednimi premisjami aplikacji, jak wymieniono powyżej.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-12-30 13:03:55
Rekurencyjnie usuwa wszystkie dzieci pliku ...
public static void DeleteRecursive(File fileOrDirectory)
{
if (fileOrDirectory.isDirectory())
{
for (File child : fileOrDirectory.listFiles())
{
DeleteRecursive(child);
}
}
fileOrDirectory.delete();
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-15 07:57:15
To działa dla mnie: (Usuń obraz z galerii)
File file = new File(photoPath);
file.delete();
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(photoPath))));
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-16 11:47:59
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());
}
Ten kod Ci pomoże.. A w manifeście Androida musisz uzyskać pozwolenie na dokonanie modyfikacji..
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-07 12:35:20
Spróbuj tego.
File file = new File(FilePath);
FileUtils.deleteDirectory(file);
Z Apache Commons
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-27 05:01:26
Sorry: jest błąd w moim kodzie wcześniej z powodu walidacji strony.
String myFile = "/Name Folder/File.jpg";
String myPath = Environment.getExternalStorageDirectory()+myFile;
File f = new File(myPath);
Boolean deleted = f.delete();
Myślę, że to jasne...
Najpierw musisz znać lokalizację pliku.
Po drugie,,, Environment.getExternalStorageDirectory()
jest metodą, która pobiera katalog aplikacji.
Na koniec plik klasy, który obsługuje Twój plik...Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-02 04:46:27
Miałem podobny problem z aplikacją działającą na 4.4. To, co zrobiłem, było czymś w rodzaju włamania.
Zmieniłem nazwę plików i zignorowałem je w mojej aplikacji.
Ie.
File sdcard = Environment.getExternalStorageDirectory();
File from = new File(sdcard,"/ecatAgent/"+fileV);
File to = new File(sdcard,"/ecatAgent/"+"Delete");
from.renameTo(to);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-14 12:58:15
To mi pomogło.
String myFile = "/Name Folder/File.jpg";
String my_Path = Environment.getExternalStorageDirectory()+myFile;
File f = new File(my_Path);
Boolean deleted = f.delete();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-07 17:22:37
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
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-13 14:38:34
Możesz usunąć plik w następujący sposób:
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());
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-23 15:58:12
File filedel = new File("/storage/sdcard0/Baahubali.mp3");
boolean deleted1 = filedel.delete();
Lub Spróbuj Tego:
String del="/storage/sdcard0/Baahubali.mp3";
File filedel2 = new File(del);
boolean deleted1 = filedel2.delete();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-02 11:36:45