package com.example.android.filemanagement; import java.io.File; import java.io.FilenameFilter; import java.util.Arrays; import java.util.Iterator; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.Button; import android.widget.TextView; public class FileCleanupActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button b = (Button) findViewById(R.id.go); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Delete those files! Iteratorit = listFiles().iterator(); while (it.hasNext()){ it.next().delete(); } } }); TextView tv = (TextView) findViewById(R.id.file_list); StringBuffer sb = new StringBuffer(); Iterator it = listFiles().iterator(); while (it.hasNext()){ sb.append(it.next().getName() + "\n"); } tv.setText(sb.toString()); } private static List listFiles(){ File[] files = Environment.getExternalStorageDirectory() .listFiles(new ExtensionFilter("jpg")); return Arrays.asList(files); } } class ExtensionFilter implements FilenameFilter { private String ext; public ExtensionFilter(String ext){ this.ext = "."+ext; } @Override public boolean accept(File dir, String filename) { return filename.endsWith(ext); } }
Monday, August 8, 2011
Deleting Files from Android SDCard
Today I needed to delete a bunch of files off of my phone because some rogue test program I was running generated several hundred image files in the root of my SDCard. Here is program I wrote to get rid of them:
Subscribe to:
Post Comments (Atom)
where is xml layout. I want To delete one file
ReplyDelete