安卓图片调用的几种方式

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

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);

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值