Android onActivityResult在横向模式下捕获图像时返回null

时间:2015-10-09 08:48:21

标签: android android-camera

我有一个表单,用户可以点击图像并在imageview中设置。当手机保持纵向模式并单击图像时,一切正常,点击图像在图像视图中设置。问题是,如果用户在纵向模式下单击捕获图像按钮然后旋转手机以单击图像,则在onActivityResult中我得到位图null。 有没有解决方案。

这是我的onActivtiyResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    switch (requestCode) {
        case 1:
            try {
                    bitmap = AppUtils.decodeFile(file.getPath(), 100, 100);
                    registrationBean.setImage(file.getPath());
                    imageList.add(file.getPath());

                    AppUtils.writeList(this,imageList,"images");
            }catch (Exception e) {
                e.printStackTrace();
            }
            if (bitmap != null) {

                iv_profile.setImageBitmap(bitmap);
                retake_photo.setAlpha(1);
                retake_photo.setClickable(true);
                take_photo.setClickable(false);
                take_photo.setAlpha((float) 0.60);

            }


            break;
        case 2:
            try {
                String uriImage;
                imageUri = imageReturnedIntent.getData();
                final InputStream imageStream = getContentResolver().openInputStream(imageUri);

                final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                imageStream.close();
                String path = getPath(imageUri);
                imageList.add(path);
                AppUtils.writeList(getBaseContext(), imageList, "images");
                registrationBean.setImage(path);
                if (selectedImage != null) {
                    iv_profile.setImageBitmap(selectedImage);
                    retake_photo.setAlpha(1);
                }
                uriImage = imageUri.toString();
                registrationBean.setImage(uriImage);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            catch (OutOfMemoryError e)
            {
                e.printStackTrace();
            }
            break;
    }
}

调用startActivityForResult的方法

private void selectImage() {
    final CharSequence[] options = {"Take a Picture", "Choose from Gallery", "Cancel"};
    AlertDialog.Builder builder = new AlertDialog.Builder(ActivityForm.this);
    builder.setCancelable(false);

    builder.setTitle("Select Profile Photo!");
    builder.setItems(options, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            if (options[item].equals("Take a Picture")) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                i = appSharedPreference.getImageNumber();
                file = new File(Environment.getExternalStorageDirectory(), "temp" + i + ".jpg");
                i++;
                appSharedPreference.setImageNumber(i);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                img_path = Environment.getExternalStorageDirectory() + "/temp.jpg";
                startActivityForResult(intent, 1);
            } else if (options[item].equals("Cancel")) {
                dialog.dismiss();
            }
            if (options[item].equals("Choose from Gallery")) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

                    Intent photoPickerIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    photoPickerIntent.setType("image/*");
                    photoPickerIntent.addCategory(photoPickerIntent.CATEGORY_OPENABLE);
                    startActivityForResult(photoPickerIntent, 2);
                } else if (options[item].equals("Cancel")) {
                    dialog.dismiss();
                } else {
                    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    img_path = MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString();
                    startActivityForResult(intent, 2);
                }
            }
        }
    });
    builder.show();
}

0 个答案:

没有答案