[英]How to do Rename and Delete operations in audio file which is in SDcard
在这里,我试图在列表视图中显示所有3Gp文件,然后要在列表项中执行“重命名”和“删除”操作,因此选择上下文菜单来解决此问题,我可以在列表中执行操作,但那些操作不会影响SDcard中的音频文件,建议我这样做。 这是我执行操作的代码
// 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);
registerForContextMenu(myList);
}
final int CONTEXT_MENU_DELETE = 1;
final int CONTEXT_MENU_RENAME = 2;
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
menu.add(Menu.NONE, CONTEXT_MENU_DELETE, Menu.NONE, "Delete");
menu.add(Menu.NONE, CONTEXT_MENU_RENAME, Menu.NONE, "Rename");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
.getMenuInfo();
Long id = myList.getAdapter().getItemId(info.position);
final int number_of_item_in_listview = Integer.valueOf(id.intValue());
final int context_menu_number = item.getItemId();
switch (item.getItemId()) { // retrieves the id of the item clicked. In
// this case it can 1,2 or 3 as we declared
// it earlier.
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);
case CONTEXT_MENU_RENAME:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Rename item");
alert.setMessage("Enter new name for selected item");
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
String value = input.getText().toString();
File f = new File(path + filename);
if (f != null && f.exists()) {
// delete it
f.delete();
}
values.set(number_of_item_in_listview, value
+ ".3gp");
adapter.notifyDataSetChanged();
}
});
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
});
alert.show();
return (true);
}
以下代码对我有用:
File sdcard = Environment.getExternalStorageDirectory();
File from = new File(sdcard,"from.txt");
File to = new File(sdcard,"to.txt");
from.renameTo(to);
并可能在下面的链接将帮助您:
http://androidadvices.com/how-to-rename-files-in-android-phone/
它的工作正常:将此代码放在您要更改名称的位置。
File sdcard = new File(Environment.getExternalStorageDirectory(), "sample");
String fromFullPath = "/username556596268.mp3";
String toFullPath = "/username.mp3";
File from = new File(sdcard,fromFullPath);
File to = new File(sdcard,toFullPath);
from.renameTo(to);
from.delete();
这里的“ sample”是我的sdcard主目录名称,“ fromFullPath”是我的文件名,它位于示例目录中,“ toFullPath”是我的更改名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.