2012-12-07 49 views
6

我正在阅读手机上选定文件夹中的文件,就像您在以下代码中看到的一样。 我怎样才能得到这个工作与Imagefile? 最后,我喜欢用图像列表预览每个图像。 喜欢的是:Android:如何使用simpleadapter将SD卡中的图像文件添加到HashMap中?

[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

编辑弗洛里安皮尔茨

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

+0

你需要实现一个自定义列表视图,在这里看到的例子[链接] [1] [1 ]:http://stackoverflow.com/questions/10267103/add-a-new-item-to-a-listview/10267200#10267200 – pouzzler

+0

为什么你两次创建HashMap实例'map = new HashMap ();'删除最后一个 –

+0

益智游戏我与一个custon列表视图工作;) ρяσѕρєя每次当你有reinizialize HashMap的,否则它不工作... –

回答

0

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

How to show thumbnail from image path?

我不得不创建一个customlistview(与帮助下ρяσѕρєяK)谢谢!

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

1

看着这张照片,并假设白图像与文字“图像”是一个ImageView实例,然后简单地...

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

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

+0

这是行不通的......当我用你的代码执行它的图像是空的,它看起来像在我的编辑回答第二个图片... –

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()); 
+0

那不是工作压力太大...的ImageView的是空的... –

+0

尝试发送到日志本:fileInDirectory.getAbsolutePath(),如果它是正确的检查,如果图像是确定的。 – neteinstein

+0

这是正确的路径和图像是好的。 –

1

你要实现自定义列表视图中选中该example,似乎存储的HashMap路径创造的问题,它会如果您提供错误日志的猫更有帮助!无论如何,你可以尝试下面的技巧来实现你的目标,而不是在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); 
相关问题