android開啟系統相機分別獲得原圖和縮圖

曉果部落格發表於2016-12-08

第一種:獲得縮圖:

開啟相機

  private void photo() {
        Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(openCameraIntent, TAKE_PICTURE);
    }

這裡使用data得到的只是縮圖,大小一般30k左右,一般用在手機展示如頭像這種小圖,想上傳圖片資料到伺服器就不合適了!

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case TAKE_PICTURE:
                String fileName = String.valueOf(System.currentTimeMillis());
                Bitmap bm = (Bitmap) data.getExtras().get("data");
                String path = FileUtils.saveBitmap(bm, fileName);
                if (!StringUtil.isBland(path)) {
                    String decodePath = FileUtils.saveBitmap(UIUtils.decodeBitmap(path), "decode" + fileName);
                    if (!StringUtil.isBland(decodePath)) {
                        selectImagesDecodePath.add(decodePath);
                        selectImagesPath.add(path);
                        showAdapter();
                    }
                }
                break;
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

第二種:獲得原圖

開啟相機

private void photo() {
    File dir = new File(Environment.getExternalStorageDirectory() + "/DataCollect/");
    if (!dir.exists()) dir.mkdirs();

    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    localTempImg = System.currentTimeMillis();
    localTempImgFileName = localTempImg + ".jpg";
    File f = new File(dir, localTempImgFileName);
    Uri u = Uri.fromFile(f);
    intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, u);
    startActivityForResult(intent, TAKE_PICTURE);
}

這裡的f.getAbsolutePath()就是原圖的路徑了:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case TAKE_PICTURE:
                File f = new File(Environment.getExternalStorageDirectory()
                        + "/DataCollect/" + localTempImgFileName);
                LogUtils.e(f.getAbsolutePath());
                if (!StringUtil.isBland(f.getAbsolutePath())) {
                    String decodePath = FileUtils.saveBitmap(UIUtils.decodeBitmap(f.getAbsolutePath()), "decode" + localTempImg);
                    if (!StringUtil.isBland(decodePath)) {
                        selectImagesDecodePath.add(decodePath);
                        selectImagesPoth.add(f.getAbsolutePath());
                        showAdapter();
                    }
                }
                break;
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

這裡可能用到他的Uri資料,

  Uri u=Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(), 
   f.getAbsolutePath(), null, null)); 

由相機或相簿中的圖片bitmap與uri互相轉換
1、bitmap to uri

Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, null,null));

2、uri to bitmap

Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);

許可權:

<uses-permission Android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

相關文章