图库和拍照功能

转别人的,自己学习下

java代码

package com.example.test;

import java.io.ByteArrayOutputStream;
import java.io.File;

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

public class MainActivity extends Activity {

    public final static int PHOTO_ZOOM = 0;
    public final static int TAKE_PHOTO = 1;
    public final static int PHOTO_RESULT = 2;
    public static final String IMAGE_UNSPECIFIED = "image/*";
    private String imageDir;
private ImageView avatar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        avatar = (ImageView) findViewById(R.id.avatar);
       // 本地图库选择按钮
        LinearLayout upload = (LinearLayout)findViewById(R.id.select_button);
        upload.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType(IMAGE_UNSPECIFIED);
            Intent wrapperIntent=Intent.createChooser(intent, null);
            startActivityForResult(wrapperIntent, PHOTO_ZOOM);
            }
        });

        // 拍照按钮
        LinearLayout takePhoto=(LinearLayout)findViewById(R.id.photo_button);
        takePhoto.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
                imageDir = "temp.jpg";
                Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(new        File(Environment.getExternalStorageDirectory(), imageDir)));
                startActivityForResult(intent, TAKE_PHOTO);
            }
        });
}

        // 图片缩放
        public void photoZoom(Uri uri) {
            Intent intent = new Intent("com.android.camera.action.CROP");
            intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
            intent.putExtra("crop", "true");
            // aspectX aspectY 是宽高的比例
            intent.putExtra("aspectX", 1);
            intent.putExtra("aspectY", 1);
            // outputX outputY 是裁剪图片宽高
            intent.putExtra("outputX", 250);
            intent.putExtra("outputY", 250);
            intent.putExtra("return-data", true);
            startActivityForResult(intent, PHOTO_RESULT);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode == RESULT_OK) {
                if (requestCode == PHOTO_ZOOM) {
                    photoZoom(data.getData());
                }
                if (requestCode == TAKE_PHOTO) {
                    File picture = new File(Environment.getExternalStorageDirectory() + "/" + imageDir);
                    photoZoom(Uri.fromFile(picture));
                }

                if (requestCode == PHOTO_RESULT) {
                    Bundle extras = data.getExtras();
                    if (extras != null) {
                        Bitmap photo = extras.getParcelable("data");
                            ByteArrayOutputStream stream = new ByteArrayOutputStream();
                            photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);
                            avatar.setImageBitmap(photo);
                        }
                    }
                }
                super.onActivityResult(requestCode, resultCode, data);
        }

}

xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#fcf3ef"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:paddingTop="30dp" >

    <ImageView
        android:id="@+id/avatar"
        android:layout_width="130dp"
        android:layout_height="130dp"
        android:background="@drawable/avatar_frame" />


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:gravity="center_horizontal" >
<!-- 图库 -->
        <LinearLayout
            android:id="@+id/select_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:layout_width="54dp"
                android:layout_height="54dp"
                android:background="@drawable/local" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="本地选择"
                android:textColor="#000000"
                android:textSize="20sp" />
        </LinearLayout>
<!-- 拍照 -->
        <LinearLayout
            android:id="@+id/photo_button"
            android:layout_width="126dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="50dp"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:layout_width="57dp"
                android:layout_height="53dp"
                android:background="@drawable/take_photo" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="拍照"
                android:textColor="#000000"
                android:textSize="20sp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值