user2581076
user2581076

Reputation:

How to handle list of music files from sdcard

I want to handle all songs from SD card (like delete or rename them), so I am listing all music files in a listview but I do not know how to handle that list, here is what I tried so far:

// Use the current directory as title
path = "/sdcard/AudioRecorder/";

if (getIntent().hasExtra("path")) {
    path = getIntent().getStringExtra("path");
}

setTitle(path);

// Read all files sorted into the values-array
final List 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.startsWith(".")) {
            values.add(file);
        }
    }
}

Collections.sort(values);

// Put the data into the list
final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_2, android.R.id.text1, values);
setListAdapter(adapter);
myList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
        return onLongListItemClick(v, pos, id);
    }

    private boolean onLongListItemClick(View v, final int pos, long id) {
        // TODO Auto-generated method stub
        AlertDialog.Builder builder = new AlertDialog.Builder(ListFileActivity.this);
        builder.setMessage("Are you sure you want to exit?")
            .setCancelable(false)
            .setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        File file = new File(path+filename);
                        file.delete();
                        values.remove(pos);
                        adapter.notifyDataSetChanged();

                    }
                })

            .setNegativeButton("No",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });

        AlertDialog alert = builder.create();
        alert.show();

        return true;
    }
});

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    filename = (String) getListAdapter().getItem(position);
    Toast.makeText(getApplicationContext(), filename, Toast.LENGTH_LONG).show();
}

Here I am able to delete the list item but file not deleting on SD card for renaming give me little idea how to approach.

Upvotes: 1

Views: 321

Answers (1)

Rohan Kandwal
Rohan Kandwal

Reputation: 9336

For deleting the file use the following

File file = new File("absolute_path_of_filename");
// check if file is not a directory
        if(!file.isDirectory())      {
// if not then delete it
            file.delete();
        }

For renaming use file.renameTo("filename with path");

Upvotes: 1

Related Questions