Android:如何使用simpleadapter将sd卡中的图像文件放到HashMap中?

时间:2012-12-07 12:25:03

标签: android image listview hashmap simplecursoradapter

我正在阅读手机上所选文件夹中的文件,就像您在下面的代码中看到的那样。 我怎么能用Imagefile来完成这项工作? 最后,我喜欢有一个图像列表,其中包含每个图像的预览。 像那样:

[IMG](imgview) - [文件名](字符串)

        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);

在下面的图片中,我喜欢用原始图片“41786486733.jpg”替换白色“图像”图片作为预览。 这样用户就可以看到这是什么图片......

enter image description here

编辑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

4 个答案:

答案 0 :(得分:1)

查看图片,并假设带有“IMAGE”文本的白色图像是ImageView实例,那么只需...

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

或者我完全误解了你的问题。 无论如何,small example尝试做类似的事情,你想要完成的事情。

答案 1 :(得分:1)

有一个类似的问题,接受了答案。检查:Cannot open image file stored in sdcard

来自那里的片段:

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

答案 2 :(得分:1)

你必须实现自定义列表视图检查这个example并且似​​乎在hashmap创建问题中存储路径,如果你提供错误日志cat会更有帮助!无论如何你可以尝试下面的技巧来实现你的目标,而不是在hashmap中存储路径?希望它会对你有所帮助。

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);

答案 3 :(得分:0)

我的问题的解决方案在这里:

How to show thumbnail from image path?

我必须创建一个自定义列表视图(在ρяσѕρєяK的帮助下)感谢!!

通过扩展baseadapter类而不是SimpleAdapter创建自定义适配器。因为我认为这很容易,或者您也可以创建更多自定义UI而不是使用inbuild