如何通过获取SDCard的位置从SDCard中删除音频文件

时间:2013-12-26 09:16:52

标签: android android-listview audio-recording listviewitem

我有一个带有复选框的自定义列表视图,它从SD卡文件夹中获取所有音频文件名,当我点击其中任何一个时播放音频。一切都很好。现在我有一个图像视图,当我点击它时,它显示了2个选项与微调器

1)删除
2)编辑

现在我想要这样,当用户从列表视图中选择任何复选框然后从该微调器中删除时,我想从SD卡以及列表视图中完全删除此音频。所以基本上我想得到列表视图的位置,用户点击了哪个项目并想删除它.​​.....

所以请指导我如何实现这一目标。以下是我的代码。

final Spinner spinnerDelete = new Spinner(Assignment.this);
            alertDialogBuilder.setView(spinnerDelete);

            adapterSpinner = ArrayAdapter.createFromResource(
                    Assignment.this, R.array.delete_menu,
                    android.R.layout.simple_spinner_item);
            adapterSpinner
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinnerDelete.setAdapter(adapterSpinner);

            alertDialogBuilder.setTitle("Delete Data");
            alertDialogBuilder
                    .setMessage("Click yes to Delete Record!")
                    .setCancelable(false)
                    .setPositiveButton("Ok",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    if (spinnerDelete
                                            .getSelectedItemPosition() == 0) {

                                        Code.i = true;
                                        int len = mListView.getCount();
                                        SparseBooleanArray checked = mListView
                                                .getCheckedItemPositions();
                                        for (int i = 0; i < len; i++)
                                            if (checked.get(i)) {
                                                 File file = new File(MEDIAPATH);
                                                 boolean deleted = file.delete();                                                                                                                                   }

媒体路径包含

private static final String MEDIA_PATH = new String(
        Environment.getExternalStorageDirectory() + "/AudioRecorder/");

我尝试使用以下链接,但没有按照我想要的方式获得解决方案

How to delete a file from SD card?

Delete File in SD Card android with string

Delete file on sd card from a listview

Delete file on sd card from a listview

how to delete a particular file from the paticular folder in sdcard when click on check boxes in list view

How can I make a file to disappear once if it is deleted in android SD card?

4 个答案:

答案 0 :(得分:2)

类似的例子我发帖希望它可以帮到你。无论你在复选框上做什么,我正在删除按钮。如果要使用复选框,则必须保持所有复选框的状态或最后选择的复选框的状态。这是一个自定义适配器,我有覆盖getView方法。看看我如何在listview中按钮点击显示吐司,然后你就会明白。如果你仍然遇到任何问题,那么你可以问。

package com.example.list_view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class array_adapter extends ArrayAdapter<String> {
    Context context = null;
    ArrayList<String> file_list = null;
    public array_adapter(Context context, int textViewResourceId,
            ArrayList<String> objects) {
        super(context, textViewResourceId, objects);
        this.context = context;
        this.file_list = objects;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View row_view = convertView;

        if (row_view == null) {
            row_view = View.inflate(context, R.layout.list_layout, null);
            TextView text_view = (TextView) row_view.findViewById(R.id.textView1);
            Button button = (Button) row_view.findViewById(R.id.button1);

            button.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // POSITION YOU HAVE SO YOU CAN DO WHATEVER YOU WANT.
                    String file_name = file_list.get(position);
                    String MEDIA_PATH = new String(Environment.getExternalStorageDirectory() + "/AudioRecorder/" + file_name);
                    File file = new File(MEDIA_PATH);
                    file.delete();
                    file_list.remove(position);
                    notifyDataSetChanged()
                    // ATTACT FILE WITH CORRECT FILE PATH AND DELETE.
                    Toast.makeText(context, file_name, Toast.LENGTH_LONG).show();
                }
            });

            text_view.setText(file_list.get(position));
        }
        return row_view;
    }
}

如果您仍然遇到任何问题,那么您可以提问。

答案 1 :(得分:0)

基本上你不会删除音频或任何文件。正如您在问题中提到的那样,列表视图中的每一行都有Index or Position

所以,简单就是你必须删除该路径以及该文件。

File file = new File(selectedFilePath);
boolean deleted = file.delete();

示例:路径/sdcard/My_App/sample_audio.mp3

如果您检查的商品位置有问题,请浏览this

答案 2 :(得分:0)

在“删除”处理程序中,您应该执行以下操作:

  • 获取所选项目的位置,如您所知:

    SparseBooleanArray checked = mListView.getCheckedItemPositions();

  • 对于每个“已检查”的位置:

    1. 构建相关文件路径并删除该文件
    2. 从适配器中删除给定位置的相应数据

  • 更新适配器数据阵列/列表并减小尺寸后 在适配器上调用notifyDataSetChanged()以更新mListView

答案 3 :(得分:0)

刚刚更改了以下代码,现在它工作正常,但谢谢大家。

for (int i = 0; i < len; i++)
    if (checked.get(i)) {
          String delete = myList.get(i);
          File f = new File(MEDIA_PATH + "/"+ delete);
          if (f != null && f.exists()) {
          f.delete();
        }
     }