Question

I am trying to make a file manager delete files from sdcard. There is only one path "/sdcard/Soundcast/" and I want to be able to delete files from it which are .mp4 and .3gp. Here's my code:

public class MainActivity extends ListActivity {

private List<String> items =null;

private void deleteFile()
{
    // TODO: Implement this method
}

@Override
public void onCreate(Bundle icicle)
{
    super.onCreate(icicle);
    setContentView(R.layout.main);

    getFiles(new File("/sdcard/Soundcast").listFiles()); }
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{ int selectedRow = (int)id; if (selectedRow == 0)
    { getFiles(new File("/sdcard/Soundcast").listFiles()); }
    else
    { File file = new File(items.get(selectedRow)); if (file.isDirectory())
        { getFiles(file.listFiles()); }
        else
        {
            new AlertDialog.Builder(this) .setTitle("Ability to open files will come in future updates.")
            .setMessage("Do You Want To Delete this recording?")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int button){
                    deleteFile();
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int button){
                }
            }).show();}
            }
            }
private void getFiles(File[] files)
{ items = new ArrayList<String>();

    items.add(getString(R.string.goto_root)); for (File file : files)
    { items.add(file.getPath()); }

    ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.file_list_row, items);
    setListAdapter(fileList); } }
Was it helpful?

Solution

As Waza_Be already states, all you need is file.delete(). So just delete your deleteFile method and instead of invoking it (in the onClick method of the positive dialog button), simply call file.delete() and you're done:

public void onClick(DialogInterface dialog, int button) {
    // deleteFile();     <-- not needed
    file.delete();   //  <-- do this instead
}

OTHER TIPS

You should use that code, or something similar:

{ File file = new File(items.get(selectedRow)); if (file.isDirectory())
    { getFiles(file.listFiles()); }
    else
    {
        new AlertDialog.Builder(this) .setTitle("Ability to open files will come in future updates.")
        .setMessage("Do You Want To Delete this recording?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int button){
                booolean success = file.delete()
                //Refresh UI and your list, so the deleted file is no more diplayed
            }
        })

And filtering by extension is not really complicated..

I don't understand where is your problem..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top