Android关于ListView显示SD卡上的Bitmap的图片(图片压缩之后)



ListView 显示缩略图

[java]  view plain copy
  1. package com.lostinai;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import android.app.Activity;  
  9. import android.graphics.Bitmap;  
  10. import android.graphics.BitmapFactory;  
  11. import android.graphics.BitmapFactory.Options;  
  12. import android.os.Bundle;  
  13. import android.util.Log;  
  14. import android.view.View;  
  15. import android.widget.AdapterView;  
  16. import android.widget.ImageView;  
  17. import android.widget.ListView;  
  18. import android.widget.SimpleAdapter;  
  19. import android.widget.AdapterView.OnItemClickListener;  
  20. import android.widget.SimpleAdapter.ViewBinder;  
  21.   
  22. public class ListviewtutuActivity extends Activity {  
  23.     private ListView lv;  
  24.     public void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.home);  
  27.         lv = (ListView)findViewById(R.id.h_list_view);  
  28.         setViews();  
  29.         lv.setOnItemClickListener(new OnItemClickListener(){  
  30.             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
  31.                     long arg3) {  
  32.                 Log.e("lostinai","lostinai");  
  33.             }  
  34.         });  
  35.     }  
  36.     private void setViews() {         
  37.           
  38.         SimpleAdapter adapter = new SimpleAdapter(this, getDatas() ,R.layout.message_list  
  39.                 , new String[]{"icon","title","shortContent"}, new int[]{R.id.ml_icon,R.id.ml_title,R.id.ml_short_content});  
  40.         lv.setAdapter(adapter);  
  41.         adapter.setViewBinder(new ViewBinder() {    
  42.             public boolean setViewValue(View arg0, Object arg1,    
  43.                     String textRepresentation) {    
  44.                 if ((arg0 instanceof ImageView) & (arg1 instanceof Bitmap)) {    
  45.                     ImageView imageView = (ImageView) arg0;    
  46.                     Bitmap bitmap = (Bitmap) arg1;    
  47.                     imageView.setImageBitmap(bitmap);    
  48.                     return true;    
  49.                 } else {    
  50.                     return false;    
  51.                 }    
  52.             }    
  53.         });    
  54.     }  
  55.     private List<Map<String,Object>> getDatas() {  
  56.         List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();  
  57.         for (int i=1; i<=12; i++) {  
  58.             Map<String,Object> map = new HashMap<String,Object>();  
  59.              String imagePath = "/sdcard/"+2+".jpg";    
  60. //           图片压缩  
  61.             Bitmap bm = null;  
  62.             Options op = new Options();  
  63.             op.inSampleSize = 5;   
  64.             op.inJustDecodeBounds = false;  
  65.             bm = BitmapFactory.decodeFile(imagePath, op);  
  66.               
  67.             map.put("icon",bm);  
  68.             map.put("title""My Title "+i);  
  69.             map.put("shortContent""my short content "+i);  
  70.             list.add(map);  
  71.         }  
  72.         return list;  
  73.     }  
  74. }  

home.xml文件如下

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ListView   
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.       android:id="@+id/h_list_view"  
  5.       android:layout_width="fill_parent"  
  6.       android:layout_height="fill_parent"  
  7.     />  

message_list.xml如下

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="wrap_content"  
  5.   android:layout_height="wrap_content">  
  6.     <ImageView android:id="@+id/ml_icon"  
  7.         android:layout_width="50dp"  
  8.         android:layout_height="58dp"  
  9.         android:src="@drawable/ic_launcher"  
  10.         />  
  11.     <LinearLayout  
  12.       xmlns:android="http://schemas.android.com/apk/res/android"  
  13.       android:orientation="vertical"  
  14.       android:layout_width="wrap_content"  
  15.       android:layout_height="58dp"  
  16.       android:layout_weight="1">  
  17.       <TextView android:id="@+id/ml_title"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="25dp"  
  20.         android:textStyle="bold"  
  21.         android:textSize="20dp"  
  22.         />  
  23.       <TextView android:id="@+id/ml_short_content"  
  24.         android:layout_width="fill_parent"  
  25.         android:layout_height="35dp"  
  26.         />  
  27.     </LinearLayout>  
  28.     <TextView  
  29.         android:layout_width="30dp"  
  30.         android:layout_height="58dp"  
  31.         android:text=">"  
  32.         android:textSize="26dp"  
  33.         android:layout_gravity="right"  
  34.         android:gravity="center"  
  35.         />  
  36. </LinearLayout>  

ImageView 上显示缩略图

[java]  view plain copy
  1. //通过openRawResource获取一个inputStream对象  
  2.         InputStream inputStream = getResources().openRawResource(R.drawable.temp);  
  3.         //通过一个InputStream创建一个BitmapDrawable对象  
  4.         BitmapDrawable drawable = new BitmapDrawable(inputStream);  
  5.         //通过BitmapDrawable对象获得Bitmap对象  
  6.         Bitmap bitmap = drawable.getBitmap();  
  7.         //利用Bitmap对象创建缩略图  
  8.         bitmap = ThumbnailUtils.extractThumbnail(bitmap, 51108);  
  9.         //imageView 显示缩略图的ImageView  
  10.         imageView.setImageBitmap(bitmap);  

SimpleAdapter 支持Bitmap

Unable to decode stream: java.io.FileNotFoundException: /android.graphics.Bitmap open failed: ENOEN

Unable to decode stream: java.io.FileNotFoundException: /android.graphics.Bitmap(或是 imageview XXXXX   /          Drawable  )这类错误 可能是由于 使用的adapter不支持图形所导致的,需要从写Adapter    ;


============

现在刚学android   好多东西还不是很清楚,所以在上面这个问题上纠结了 好几天  ,我使用的是SimpleAdapter   ,而simpleAdapter  并不支持bitmap或是 imageview  默认的只是支持ImageView的id 

要想使用SimpleAdapter支持imageView  或  bitmap     

有两种方法:

一:

    

[java]  view plain copy
  1. SimpleAdapter listAdapter = new SimpleAdapter( 参数省略);  

[java]  view plain copy
  1. listAdapter.setViewBinder(new ViewBinder() {  
  2.                       
  3.                     @Override  
  4.                     public boolean setViewValue(View view, Object attentionList, String textRepresentation) {  
  5.                             // TODO Auto-generated method stub  
  6.                             if(view instanceof ImageView && attentionList instanceof Bitmap){  
  7.                                     ImageView iv=(ImageView)view;  
  8.                                     iv.setImageBitmap((Bitmap) attentionList);  
  9.                                     return true;  
  10.                             }else{  
  11.                                     return false;  
  12.                             }     
  13.                     }  
  14.             });  
二:就是从写  simpleadapter  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容使用说明 YOLO高分设计资源源码,详情请查看资源内容使用说明 YOLO高分设计资源源码,详情请查看资源内容使用说明 YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值