android 通过uri截取指定大小的图片显示在imageview上
jopen
11年前
在gridview的adapter中设置
public class GridViewAdapter extends BaseAdapter { protected List<Map<String, Object>> mlist; protected Context mContext; protected LayoutInflater mInflater = null; public GridViewAdapter(Context context, List<Map<String, Object>> list) { mlist = list; mContext = context; mInflater = LayoutInflater.from(context); } @Override public int getCount() { // TODO Auto-generated method stub return mlist.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return mlist.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder viewHolder = null; if (null == convertView) { viewHolder = new ViewHolder(); convertView = mInflater.inflate(R.layout.item_gridview, null); viewHolder.imageButton = (ImageButton) convertView .findViewById(R.id.imageButton1); viewHolder.imageView = (ImageView) convertView .findViewById(R.id.imageView1); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } if (!mlist.get(position).get("drawable").equals("") && mlist.size() < 6) { viewHolder.imageView .setImageResource(R.drawable.app_panel_add_icon_normal); viewHolder.imageButton.setVisibility(View.GONE); } else { //自动裁剪出制定大小的图片显示在imageview上 ContentResolver contentProvider = mContext.getContentResolver(); Uri uri01 = (Uri)mlist.get(position).get("uri"); Bitmap mBitmap = null; try { Bitmap bmp = BitmapFactory.decodeStream(contentProvider.openInputStream(uri01)); mBitmap = Bitmap.createScaledBitmap(bmp, 100, 100, true); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } viewHolder.imageView.setImageBitmap(mBitmap); // viewHolder.imageView.setImageURI((Uri) (mlist.get(position) // .get("uri"))); viewHolder.imageButton.setVisibility(View.VISIBLE); } final int i = position; viewHolder.imageButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if (mlist.size() == 5 && mlist.get(4).get("drawable").equals("")) { Map<String, Object> map = new HashMap<String, Object>(); map.put("drawable", R.drawable.ic_launcher); mlist.add(map); } mlist.remove(i); GridViewAdapter.this.notifyDataSetChanged(); } }); return convertView; } static class ViewHolder { ImageView imageView; ImageButton imageButton; } }