以编程方式在android中重命名/删除音频文件

时间:2013-12-23 01:25:38

标签: android

列出了来自图库的所有音频文件,这是我重命名和删除音频文件列表的代码。通过使用此代码,我可以在列表上执行重命名和删除操作,只有那些不影响音频文件,如何执行重命名并根据我的代码删除了库中音频文件的操作,我尝试使用File但是没有用,我的代码中有任何错误,请更正我plz 这是我的代码 变量

ListView myList;
    List values;
    ArrayAdapter adapter;
    MediaPlayerActivity mp = new MediaPlayerActivity();

使用contextmenu重命名和删除的代码

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("öğ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));
}

2 个答案:

答案 0 :(得分:1)

重命名文件: 工作正常:将此代码放在要更改名称的位置。

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

这里&#34;样本&#34;是我的SD卡主目录名,&#34; fromFullPath&#34;是我的文件名,它位于示例目录中,&#34; toFullPath&#34;是我改名。

答案 1 :(得分:0)

删除文件: 工作正常:将此代码放在要删除文件的位置。

File sdcard = new File(Environment.getExternalStorageDirectory(), "sample");
            String fromFullPath = "/username556596268.mp3";

            File from = new File(sdcard,fromFullPath);
            from.delete();
from.deleteOnExit();