【问题标题】:How to do Rename and Delete operations in audio file which is in SDcard如何对 SD 卡中的音频文件进行重命名和删除操作
【发布时间】:2015-04-29 18:57:20
【问题描述】:

在这里,我尝试在列表视图中显示所有 3Gp 文件,然后想要在列表项中执行重命名和删除操作,因此选择上下文菜单来解决此问题,我可以在列表上执行操作,但这些操作不会影响音频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);

    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);
    }

【问题讨论】:

    标签: android


    【解决方案1】:

    以下代码适用于我:

    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/

    【讨论】:

      【解决方案2】:

      它工作正常:将此代码放在您要更改名称的位置。

       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”是我的改名。

      【讨论】:

        猜你喜欢
        • 2014-01-04
        • 2018-07-09
        • 2019-01-29
        • 2011-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-22
        • 1970-01-01
        相关资源
        最近更新 更多