当前位置: 代码迷 >> Android >> Androd提选相册照片和拍照处理-android学习之旅(62)
  详细解决方案

Androd提选相册照片和拍照处理-android学习之旅(62)

热度:95   发布时间:2016-04-27 23:36:38.0
Androd选取相册照片和拍照处理-android学习之旅(62)

实现如下图所示效果

这里写图片描述

核心代码 -构建打开相册和拍照的Intent

拍照

 File outputImage = new File(Environment.getExternalStorageDirectory(),"tempImage.jpg");                try {                    if (outputImage.exists()){                        outputImage.delete();                    }                    outputImage.createNewFile();                }catch (Exception e){                    e.printStackTrace();                }                imageUri = Uri.fromFile(outputImage);                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");                intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);                startActivityForResult(intent,TAKE_PHOTO);

打开相册

File outputFile = new File(Environment.getExternalStorageDirectory(),"output_image.jpg");                try {                    if (outputFile.exists()){                        outputFile.delete();                    }                    outputFile.createNewFile();                }catch (Exception e){                    e.printStackTrace();                }                imageUri = Uri.fromFile(outputFile);                Intent intent = new Intent("android.intent.action.GET_CONTENT");                intent.setType("image/*");                intent.putExtra("crop",true);                intent.putExtra("scale",true);                intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);                startActivityForResult(intent,CROP_PHOTO);

整体代码

package com.example.euler_kalvinhe.takephoto;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.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.ImageView;import java.io.File;public class MainActivity extends Activity {    private static final int TAKE_PHOTO = 1;    private static final int CROP_PHOTO = 2;    private Button takePhoto;    private ImageView picture;    private Uri imageUri;    private Button choose;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        takePhoto = (Button) findViewById(R.id.button);        picture = (ImageView) findViewById(R.id.image);        choose = (Button) findViewById(R.id.choosePhoto);        choose.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                File outputFile = new File(Environment.getExternalStorageDirectory(),"output_image.jpg");                try {                    if (outputFile.exists()){                        outputFile.delete();                    }                    outputFile.createNewFile();                }catch (Exception e){                    e.printStackTrace();                }                imageUri = Uri.fromFile(outputFile);                Intent intent = new Intent("android.intent.action.GET_CONTENT");                intent.setType("image/*");                intent.putExtra("crop",true);                intent.putExtra("scale",true);                intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);                startActivityForResult(intent,CROP_PHOTO);            }        });        takePhoto.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                File outputImage = new File(Environment.getExternalStorageDirectory(),"tempImage.jpg");                try {                    if (outputImage.exists()){                        outputImage.delete();                    }                    outputImage.createNewFile();                }catch (Exception e){                    e.printStackTrace();                }                imageUri = Uri.fromFile(outputImage);                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");                intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);                startActivityForResult(intent,TAKE_PHOTO);            }        });    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        switch (requestCode){            case TAKE_PHOTO:                if (resultCode == RESULT_OK){                    Intent intent = new Intent("com.android.camera.action.CROP");                    intent.setDataAndType(imageUri,"image/*");                    intent.putExtra("scale",true);                    intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);                    startActivityForResult(intent,CROP_PHOTO);                }                break;            case CROP_PHOTO:                if (resultCode == RESULT_OK){                    try{                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));                        picture.setImageBitmap(bitmap);                    }catch (Exception e){                        e.printStackTrace();                    }                }                break;            default:                break;        }    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

版权声明:本文为博主原创文章,未经博主允许不得转载。

  相关解决方案