ImageView iv= (ImageView)this.findViewById(R.id.myImageView1);
1.
iv.setImageResource(R.drawable.icon)
2.
iv.setImageDrawable(getResources().getDrawable(R.drawable.icon)
setImageResource是同步的,资源图片的读取和解码都是在主线程中进行的。setImageDrawable是异步的。加载速度的区别。setImageResource要快于setImageDrawable和setImageBitmap.
3.
String fileName = "/data/data/com.test/aa.png;
Bitmap bm = BitmapFactory.decodeFile(fileName);
也可以=BitmapFactory.decodeResource(getResources(),R.drawable.location_marker));
iv.setImageBitmap(bm);
4.
iv.setImageResource(iv[position]);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(136,88));
5.从SD卡上获得路径URI变成Bitmap
Uri uri = data.getData();
System.out.println(uri.getPath());
ContentResolver cr = this.getContentResolver();
bmp = MediaStore.Images.Media.getBitmap(cr, uri); photo.setImageBitmap(bmp);
第二种方式
ContentResolver resolver=getContentResolver();
InputStream is=resolver.openInputStream(tempUri);
bitmap=BitmapFactory.decodeStream(is);
PS
由相机或图库中的图片bitmap与uri互相转换
Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, null,null));
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
将文件转为URI
Uri.parse("file://"+ file.getAbsolutePath())
或者Uri.fromFile(file);
将URI转为文件
1.private File uri2File(Uri uri) {
2. File file = null;
3.String[] proj = { MediaStore.Images.Media.DATA };
4.Cursor actualimagecursor = getActivity().managedQuery(uri, proj, null, null, null);
5. int actual_image_column_index = actualimagecursor
6. .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
7. actualimagecursor.moveToFirst();
8. String img_path = actualimagecursor
9. .getString(actual_image_column_index);
10. file = new File(img_path);
11. return file;
12. }
6.通过网络路径获取图片
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5555);// 设置连接网络超时时间
conn.setRequestMethod("GET");// 设置 URL 请求的方法,
if (conn.getResponseCode() == 200)// 从 HTTP 响应消息获取状态码 HTTP/1.0 200(成功) HTTP/1.0 401 Unauthorized(无法识别)
InputStream is = conn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);