Im trying to delete a music file through my App but can't achieve that. Ive checked with
我试图通过我的应用删除一个音乐文件,但无法实现。我检查了
boolean exists = temp.exists();
boolean isFile = temp.isFile();
if there true and yes they are. These methods returns me true. But when I come to the delete method :
如果它们是真的,它们是真的。这些方法返回true。但是当我提到删除方法时:
boolean deleted = temp.delete();
It returns me False and the file is not getting deleted. There are no Exception throws just a false return to my deleted variable.
它返回False,文件没有被删除。没有例外,只是向已删除的变量抛出一个错误的返回。
Im also using these permissons :
我也使用这些permisson:
<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.ACTION_HEADSET_PLUG"/>
Someone got an Idea for a solution ? (Or other classes I can use ?)
有人有办法解决吗?(或者其他我可以使用的类?)
Edit: Thats my full code
编辑:这是我的全部代码
File temp = new File(str_path);
boolean exists = temp.exists();
boolean isFile = temp.isFile();
if (exists)) {
boolean deleted = temp.delete();
if (deleted) {
Toast.makeText(context, "Successful deleted " + Title_Artist, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Not able to delete file " + Title_Artist, Toast.LENGTH_SHORT).show();
}
}
(And I checked while debuging if the object has his path in it and it have it)
(我在调试时检查了对象是否有它的路径,并且它有它)
4 个解决方案
#1
3
Delete file music you must do two task:
删除文件音乐你必须做两个任务:
-
Delete file in Storage.
删除文件的存储。
public static boolean delete(File path) { boolean result = true; if (path.exists()) { if (path.isDirectory()) { for (File child : path.listFiles()) { result &= delete(child); } result &= path.delete(); // Delete empty directory. } if (path.isFile()) { result &= path.delete(); } if (!result) { Log.e("Delete", "Delete failed;"); } return result; } else { Log.e("Delete", "File does not exist."); return false; } }
-
Delete file from MediaStore:
从MediaStore删除文件:
public static void deleteFileFromMediaStore(final ContentResolver contentResolver, final File file) { int sdk = android.os.Build.VERSION.SDK_INT; if (sdk >= android.os.Build.VERSION_CODES.HONEYCOMB) { String canonicalPath; try { canonicalPath = file.getCanonicalPath(); } catch (IOException e) { canonicalPath = file.getAbsolutePath(); } final Uri uri = MediaStore.Files.getContentUri("external"); final int result = contentResolver.delete(uri, MediaStore.Files.FileColumns.DATA + "=?", new String[]{canonicalPath}); if (result == 0) { final String absolutePath = file.getAbsolutePath(); if (!absolutePath.equals(canonicalPath)) { contentResolver.delete(uri, MediaStore.Files.FileColumns.DATA + "=?", new String[]{absolutePath}); } } } }
You can reset/rescan MediaStore instead of do some code above.
您可以重置/重新扫描MediaStore,而不是执行上面的一些代码。
Note: If you delete from SD card and android 4.4 +
注意:如果从SD卡和android 4.4 +中删除
Change for Android 4.4+ : Apps are not allowed to write (delete, modify ...) to external storage except to their package-specific directories.
Android 4.4+的改变:除了特定于包的目录之外,应用程序不允许将(删除、修改…)写入外部存储。
#2
2
The path from your comment looks like the file is on a removable SD card. You need special permissions on Android 4.4+ to manage or delete files on an SD card. You will need to use DocumentFile#delete()
.
您的注释的路径看起来像是文件在一个可移动的SD卡上。您需要Android 4.4+上的特殊权限来管理或删除SD卡上的文件。您将需要使用DocumentFile#delete()。
For help accessing files on a removable SD card using DocumentFile see the following * post:
有关使用DocumentFile访问可移动SD卡上的文件,请参阅以下*文章:
How to use the new SD card access API presented for Android 5.0 (Lollipop)?
如何使用为Android 5.0 (Lollipop)提供的新的SD卡访问API ?
There is also a hack that might work without using DocumentFile
as explained by the developer of FX file manager here: http://forum.xda-developers.com/showpost.php?p=52151865
还有一种方法可以在不使用DocumentFile的情况下工作,如FX文件管理器的开发人员在这里所解释的:http://forum.xdda -developers.com/showpost.php?
#3
0
Since you are checking that the file exists, there can only be one reason you can not delete the file: you don't have permissions to do it.
由于您正在检查该文件是否存在,因此您不能删除该文件的唯一原因是:您没有执行该文件的权限。
An app can not delete system files, or files of other apps.
应用程序不能删除系统文件或其他应用程序的文件。
#4
0
Suppose your file path is
假设您的文件路径是
Environment.getExternalStorageDirectory().getPath()
+ "/Music"
+ "/"
+ "song.mp3"
delete it like this
这样删除
File dir = new File(Environment.getExternalStorageDirectory()
.getPath()
+ "/Music");
if (dir.isDirectory()) {new File(dir, song.mp3).delete();}
if you want to delete all the files in music folder do this
如果你想删除音乐文件夹中的所有文件,请这样做
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
new File(dir, children[i]).delete();
}
}
#1
3
Delete file music you must do two task:
删除文件音乐你必须做两个任务:
-
Delete file in Storage.
删除文件的存储。
public static boolean delete(File path) { boolean result = true; if (path.exists()) { if (path.isDirectory()) { for (File child : path.listFiles()) { result &= delete(child); } result &= path.delete(); // Delete empty directory. } if (path.isFile()) { result &= path.delete(); } if (!result) { Log.e("Delete", "Delete failed;"); } return result; } else { Log.e("Delete", "File does not exist."); return false; } }
-
Delete file from MediaStore:
从MediaStore删除文件:
public static void deleteFileFromMediaStore(final ContentResolver contentResolver, final File file) { int sdk = android.os.Build.VERSION.SDK_INT; if (sdk >= android.os.Build.VERSION_CODES.HONEYCOMB) { String canonicalPath; try { canonicalPath = file.getCanonicalPath(); } catch (IOException e) { canonicalPath = file.getAbsolutePath(); } final Uri uri = MediaStore.Files.getContentUri("external"); final int result = contentResolver.delete(uri, MediaStore.Files.FileColumns.DATA + "=?", new String[]{canonicalPath}); if (result == 0) { final String absolutePath = file.getAbsolutePath(); if (!absolutePath.equals(canonicalPath)) { contentResolver.delete(uri, MediaStore.Files.FileColumns.DATA + "=?", new String[]{absolutePath}); } } } }
You can reset/rescan MediaStore instead of do some code above.
您可以重置/重新扫描MediaStore,而不是执行上面的一些代码。
Note: If you delete from SD card and android 4.4 +
注意:如果从SD卡和android 4.4 +中删除
Change for Android 4.4+ : Apps are not allowed to write (delete, modify ...) to external storage except to their package-specific directories.
Android 4.4+的改变:除了特定于包的目录之外,应用程序不允许将(删除、修改…)写入外部存储。
#2
2
The path from your comment looks like the file is on a removable SD card. You need special permissions on Android 4.4+ to manage or delete files on an SD card. You will need to use DocumentFile#delete()
.
您的注释的路径看起来像是文件在一个可移动的SD卡上。您需要Android 4.4+上的特殊权限来管理或删除SD卡上的文件。您将需要使用DocumentFile#delete()。
For help accessing files on a removable SD card using DocumentFile see the following * post:
有关使用DocumentFile访问可移动SD卡上的文件,请参阅以下*文章:
How to use the new SD card access API presented for Android 5.0 (Lollipop)?
如何使用为Android 5.0 (Lollipop)提供的新的SD卡访问API ?
There is also a hack that might work without using DocumentFile
as explained by the developer of FX file manager here: http://forum.xda-developers.com/showpost.php?p=52151865
还有一种方法可以在不使用DocumentFile的情况下工作,如FX文件管理器的开发人员在这里所解释的:http://forum.xdda -developers.com/showpost.php?
#3
0
Since you are checking that the file exists, there can only be one reason you can not delete the file: you don't have permissions to do it.
由于您正在检查该文件是否存在,因此您不能删除该文件的唯一原因是:您没有执行该文件的权限。
An app can not delete system files, or files of other apps.
应用程序不能删除系统文件或其他应用程序的文件。
#4
0
Suppose your file path is
假设您的文件路径是
Environment.getExternalStorageDirectory().getPath()
+ "/Music"
+ "/"
+ "song.mp3"
delete it like this
这样删除
File dir = new File(Environment.getExternalStorageDirectory()
.getPath()
+ "/Music");
if (dir.isDirectory()) {new File(dir, song.mp3).delete();}
if you want to delete all the files in music folder do this
如果你想删除音乐文件夹中的所有文件,请这样做
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
new File(dir, children[i]).delete();
}
}