拍照并且剪切

package com.example.nineteen;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * @author HD
 * @date 2015-12-7
 * @package_name com.example.nineteen
 * @file_name MainActivity.java
 */
public class MainActivity extends Activity implements OnClickListener {

    private Button btnTakePhone;

    private Button btnReceiveAlbum;

    private ImageView ivImage;

    private Uri pictureUri;

    public static final int TAKE_PHONE = 0;

    public static final int CROP_PHONE = 1;

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

        btnTakePhone = (Button) findViewById(R.id.btnTakePhone);
        ivImage = (ImageView) findViewById(R.id.ivImage);
        btnReceiveAlbum = (Button) findViewById(R.id.btnReceiveAlbum);
        btnTakePhone.setOnClickListener(this);
        btnReceiveAlbum.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO 自动生成的方法存根
        File file = new File(Environment.getExternalStorageDirectory(),
                "myFirstPicture.jpg");
        try {
            if(file.exists()){
                file.delete();
            }
            file.createNewFile();
        } catch (Exception e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        pictureUri = Uri.fromFile(file);
        switch (v.getId()) {
        case R.id.btnTakePhone:
            Intent intent = new Intent();
            // 包名+action.描述
            intent.setAction("android.media.action.IMAGE_CAPTURE");
            // 指定的拍照之后文件的输出路径
            intent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);
            startActivityForResult(intent, TAKE_PHONE);
            break;

        default:
            break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO 自动生成的方法存根
        switch (requestCode) {
        case TAKE_PHONE:
            if (resultCode == RESULT_OK) {
                Toast.makeText(MainActivity.this, "拍照成功", Toast.LENGTH_SHORT)
                        .show();
                Intent intent = new Intent();
                intent.setAction("com.android.camera.action.CROP");
                // 设置对应文件的MIME类型,用来打开文件时的标识
                intent.setDataAndType(pictureUri, "image/*");
                // 告知需要对文件进行剪切
                intent.putExtra("scale", true);
                startActivityForResult(intent, CROP_PHONE);
            }
            break;

        case CROP_PHONE:
            Log.i("hhhd", "crop phone");
            if (resultCode == Activity.RESULT_OK) {
                try {
//                  通过内容提供者打开一个文件输入流,然后进行解码成Bitmap对象
                    Bitmap bitmap = BitmapFactory
                            .decodeStream(getContentResolver().openInputStream(
                                    pictureUri));
                    ivImage.setImageBitmap(bitmap);
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
            break;

        default:
            break;
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.nineteen"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.nineteen.MainActivity" >

    <Button 
        android:id="@+id/btnTakePhone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="拍照"/>

    <ImageView 
        android:id="@+id/ivImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>

    <Button
        android:id="@+id/btnReceiveAlbum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/btnTakePhone"
        android:layout_alignBottom="@+id/btnTakePhone"
        android:layout_marginLeft="22dp"
        android:layout_toRightOf="@+id/btnTakePhone"
        android:text="获取相册相片" />

</RelativeLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值