开发者

How to get all the pictures from the sdcard of emulator and display it in a listView?

I have a folder called images in the sdcard of my emulator. This folder contains pictures clicked from my application. I开发者_运维技巧 want to display all the pictures from that folder to a listview. How can i do that? Thanks in advance.


This should get you started with it.

http://android-er.blogspot.com/2010/01/android-file-explorer-with-jpgs-exif_08.html

A simple logic to calculate the requirments,

 private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);

item = new ArrayList<String>();
path = new ArrayList<String>();

File f = new File(dirPath);
File[] files = f.listFiles();

if(!dirPath.equals(root))
{

 item.add(root);
 path.add(root);

 item.add("../");
 path.add(f.getParent());

}

for(int i=0; i < files.length; i++)
{
  File file = files[i];
  path.add(file.getPath());
  if(file.isDirectory())
   item.add(file.getName() + "/");
  else
   item.add(file.getName());
}

ArrayAdapter<String> fileList =
 new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
 }


I haven't tried it, but I suppose if you could be able to create IMageView by putting the images from SD card, you can be able to do this. You can get the images from SD card by creating ContentResolver cursor by passing MediaStore.Images.Media.EXTERNAL_CONTENT_URI and then creating imageview by passing the URI and id (from cursor) to it. After you create ImageView you can create an array of it and pass it to the ArrayAdapter which will then be used to set the data to your ListView. I hope this will help you to solve the problem.


ArrayList<File> imageReader(File root) {
        ArrayList<File> a = new ArrayList<>();

        File[] files = root.listFiles();
        for(int i=0; i < files.length; i++) {
            if(files[i].isDirectory()) {
                a.addAll(imageReader(files[i]));
            }
            else {
                if(files[i].getName().endsWith(".jpg")) {
                    a.add(files[i]);
                }
            }
        }
        return a;
    }

give your root directory as argument, add all the extensions you want to retrieve.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜