这是我在列表视图中显示所有音乐文件名的代码,这里我将数组列表中的所有文件显示在列表视图中,然后使用上下文菜单在listitems上执行“重命名”,但这些更改不会影响实际在SD卡中的文件,建议我如何做到这一点,我尝试了这样的文件操作。
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, "silmek");
menu.add(Menu.NONE, CONTEXT_MENU_RENAME, Menu.NONE, "adını değiştirmek");
}
@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()) {
这是我如何使用listview
的上下文菜单以编程方式重命名文件case CONTEXT_MENU_RENAME:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("öğeyi yeniden adlandırmak");
alert.setMessage("Seçili öğe için yeni bir isim girin");
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("tamam",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
String value = input.getText().toString();
values.set(number_of_item_in_listview, value
+ ".3gp");
adapter.notifyDataSetChanged();
/*
* File sdcard =
* Environment.getExternalStorageDirectory(); File
* from = new File(sdcard,"from.txt"); File to = new
* File(sdcard,"to.txt"); from.renameTo(to);
*/
File f = new File(path + filename);
if (f != null && f.exists()) {
File from = new File(f, f.getName());
File to = new File(f, value);
from.renameTo(to);
}
}
});
alert.setNegativeButton("iptal",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
});
alert.show();
return (true);
}
return (super.onOptionsItemSelected(item));
答案 0 :(得分:2)
我建议使用File.renameTo()而不是运行mv命令,因为我很确定后者不受支持..
您是否已授权您的申请写入SD卡?
您可以通过将以下内容添加到 AndroidManifest.xml :
来执行此操作<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
如果在添加权限后它不起作用,请在尝试重命名文件时检查设备日志是否有错误(使用adb命令或在Eclipse中的logcat视图中)。
访问SD卡时,不应对路径进行硬编码,而应使用Environment.getExternalStorageDirectory()
方法获取目录。
以下代码适用于我:
File sdcard = Environment.getExternalStorageDirectory();
File from = new File(sdcard,"from.txt");
File to = new File(sdcard,"to.txt");
from.renameTo(to);