Android 選擇圖片 拍照 並編輯圖片

調用系統圖庫並返回圖片的url:

Intent intentFromGallery = new Intent();
intentFromGallery.setType("image/*"); // 設置文件類型
intentFromGallery.setAction(Intent.ACTION_PICK);
startActivityForResult(intentFromGallery, IMAGE_REQUEST_CODE);

調用系統相機,傳遞圖片保存地址的文件參數:

// 判斷存儲卡是否可以用,可用進行存儲
if (Tools.hasSdcard()) {
    Intent intentFromCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File file = new File(Environment.getExternalStorageDirectory(), IMAGE_FILE_NAME);
    intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
    startActivityForResult(intentFromCapture,CAMERA_REQUEST_CODE);
}

獲取回傳的圖片url,調用系統編輯圖片的功能:

Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// 設置裁剪
intent.putExtra("crop", "true");
// aspectX aspectY 是寬高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪圖片寬高
intent.putExtra("outputX", 320);
intent.putExtra("outputY", 320);
intent.putExtra("return-data", true);
startActivityForResult(intent, 2);








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章