从我自己的设备(奥利奥)以外的相机意图中检索图片的问题

问题描述 投票:0回答:1

我想从相机意图中获取图像。它工作得很好,但仅限于我的设备(oreo / 8.1.0)。我生成unsigned apk并试图在其他设备(馅饼和lolipop)中运行,但它无法正常工作。

我搜索stackoverflow和其他网站,但人们问了关于“lolipop中的相机意图问题”的问题。我的问题是不同的(我认为)。

我试图检查sdk版本并应用不同的代码。即根据stackoverflow的一个答案,我在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版本中给出准确的结果吗?请帮我。提前致谢。

java android android-camera-intent
1个回答
0
投票

试试这个:

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();
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.