android调用系统相机拍照

权限
    
    
    
    
    
mainactivity.java
public class MainActivity extends AppCompatActivity {

    private ImageView iv;
    private static final int PHOTO_GRAPH = 1;// 拍照
    private static final int PHOTO_SELECT = 2;
    private static final int PHOTO_RESOULT = 3;// 结果
    private static final String IMAGE_UNSPECIFIED = "image/*";
    private Uri outputFileUri;
    Bitmap bitmap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.iv);
    }

    public void takePhoto(View v){
        File file = new File(
                Environment.getExternalStorageDirectory().getAbsolutePath(), "temp.jpg");
        outputFileUri = Uri.fromFile(file);
        Intent intent = new Intent();
        intent.setAction("android.media.action.IMAGE_CAPTURE");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(intent, PHOTO_GRAPH);
       // overridePendingTransition(R.animator.in,R.animator.out);

    }
    //实现从相册选取
    public void selectPhoto(View v){
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivityForResult(intent, PHOTO_SELECT);
    }


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if(requestCode==PHOTO_SELECT){
                if (data == null){
                    return;
                }else{
                    //用户从图库选择图片后会返回所选图片的Uri
                    Uri uri;
                    //获取到用户所选图片的Uri
                    uri = data.getData();
                 ///   bitmap = Bitmpcompress.compressBitmap(this,uri,iv);
                    iv.setImageBitmap(bitmap);
                   // saveBitmap();
                }
            }
            if (requestCode == PHOTO_GRAPH) {
                // 设置文件保存路径
                if (data != null) {;
                    Bitmap thumbnail = data.getParcelableExtra("data");
                    iv.setImageBitmap(thumbnail);
                } else {
                    int width = iv.getWidth();
                    int height = iv.getHeight();
                    BitmapFactory.Options faOptions = new BitmapFactory.Options();
                    faOptions.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(outputFileUri.getPath(), faOptions);
                    int imagewidth = faOptions.outWidth;
                    int imageheight = faOptions.outHeight;
                    //确定将图缩小多少!
                    int scaleFactor = Math.min(imagewidth / width, imageheight / height);
                    faOptions.inJustDecodeBounds = false;
                    faOptions.inSampleSize = scaleFactor;
                    faOptions.inPurgeable = true;
                    bitmap = BitmapFactory.decodeFile(outputFileUri.getPath(), faOptions);
                    iv.setImageBitmap(bitmap);
                }
            }
        } else {
            Log.i("gsg", "nonononono");
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

}

你可能感兴趣的:(android调用系统相机拍照)