要求:

  1. 屏幕上显示一个按钮和图片视图控件。
  2. 点击“载入图片”按钮,将用户重定向到Android的图片库,在那里可以选择一个图片。
  3. 一旦图片被选中,图片将在主屏

工程名为ThePhotoFromSystem

  1. 2修改layout/main.xml布局文件:

        xmlns:tools="幕上的图片视图控件中

步骤:

1 创建一个android工程,http://schemas.android.com/tools"

 android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


          android:id="@+id/imgView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">
            android:id="@+id/buttonLoadPicture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:text="Load Picture"
        android:layout_gravity="center">



3在MainActivity完成业务逻辑

package cn.xzmd.diaoyong.photo;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private static int RESULT_LOAD_IMAGE = 10;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

 Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
 //为按钮设置点击事件
       buttonLoadImage.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View arg0) {
             //点击事件,而重定向到图片库
               Intent intent = new Intent(
                       Intent.ACTION_PICK,
                       android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            //这里要传一个整形的常量RESULT_LOAD_IMAGE到startActivityForResult()方法。
               startActivityForResult(intent, RESULT_LOAD_IMAGE);
           }
       });

}
//用户选择一张图片,onActivityResult()方法将会被调用,
 @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       super.onActivityResult(requestCode, resultCode, data);
//我们需要判断requestCode是否是我们之前传给startActivityForResult()方法的RESULT_LOAD_IMAGE,并且返回的数据不能为空
       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]);
           String picturePath = cursor.getString(columnIndex);
           cursor.close();
 
           ImageView imageView = (ImageView) findViewById(R.id.imgView);
           imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
 
       }
 
   }

}

效果如下

android 调用图库中选择的图片_第1张图片android 调用图库中选择的图片_第2张图片android 调用图库中选择的图片_第3张图片

更多相关文章

  1. android 图片溢出问题2...[geoway]
  2. android 单选按钮组的使用
  3. Android异步加载图片
  4. Android对图片局部扩大的实现
  5. android:动态创建多个按钮 并给每个按键添加监听事件
  6. 简单的中间文字两边按钮

随机推荐

  1. 求一段与读取数据库数据,写入一个数组的ja
  2. Tinking in java枚举类型的自动贩售机
  3. JAVA 静态方法和静态变量和final和※静态
  4. 基于JavaScript实现验证码功能
  5. JAVA: 第一个只出现1次的字符
  6. 在iOS上签名的数据无法在Java中验证
  7. 整理的关于Java对mongodb进行的CURD操作
  8. Memcached提升web数据操作读写性能
  9. 未能解决:com.google.android.gms:play-s
  10. java 访问控制修饰符总结详解