I am working on an application that saves images to a directory and then allows you to view those images. Currently I am able to save images and then view a list of the images within that directory. I want to be able to open an image in an imageview
when the user selects the image file from the list. Here is the code I currently have for the list:
public class Test extends ListActivity {
private List<String> fileList = new ArrayList<String>();
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
String root = Environment.getExternalStorageDirectory().toString();
File mapfiles= new File(root + "/Maps");
ListDir(mapfiles);
}
void ListDir(File f) {
File[] files = f.listFiles();
fileList.clear();
for (File file : files) {
//fileList.add(file.getPath());
fileList.add(file.getName());
}
ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fileList);
setListAdapter(directoryList);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
String item=fileList.get(position).toString();
Toast.makeText(Test.this, "You clicked on: "+item, Toast.LENGTH_SHORT).show();
}
}
I used toast just to verify that I could get something to happen when clicking on an item in the list, and that is working. The code I tried to use within the onItemListClick
is:
File selected = new File(fileList.get(position));
if(selected.isDirectory()){
ListDir(selected);
} else {
Uri selectedUri = Uri.fromFile(selected);
Intent notificationIntent = new Intent(Intent.ACTION_VIEW, selectedUri);
startActivity(notificationIntent);
}
That causes the app to crash and I'm not sure why. My question is what is incorrect in the above code? Or simply how can I view an image by selecting it from my list?
Thanks.