簡體   English   中英

如何對SD卡中的音頻文件進行重命名/刪除操作

[英]How to do Rename/Delete operations on audio files which are in SD card

我正在嘗試刪除和重命名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文件中

添加權限以首先在xml中寫外部存儲。

   String newName ="new Name";
   File oldFile = new File(Environment.getExternalStorageDirectory()+songName);

   File latestname = new File(Enviornment.get+ newName + ".mp3");
   boolean  success = oldFile .renameTo(latestname );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM