gpt4 book ai didi

android - 如果相机旋转,来自相机的图像为空

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:55:17 27 4
gpt4 key购买 nike

我正在向用户展示一个选择器,让用户在相册或相机之间进行选择以选择照片。如果我选择相机,那么一旦加载相机,我就会旋转拍摄风景照片,拍摄照片并单击完成,它返回到我的应用程序,但返回的图像为空。如果我不旋转相机,图像会正常返回。我错过了什么?我知道旋转会导致 Activity 重建,但为什么 onActivityResult 不包含正确的信息?这是我的 openImage Intent:

public void openImageIntent() {

// Determine Uri of camera image to save.
final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MyAppImages"
+ File.separator);
root.mkdirs();
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyy-hhmmss");
final String fname = String.format("%s.jpg", sdf.format(new Date()));
final File sdImageMainDirectory = new File(root, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);

// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}

// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");

// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[] {}));

startActivityForResult(chooserIntent, SELECT_PICTURE_REQUEST);
}

以及 onActivityResult 方法:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE_REQUEST) {
final boolean isCamera;
if (data == null) {
isCamera = true;
} else {
final String action = data.getAction();
if (action == null) {
isCamera = false;
} else {
isCamera = true;
}
}

Uri selectedImageUri;
if (isCamera) {
selectedImageUri = outputFileUri;
} else {
selectedImageUri = data == null ? null : data.getData();
}

if (imageDelegate != null) {
Log.e(TAG, "imageDelegate not null: " + imageDelegate);
imageDelegate.gotNewImageUri(selectedImageUri);
imageDelegate = null;

} else if (getSupportFragmentManager().findFragmentByTag("addofferdialog") != null) {
imageDelegate = (AddOfferFragment) getSupportFragmentManager().findFragmentByTag("addofferdialog");
Log.e(TAG, "imageDelegate is null but found fragment: " + imageDelegate);
Log.e(TAG, "Activity image: " + selectedImageUri);
imageDelegate.gotNewImageUri(selectedImageUri);
imageDelegate = null;
} else {
Log.e(TAG, "cannot find imageDelegate!!!!");
}

Log.e(TAG, "selectedImageUri: " + selectedImageUri);
}
}
}

最佳答案

只需将 outputFileUri 保存在 onSaveInstanceState 中并在 onRestoreInstanceState 中恢复它否则它将在方向更改中为空。

关于android - 如果相机旋转,来自相机的图像为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16958010/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com