應用啟動系統相機拍照,拍攝完成後自動進入裁剪介面,裁剪確認後在 ImageView 中顯示結果。本文提供基於 Intent 協調相機、媒體掃描、系統裁剪 Activity 的完整方案。
MediaStore.EXTRA_OUTPUT)startActivityForResult 啟動相機,請求碼 CAMERA_RESULT_CUTcom.android.camera.action.CROP),配置輸出尺寸、比例、返回數據public class MainActivity extends Activity {
private Button mButton;
private ImageView mImageView;
private File mPhotoFile;
private String mPhotoPath;
private Uri mPhotoOnSDCardUri;
public static final int CAMERA_RESULT_CUT = 888;
public static final int CAMERA_RESULT_CUT_OVER = 999;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = findViewById(R.id.button);
mImageView = findViewById(R.id.imageView);
mButton.setOnClickListener(v -> dispatchTakePictureIntent());
}
private void dispatchTakePictureIntent() {
try {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mPhotoPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
+ "/Camera/" + getPhotoFileName();
mPhotoFile = new File(mPhotoPath);
if (!mPhotoFile.exists()) mPhotoFile.createNewFile();
mPhotoOnSDCardUri = Uri.fromFile(mPhotoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoOnSDCardUri);
startActivityForResult(intent, CAMERA_RESULT_CUT);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "創建圖片文件失敗", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) return;
if (requestCode == CAMERA_RESULT_CUT) {
handlePhotoTaken();
} else if (requestCode == CAMERA_RESULT_CUT_OVER) {
handleCropResult(data);
}
}
private void handlePhotoTaken() {
// 1. 發送媒體掃描廣播,讓系統媒體庫識別新文件
Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mPhotoOnSDCardUri);
sendBroadcast(scanIntent);
// 2. 等待掃描完成(簡易做法,生產環境建議用 ContentObserver 監聽)
try { Thread.sleep(2000); } catch (InterruptedException ignored) {}
// 3. 查詢 MediaStore 獲取 Content URI
Uri systemImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(systemImageUri, null,
MediaStore.Images.Media.DISPLAY_NAME + "=?", new String[]{mPhotoFile.getName()}, null);
Uri photoUriInMedia = null;
if (cursor != null && cursor.moveToLast()) {
long id = cursor.getLong(0);
photoUriInMedia = ContentUris.withAppendedId(systemImageUri, id);
cursor.close();
}
if (photoUriInMedia == null) {
Toast.makeText(this, "無法獲取圖片 URI", Toast.LENGTH_SHORT).show();
return;
}
// 4. 啟動系統裁剪
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(photoUriInMedia, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("outputX", 250);
cropIntent.putExtra("outputY", 250);
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("return-data", true); // 返回 Bitmap 數據而非文件
startActivityForResult(cropIntent, CAMERA_RESULT_CUT_OVER);
}
private void handleCropResult(Intent data) {
if (data != null) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap bitmap = extras.getParcelable("data");
if (bitmap != null) {
mImageView.setImageBitmap(bitmap);
}
}
}
}
private String getPhotoFileName() {
return new SimpleDateFormat("'IMG'_yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()) + ".jpg";
}
}
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="點擊拍照並裁剪" />
<ImageView
android:id="@+id/imageView"
android:layout_below="@id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop" />
</RelativeLayout>
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-feature android:name="android.hardware.camera" android:required="true" />
ContentObserver 監聽 MediaStore.Images.Media.EXTERNAL_CONTENT_URI 變化。MediaStore.EXTRA_OUTPUT)。FileProvider 生成 content:// URI,不可直接用 file://。本文展示了從拍照到裁剪顯示的標準 Intent 協作流程,代碼結構清晰、邏輯完整。實際項目中請根據目標 Android 版本與設備兼容性,靈活選擇系統裁剪或第三方庫方案。