Android开发笔记: 获取相机拍照图片和选择图片

 1 MainActivity.java

package as.cuanbo.photoapp;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
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.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 获取相机照片
 */
public class MainActivity extends Activity {

    private ImageView img_show_photo_yuantu,img_show_photo_suolve,img_show_photo_select;
    private TextView tv_path;
    String photoPath;
    Uri photoUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        //sd读写权限
        FileOperation.wrPermission(this);

        //初始化目录
        FileOperation.init_config_dir();

        img_show_photo_yuantu = (ImageView) findViewById(R.id.img_show_photo_yuantu);
        img_show_photo_suolve = (ImageView) findViewById(R.id.img_show_photo_suolve);
        img_show_photo_select = (ImageView) findViewById(R.id.img_show_photo_select);
        tv_path = (TextView) findViewById(R.id.tv_path);

    }

    /**
     * 按钮事件
     * @param v
     */
    public void startTakePhoto(View v){
        if (v == findViewById(R.id.btn_start_photo_yuantu)){
            //原图
            Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File photoFile= createImgFile1();
            photoUri = Uri.fromFile(photoFile);
            intent.putExtra(MediaStore.EXTRA_OUTPUT,photoUri);
            startActivityForResult(intent,100);
        }
        else if (v == findViewById(R.id.btn_start_photo_suolve)){
            //缩率图
            Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
           startActivityForResult(intent,101);

        }
        else if (v == findViewById(R.id.btn_start_photo_select)){
            //选择图片
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_PICK);
            intent.setType("image/*");
            startActivityForResult(intent,102);
        }
    }

    /**
     * activity 返回数据
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {

            if (requestCode == 100 ) {
                //原图
                Bitmap bitmap  = BitmapFactory.decodeFile(photoPath);
                img_show_photo_yuantu.setImageBitmap(bitmap);
            }
            if (requestCode == 101) {
                //缩略图
                Bundle bundle = data.getExtras();
                Bitmap bitmap = (Bitmap) bundle.get("data");
                img_show_photo_suolve.setImageBitmap(bitmap);
            }
            if (requestCode == 102) {
                //选择图片
                Uri uri = data.getData();
                Cursor cursor = getContentResolver().query(uri,null,null,null,null);
                if (cursor.moveToNext()){
                    int index = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
                    String path = cursor.getString(index);
                    Bitmap bitmap  = BitmapFactory.decodeFile(path);
                    img_show_photo_select.setImageBitmap(bitmap);
                }
            }
        }
    }

    /**
     * 自定义图片名,获取照片的file
     */
    private File createImgFile1(){
        //确定文件名
        String fileName="img_"+new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date())+".png";
        File dir;
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            dir=Environment.getExternalStorageDirectory();
        }else
        {
            dir=getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        }
        File tempFile=new File(dir,fileName);
        try{
            if(tempFile.exists()){
                tempFile.delete();
            }
            tempFile.createNewFile();
        }catch (IOException e){
            e.printStackTrace();
        }
        //获取文件路径
        photoPath=tempFile.getAbsolutePath();
        tv_path.setText(tempFile.exists()+":"+photoPath);
        return tempFile;
    }
}

 
 

 2 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="as.cuanbo.photoapp.MainActivity">

    <Button
        android:text="原图-拍照"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:id="@+id/btn_start_photo_yuantu"
        android:layout_marginTop="20dp"
        android:layout_alignParentLeft="true"
        android:onClick="startTakePhoto"/>

    <Button
        android:text="选择图片"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:id="@+id/btn_start_photo_select"
        android:layout_marginTop="20dp"
        android:layout_centerHorizontal="true"
        android:onClick="startTakePhoto"/>

    <Button
        android:text="缩率图-拍照"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:id="@+id/btn_start_photo_suolve"
        android:layout_marginTop="20dp"
        android:layout_alignParentRight="true"
        android:onClick="startTakePhoto"/>


    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#514d4d"
        android:id="@+id/img_show_photo_yuantu"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/btn_start_photo_yuantu"
         />

    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#514d4d"
        android:id="@+id/img_show_photo_select"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/btn_start_photo_select" />

    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#514d4d"
        android:id="@+id/img_show_photo_suolve"
        android:layout_alignParentRight="true"
        android:layout_below="@id/btn_start_photo_suolve" />


    <TextView
        android:text="path:"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/img_show_photo_yuantu"
        android:layout_alignLeft="@id/img_show_photo_yuantu"
        android:id="@+id/tv_path" />
</RelativeLayout>

 3 清单文件


  4 演示




















































猜你喜欢

转载自blog.csdn.net/taoerit/article/details/79899035