在我自己的设备 (Oreo) 中从 Camera Intent 中检索图片时出现问题

Problem retrieving picture from Camera intent in other than myself device (Oreo)

我正在尝试从相机意图中获取图像。它运行良好,但仅在我的设备 (oreo / 8.1.0) 中有效。我生成未签名的 apk 并尝试在其他设备(pie 和 lolipop)中 运行 但它不起作用。

我搜索了 Whosebug 和其他网站,但有人问了关于 "camera intent problem in lolipop" 的问题。我的问题有些不同(我认为)。

我尝试检查 sdk 版本并应用不同的代码。 IE。根据 Whosebug 的一个回答,我在 LOLIPOP 中使用了这段代码 Picasso.get().load(file).into(imageView); 而不是 Picasso.get().load(photoURI).into(imageView); 但它不起作用。

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            File photoFile = null; // Create the File where the photo should go
            try {
                photoFile = createImageFile();
                tempPhoto = photoFile;
            } catch (IOException ex) {
                // Error occurred while creating the File
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                photoURI = FileProvider.getUriForFile(this,
                        "com.package.appname.fileprovider",
                        photoFile);
                tempPhoto = photoFile;
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }

createImageFile 函数代码:

    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = image.getAbsolutePath();
        return image;
    }

这里收到结果

   protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        try {
            if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
                File file = new File(mCurrentPhotoPath);

                Picasso.get().load(file).into(imageView);

//                Bitmap bitmap = MediaStore.Images.Media
//                        .getBitmap(this.getContentResolver(), Uri.fromFile(file));
//                if (bitmap != null) {
//                    imageView.setImageBitmap(bitmap);
//                }
            } else if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK) {
                File file = new File(mCurrentPhotoPath);
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.fromFile(file));
                if (bitmap != null) {
                    imageView.setImageBitmap(bitmap);
                }
            }

        } catch (Exception error) {
            error.printStackTrace();
        }
    }

任何人都可以帮助我在所有 sdk 版本中给出准确的结果吗?请帮我。提前致谢。

试试这个:

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        try {
            if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        Uri uri = intent.getData(); // you need take uri of image
        imageView.setImageURI(uri); // you don't need of picasso to load local images     


           //     Picasso.get().load(file).into(imageView);

//                Bitmap bitmap = MediaStore.Images.Media
//                        .getBitmap(this.getContentResolver(), Uri.fromFile(file));
//                if (bitmap != null) {
//                    imageView.setImageBitmap(bitmap);
//                }
            } else if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK) {
                File file = new File(mCurrentPhotoPath);
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.fromFile(file));
                if (bitmap != null) {
                    imageView.setImageBitmap(bitmap);
                }
            }

        } catch (Exception error) {
            error.printStackTrace();
        }
    }