我正在尝试删除和重命名SD卡中的音频文件,因此这是我的代码,用于显示SD卡中的音频文件列表。
// Use the current directory as title
path = "/sdcard/";
if (getIntent().hasExtra("path")) {
path = getIntent().getStringExtra("path");
}
setTitle(path);
// Read all files sorted into the values-array
values = new ArrayList();
File dir = new File(path);
if (!dir.canRead()) {
setTitle(getTitle() + " (inaccessible)");
}
final String[] list = dir.list();
if (list != null) {
for (String file : list) {
if (file.contains(".3gp")) {
values.add(file);
}
}
}
Collections.sort(values);
// Put the data into the list
adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_2, android.R.id.text1, values);
setListAdapter(adapter);
在这里,我可以显示所有音乐文件,但是如果我尝试重命名或删除操作,则不会影响SD卡中的文件,建议我采取一些解决方案
删除和重命名的代码
case CONTEXT_MENU_DELETE:
Toast.makeText(
this,
"You selected item " + context_menu_number
+ " from the context menu", Toast.LENGTH_SHORT)
.show();
Toast.makeText(
this,
"You removed item " + number_of_item_in_listview
+ " from the list", Toast.LENGTH_SHORT).show();
values.remove(number_of_item_in_listview);
// myadapter.notifyDataSetChanged(); //if this does not work,
// reinitialize the adapter:
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,
values);
myList.setAdapter(adapter);
File f = new File(path + filename);
if (f != null && f.exists()) {
// delete it
f.delete();
}
return (true);
最佳答案
尝试以下代码从sdcard删除文件。
File file = new File(FilePath);
boolean deleted = file.delete();
其中
FilePath
是您要删除的文件的路径-例如:/sdcard/YourCustomDirectory/YourFile.mp3
同时给予许可
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
在
AndroidManifest.xml
文件中
关于android - 如何对SD卡中的音频文件进行重命名/删除操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20628210/