1:先獲得圖片的地址:
訪問收手機圖庫:
Intent intent= new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
獲取地址並顯示在ImageView中:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
selectPicturePath = cursor.getString(columnIndex);
cursor.close();
// String picturePath contains the path of selected Image
ImageView imageView = new ImageView(NewNoticeActivity.this);
LayoutParams lp =new LinearLayout.LayoutParams(150,150);
lp.rightMargin=20;
imageView.setLayoutParams(lp);
imageView.setBackgroundResource(R.drawable.border_grey2);
imageView.setPadding(2, 2, 2, 2);
imageView.setImageBitmap(Common.getImageThumbnail(selectPicturePath, 150, 150));
int index=ll_img.getChildCount();
ll_img.addView(imageView, index-1);
}
}
效果圖:
如果是在listview中顯示,先自定義適配器,正常SimpleAdapter可以放圖片,但是隻能是drawable裏面固定圖片,如果是手機圖庫裏任意一張圖片,或者網絡上這些不固定地址的圖片就不行了。所以要自定義adapter繼承SimpleAdapter。重寫裏面的setViewImage(ImageView v, String value){}函數。代碼如下:
public class NewsListAdapter extends SimpleAdapter {
Context context;
public NewsListAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
this.context = context;
}
// set the imageView using the path of image
public void setViewImage(ImageView v, int value) {
v.setImageResource(value);
}
@SuppressLint("NewApi")
public void setViewImage(ImageView v, String value) {
// LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 200);
// v.setLayoutParams(lp);
int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
v.measure(w, h);
int width =v.getMeasuredWidth();
int height =v.getMeasuredHeight();
System.out.println("---tupian : "+width+" , "+ height);
v.setImageBitmap(Common.getImageThumbnail(value, width, height));
// try {
// Bitmap bitmap = BitmapFactory.decodeFile(value);
// Bitmap newBit = Bitmap.createScaledBitmap(bitmap,
// v.getLayoutParams().width, v.getLayoutParams().height, true);
// v.setImageBitmap(newBit);
// } catch (NumberFormatException nfe) {
// v.setImageURI(Uri.parse(value));
// }
}
}
注:LayoutParams貌似只能設置寬高,不能獲取寬高,獲取的寬高都是0或者-2,不準確。如果是獲取寬高,還是用getMeasuredWidth()和getMeasuredHeight();
效果圖:
最後關於Common.getImageThumbnail()函數:
/**
* 根據指定的圖像路徑和大小來獲取縮略圖 此方法有兩點好處: 1.
* 使用較小的內存空間,第一次獲取的bitmap實際上爲null,只是爲了讀取寬度和高度,
* 第二次讀取的bitmap是根據比例壓縮過的圖像,第三次讀取的bitmap是所要的縮略圖。 2.
* 縮略圖對於原圖像來講沒有拉伸,這裏使用了2.2版本的新工具ThumbnailUtils,使 用這個工具生成的圖像不會被拉伸。
* @param imagePath 圖像的路徑
* @param width 指定輸出圖像的寬度
* @param height 指定輸出圖像的高度
* @return 生成的縮略圖
*/
public static Bitmap getImageThumbnail(String imagePath, int width, int height) {
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// 獲取這個圖片的寬和高,注意此處的bitmap爲null
try
{
bitmap = BitmapFactory.decodeFile(imagePath, options);
} catch (Exception e)
{
e.printStackTrace();
}
options.inJustDecodeBounds = false; // 設爲 false
// 計算縮放比
int h = options.outHeight;
int w = options.outWidth;
int beWidth = w / width;
int beHeight = h / height;
int be = 1;
if (beWidth < beHeight)
{
be = beWidth;
} else
{
be = beHeight;
}
if (be <= 0)
{
be = 1;
}
options.inSampleSize = be;
// 重新讀入圖片,讀取縮放後的bitmap,注意這次要把options.inJustDecodeBounds 設爲 false
bitmap = BitmapFactory.decodeFile(imagePath, options);
// 利用ThumbnailUtils來創建縮略圖,這裏要指定要縮放哪個Bitmap對象
bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
return bitmap;
}