Android从摄像头或相册中获取照片

Android从摄像头或相册中获取照片

关键代码:

    /**
     * 从相册中获取,返回结果会在onActivityResult()中
     */
    private void selectPicFromAlbum() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivityForResult(intent, RESULT_FROM_ALBUM);
    }

    /**
     * 从摄像头中获取,返回结果会在onActivityResult()中
     */
    private void selectPicFromCamera() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, RESULT_FROM_CAMERA);
    }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (null == data) return;
    switch (requestCode) {
        case RESULT_FROM_ALBUM:
            Uri imageUri = data.getData();

            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
                imageView.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }

            break;

        case RESULT_FROM_CAMERA:
            Bitmap bitmap = (Bitmap) data.getExtras().get("data");
            imageView.setImageBitmap(bitmap);
            break;
    }

}

具体代码:

package com.ashzheng.studydemo.demo7;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ashzheng.studydemo.R;

import java.io.IOException;

public class Demo7Activity extends Activity {


    private ImageView imageView;
    private TextView textView;

    public static final int RESULT_FROM_ALBUM = 1;
    public static final int RESULT_FROM_CAMERA = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo7);

        imageView = (ImageView) findViewById(R.id.demo7_iv);
        textView = (TextView) findViewById(R.id.demo7_tv);

        findViewById(R.id.demo7_bt1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectPicFromAlbum();
            }
        });

        findViewById(R.id.demo7_bt2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectPicFromCamera();
            }
        });
    }

    /**
     * 从相册中获取,返回结果会在onActivityResult()中
     */
    private void selectPicFromAlbum() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivityForResult(intent, RESULT_FROM_ALBUM);
    }

    /**
     * 从摄像头中获取,返回结果会在onActivityResult()中
     */
    private void selectPicFromCamera() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, RESULT_FROM_CAMERA);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (null == data) return;
        switch (requestCode) {
            case RESULT_FROM_ALBUM:
                Uri imageUri = data.getData();

                try {
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
                    imageView.setImageBitmap(bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                break;

            case RESULT_FROM_CAMERA:
                Bitmap bitmap = (Bitmap) data.getExtras().get("data");
                imageView.setImageBitmap(bitmap);
                break;
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值