关闭

Android_通过Intent调用系统相机和相册回传bitmap

标签: android布局
675人阅读 评论(0) 收藏 举报
分类:

DO

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView 
        android:id="@+id/takePhoto_imageView"
        android:layout_margin="50dp"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:src="@drawable/ic_launcher"/>

    <Button 
        android:layout_marginTop="90px"
        android:id="@+id/takePhoto_bt1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="20sp"
        android:textStyle="bold"
        android:text="拍照"/>

    <Button 
        android:id="@+id/takePhoto_bt2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="20sp"
        android:textStyle="bold"
        android:text="从相册选择照片"/>
</LinearLayout>

MainActivity.java

package com.lxf;

import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

/**
 * @address BeiJing
 * @author LiXufei
 * @function  拍照取图相册取图
 */
public class cTakePhoto extends Activity{

    private ImageView imageView;
    private int TAKE_PHOTO=1;   //拍照
    private int GET_PHOTO=2;    //取照片

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.android_takephoto);
        //获取ImageView控件
        imageView = (ImageView) findViewById(R.id.takePhoto_imageView);

        /*
         * 拍照取图
         */
        findViewById(R.id.takePhoto_bt1).setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                //设置拍照意图
                Intent mIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(mIntent, TAKE_PHOTO);

            }
        });

        /*
         * 相册取图
         */
        findViewById(R.id.takePhoto_bt2).setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                startActivityForResult(intent, GET_PHOTO);

            }
        });

    }
    //接受回传值
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {  //回传值接受成功

            if (requestCode == TAKE_PHOTO) {    //拍照取图

                Bundle bundle = data.getExtras();   //获取data数据集合
                Bitmap bitmap = (Bitmap) bundle.get("data");        //获得data数据
                Log.i("TAG", "拍照回传bitmap:"+bitmap);
                imageView.setImageBitmap(bitmap);

            }

            if (requestCode == GET_PHOTO) {     //相册取图

                ContentResolver contentResolver = getContentResolver();
                try {

                    Bitmap bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(data.getData()));
                    Log.i("TAG", "从相册回传bitmap:"+bitmap);
                    imageView.setImageBitmap(bitmap);

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

            }

        }

    }

}
1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2977次
    • 积分:153
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:1篇
    • 译文:0篇
    • 评论:1条
    文章分类
    文章存档
    最新评论