【问题标题】:Android: How to put an image file from sd card to HashMap with simpleadapter?Android:如何使用simpleadapter将图像文件从sd卡放入HashMap?
【发布时间】:2012-12-07 12:25:03
【问题描述】:

我正在从手机上的选定文件夹中读取文件,如下面的代码所示。 我怎么能用 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”的原始图片作为预览。 这样用户就可以看到这是什么图片了……

编辑弗洛里安皮尔兹

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



                }

【问题讨论】:

  • 你需要实现一个自定义的列表视图,这里看一个例子[链接][1][1]:stackoverflow.com/questions/10267103/…
  • 为什么你要创建两次HashMap实例map = new HashMap&lt;String, String&gt;();删除最后一个
  • 谜题我正在使用自定义列表视图 ;) ρяσѕρєя 每次你必须重新初始化 HashMap 否则它不起作用......
  • @MarcoSeiz : 把map = new HashMap&lt;String, String&gt;();放在`File file = files[i];`里面for循环和remove`map = new HashMap();`从每个地方和plz清楚地告诉我你正在解决的当前问题是什么
  • @ρяσѕρєя 我在我的问题中添加了一张图片来解释我想要什么。谢谢和欢呼:)

标签: android image listview hashmap simplecursoradapter


【解决方案1】:

看图,假设带有文字“IMAGE”的白色图片是一个ImageView实例,那么简单...

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

还是我完全误解了你的问题。 无论如何,这个small example 试图做类似的事情,你想要完成的事情。

【讨论】:

  • 这不起作用...当我使用您的代码执行此操作时,图像是空的,看起来像我编辑的答案中的第二张图片...
【解决方案2】:

有一个类似的问题已接受答案。检查: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());

【讨论】:

  • 尝试发送到日志这个:fileInDirectory.getAbsolutePath() 如果它是正确的检查图像是否正常。
【解决方案3】:

您必须实现自定义列表视图检查此example 并似乎在哈希映射创建问题中存储路径,如果您提供错误日志猫会更有帮助!无论如何,您可以尝试以下技巧来实现您的目标,而不是将路径存储在 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);

【讨论】:

    【解决方案4】:

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

    How to show thumbnail from image path?

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-18
      • 2011-10-07
      • 2011-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多