Android呼叫系統相機和相簿
阿新 • • 發佈:2019-01-09
拍照和相簿的功能在實際開發中是最常見的功能,這裡記錄下。
準備工作
許可權
1234 | <!-- 往SDCard寫入資料許可權 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!--請求訪問使用照相裝置--><uses-permission android:name="android.permission.CAMERA" /> |
常量
1234567 |
public final static int ALBUM_REQUEST_CODE = 1 |
相機
1234567891011121314151617181920 |
// 指定相機拍攝照片儲存地址String state = Environment.getExternalStorageState();if |
onActivityResult
拿到cameraPath,就隨便你搞了。
12345678 | @Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == Activity.RESULT_OK) { if (requestCode == CAMERA_REQUEST_CODE) { LogUtil.d("path=" + cameraPath); } } |
不需要自定義路徑,預設為Environment.DIRECTORY_PICTURES
1234567891011121314151617181920212223242526272829303132 | /** * 啟動相機 */ public static String startCamera(Activity activity, int requestCode) { // 指定相機拍攝照片儲存地址 String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { Intent intent = new Intent(); // 指定開啟系統相機的Action intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); File outDir = Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); if (!outDir.exists()) { outDir.mkdirs(); } File outFile = new File(outDir, System.currentTimeMillis() + ".jpg"); // 把檔案地址轉換成Uri格式 Uri uri = Uri.fromFile(outFile); LogUtil.d("getAbsolutePath=" + outFile.getAbsolutePath()); // 設定系統相機拍攝照片完成後圖片檔案的存放地址 intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); // 此值在最低質量最小檔案尺寸時是0,在最高質量最大檔案尺寸時是1 intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); activity.startActivityForResult(intent, requestCode); return outFile.getAbsolutePath(); } else { Toast.makeText(activity, "請確認已經插入SD卡", Toast.LENGTH_LONG).show(); return null; } } |
相簿
1234 | Intent intent = new Intent(Intent.ACTION_PICK, null);intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");intent.setAction(Intent.ACTION_GET_CONTENT);startActivityForResult(intent, ALBUM_REQUEST_CODE); |
onActivityResult
呼叫系統相簿,然後通過Uri拿到圖片的絕對地址。
12345678910111213141516 | public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == Activity.RESULT_OK) { if (requestCode == AppConstants.ALBUM_REQUEST_CODE) { try { Uri uri = data.getData(); final String absolutePath= getAbsolutePath(mActivity, uri); LogUtil.d("path=" + absolutePath); } catch (Exception e) { e.printStackTrace(); } } } } |
getAbsolutePath方法
1234567891011121314151617181920212223 | public String getAbsolutePath(final Context context, final Uri uri) { if (null == uri) return null; final String scheme = uri.getScheme(); String data = null; if (scheme == null) data = uri.getPath(); else if (ContentResolver.SCHEME_FILE.equals(scheme)) { data = uri.getPath(); } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) { Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null); if (null != cursor) { if (cursor.moveToFirst()) { int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); if (index > -1) { data = cursor.getString(index); } } cursor.close(); } } return data;} |