【Android】本地图片选择(打开媒体库,选择图片)

在此调查中我要实现的是:点击Pictures按钮后,获取手机内所有图片,选择某一个图片,并显示到ImageView中。

 

应用范围: 图片上传时的图片选择  , 类似"浏览"。

 

效果:

 

 

 

所有的图片都会列出来,包括目录。

 

 

在Activity Action里面有一个“ACTION_GET_CONTENT”字符串常量,该常量让用户选择特定类型的数据,并返回该数据的URI.我们利用该常量,然后设置类型为“image/*”,就可获得android手机内的所有image。

 

 

main.xml :

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello"  
  11.     />  
  12.    <Button   
  13.         android:id="@+id/b01"  
  14.         android:layout_width="fill_parent"   
  15.         android:layout_height="wrap_content"   
  16.     />  
  17.     <ImageView  
  18.         android:id="@+id/iv01"  
  19.         android:layout_width="fill_parent"   
  20.         android:layout_height="wrap_content"   
  21.      />  
  22. </LinearLayout>  
 

 

Lesson_01_Pic.java:

 

  1. package com.yfz;  
  2. import java.io.FileNotFoundException;  
  3. import android.app.Activity;  
  4. import android.content.ContentResolver;  
  5. import android.content.Intent;  
  6. import android.graphics.Bitmap;  
  7. import android.graphics.BitmapFactory;  
  8. import android.net.Uri;  
  9. import android.os.Bundle;  
  10. import android.util.Log;  
  11. import android.view.View;  
  12. import android.widget.Button;  
  13. import android.widget.ImageView;  
  14. public class Lesson_01_Pic extends Activity {  
  15.     /** Called when the activity is first created. */  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.           
  21.         Button button = (Button)findViewById(R.id.b01);  
  22.         button.setText("选择图片");  
  23.         button.setOnClickListener(new Button.OnClickListener(){  
  24.             @Override  
  25.             public void onClick(View v) {  
  26.                 Intent intent = new Intent();  
  27.                 /* 开启Pictures画面Type设定为image */  
  28.                 intent.setType("image/*");  
  29.                 /* 使用Intent.ACTION_GET_CONTENT这个Action */  
  30.                 intent.setAction(Intent.ACTION_GET_CONTENT);   
  31.                 /* 取得相片后返回本画面 */  
  32.                 startActivityForResult(intent, 1);  
  33.             }  
  34.               
  35.         });  
  36.     }  
  37.       
  38.     @Override  
  39.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  40.         if (resultCode == RESULT_OK) {  
  41.             Uri uri = data.getData();  
  42.             Log.e("uri", uri.toString());  
  43.             ContentResolver cr = this.getContentResolver();  
  44.             try {  
  45.                 Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));  
  46.                 ImageView imageView = (ImageView) findViewById(R.id.iv01);  
  47.                 /* 将Bitmap设定到ImageView */  
  48.                 imageView.setImageBitmap(bitmap);  
  49.             } catch (FileNotFoundException e) {  
  50.                 Log.e("Exception", e.getMessage(),e);  
  51.             }  
  52.         }  
  53.         super.onActivityResult(requestCode, resultCode, data);  
  54.     }  
  55. }  
 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值