Marco Seiz
Marco Seiz

Reputation: 954

Android: How to put an image file from sd card to HashMap with simpleadapter?

I am reading files from the selected folder on my phone like you can see in the following code. And how could I get this work with an Imagefile? At the end I like to have an imagelist with a preview of every image. Like that:

[IMG] (imgview) - [Filename] (String)

        for(int i=0; i < files.length; i++) {

        File file = files[i];
            map = new HashMap<String, String>();
        if(!file.isHidden() && file.canRead()) {
            path.add(file.getPath());

            if(file.isDirectory()) {
                map.put("img_list", ""+R.drawable.folder);
                map.put("string_cell", file.getName()+"/");
                your_array_list.add(map);

            }else{


                ImageFileFilter filefilter = new ImageFileFilter(file);

                if(filefilter.accept(file)){
                   //Its an imagefile

                    // ==> I like to replace the ""+R.drawable.image with the file that I have read
                    map.put("img_list", ""+R.drawable.image);


                } else {

                    //Its not an image file

                }

                map.put("string_cell", file.getName());
                your_array_list.add(map);

            }
        } 


    }

        SimpleAdapter mSchedule = new SimpleAdapter(this, your_array_list, R.layout.connected_upload_row,
            new String[] {"img_list", "string_cell"}, new int[] {R.id.img_list, R.id.string_cell});
list.setAdapter(mSchedule);

In the following picture I like to replace the white "image" picture with the original picture called "41786486733.jpg" as preview. So that the user can see what picture this is...

enter image description here

EDIT FLORIAN PILZ

if(filefilter.accept(file)){

                    Log.v("PATH1", file.getPath() );


                    ImageView myImageView = (ImageView) findViewById(R.id.img_list);
                    Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath());
                    myImageView.setImageBitmap(bmp); 



                }

enter image description here

Upvotes: 6

Views: 1636

Answers (4)

Marco Seiz
Marco Seiz

Reputation: 954

the solution for my problem is here:

How to show thumbnail from image path?

I had to create a customlistview (with help from ρяσѕρєя K) THANKS!!

create an custom adapter by extended baseadapter class instead of SimpleAdapter . as i think this will be easy or also you can create more custom UI instead of using inbuild

Upvotes: 0

RobinHood
RobinHood

Reputation: 10969

You have to implement custom listview check this example and seem storing path in hashmap creating issue,it will more helpful if you provide error log cat! anyhow can you try below trick to achieve you goal instead storing path in hashmap?,hope it will help to you.

File file = new File(Environment.getExternalStoragePath()+"/Folder/");

    file imageList[] = file.listFiles();

     for(int i=0;i<imageList.length;i++)
     {
       Log.e("Image: "+i+": path", imageList[i].getAbsolutePath());

       Bitmap bitmap = BitmapFactory.decodeFile(imageList[i].getAbsolutePath());
        myImageView.setImageBitmap(bitmap);

Upvotes: 1

neteinstein
neteinstein

Reputation: 17613

There is a similar question with an answer accepted. Check: Cannot open image file stored in sdcard

Snippet from there:

File directory = new File(extStorageDirectory, "myFolder");
File fileInDirectory = new File(directory, files[which]);
Bitmap bitmap = BitmapFactory.decodeFile(fileInDirectory.getAbsolutePath());

Upvotes: 1

Florian Pilz
Florian Pilz

Reputation: 337

Looking at the picture, and assuming that the white image with the text "IMAGE" is an ImageView instance, then simply...

Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath);
myImageView.setImageBitmap(bmp); 

Or did I completely misunderstood your question. Anyhow this small example tries to do something similar, what your are trying to accomplish.

Upvotes: 1

Related Questions