安卓开发———打开相机拍照或者打开相册选择照片并显示出来

打开相机

测试UI

布局代码

<Button
    android:id="@+id/take_photo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="打开相机"/>

<Button
    android:id="@+id/chose_photo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="选择本地照片"/>


<ImageView
    android:id="@+id/picture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    />

获取控件并绑定事件

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    take_photo=findViewById(R.id.take_photo);
    chose_photo=findViewById(R.id.chose_photo);
    picture=findViewById(R.id.picture);


    take_photo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setTake_photo();
        }
    });

    chose_photo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           setChose_photo();
        }

    });
}

启动照相机

private void setTake_photo(){
    //创建file对象储存拍摄到的照片,将图片命名为output_image.jpg,将他存储在sd卡的关联目录下,调用getExternalCacheDir()
    //方法可以获得这个目录
    File outputImg=new File(getExternalCacheDir(),"output_image.jpg");
    try {
        if (outputImg.exists()){
            outputImg.delete();
        }
        outputImg.createNewFile();
    }catch (IOException e){
        e.printStackTrace();
    }

    //判断系统版本,低于7.0会将file对象转换为uir对象否则调用getUriForFile将file对象转化为一个封装过的uir对象
    //因为7.0开始直接使用本地真实路径会被认为是不安全的会抛出FileUirExposeption异常,FileProvider是一个
    //内容提供器会将封装的uir提供给外部
    if (Build.VERSION.SDK_INT>=24){
        imgUri= FileProvider.getUriForFile(MainActivity.this,"com.example.cameraalbumtest.fileprovider",outputImg);
        String adb=imgUri.toString();

    }else {
        imgUri=Uri.fromFile(outputImg);
    }
    Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");

           /* 先来说下intent的作用,intent是Android程序中各组件之间进行交互的一种重要方式,一般被用来启动活动、启动服务以及发送广播等;
           intent在启动Activity的时候可以传递数据,比如说给另一个Acti
  • 7
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
好的,以下是实现从相册选择照片显示Android代码: 1. 在Manifest文件中添加读取外部存储权限: ```xml <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> ``` 2. 在布局文件中添加一个ImageView用于显示选中的图片: ```xml <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dp" android:src="@drawable/ic_launcher_background" /> ``` 3. 在Activity或Fragment中添加以下代码实现从相册选择照片显示: ```java // 声明一个用于返回结果的常量 private static final int REQUEST_CODE_PICK_IMAGE = 1001; // 点击按钮选择照片 findViewById(R.id.buttonPickImage).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 创建Intent对象,设置类型为image/* Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent.setType("image/*"); // 启动Intent,选择照片 startActivityForResult(Intent.createChooser(intent, "选择照片"), REQUEST_CODE_PICK_IMAGE); } }); // 处理选择照片的结果 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == RESULT_OK && data != null) { // 获取选中的照片的URI Uri uri = data.getData(); // 将URI转换成Bitmap并显示在ImageView中 try { Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri); imageView.setImageBitmap(bitmap); } catch (IOException e) { e.printStackTrace(); } } } ``` 以上代码中,我们首先声明了一个常量用于返回结果,然后在点击按钮时创建了一个Intent对象,设置类型为image/*,启动Intent,选择照片。最后在处理选择照片的结果时,获取选中的照片的URI,将URI转换成Bitmap并显示在ImageView中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值