0

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.

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
user2822397
  • 21
  • 1
  • 4
  • What errors are you getting? That should be your first step always. – Stephan Branczyk Nov 04 '13 at 00:18
  • Well I'm not able to run it with my device connected to my computer...if I do, it doesn't work because the memory of my device can't be accessed when it is connected via usb. I've been sending the apk file to my device and installing the application and running it, so I'm not able to view the error(s) in eclipse – user2822397 Nov 04 '13 at 00:22
  • @Mohom.R question is a couple of years old, I doubt there is still need for an answer, don't know why vrund purohit edited this old post. – Frederic Klein Oct 17 '16 at 06:13
  • Possible duplicate of [Set Imageview to show image in sdcard?](http://stackoverflow.com/questions/6224710/set-imageview-to-show-image-in-sdcard) – Frederic Klein Oct 17 '16 at 06:17

0 Answers0