我有以下代码,用于创建从相机库中选择照片的意图:
public void onPhotoButtonClick(View view) {
File root = new File(Environment.getExternalStorageDirectory() + File.separator + "photos" + File.separator);
root.mkdirs();
String fileName = String.format("photo_%s.jpg", System.currentTimeMillis());
File sdImageMainDirectory = new File(root, fileName);
photoUri = Uri.fromFile(sdImageMainDirectory);
List<Intent> cameraIntents = new ArrayList<Intent>();
Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
PackageManager packageManager = getPackageManager();
List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for(ResolveInfo resolvedInfo : listCam) {
String packageName = resolvedInfo.activityInfo.packageName;
Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(packageName, resolvedInfo.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
cameraIntents.add(intent);
}
Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_PICK);
Intent chooserIntent = Intent.createChooser(galleryIntent, "Select source");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
startActivityForResult(chooserIntent, SELECT_PHOTO);
}
问题是当我用相机拍照时,它没有激活下面的onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, String.valueOf(requestCode));
Log.d(TAG, String.valueOf(resultCode));
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PHOTO) {
final boolean isCamera;
if (data == null) {
isCamera = true;
} else {
final String action = data.getAction();
isCamera = MediaStore.ACTION_IMAGE_CAPTURE.equals(data.getAction());
}
Uri selectedImageUri;
if (isCamera) {
selectedImageUri = photoUri;
} else {
selectedImageUri = data == null ? null : data.getData();
}
photoText.setText(selectedImageUri.toString());
Log.d(TAG, selectedImageUri.toString());
}
}
}
我的清单中有以下内容:
<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
我错过了什么吗?