java - 在除我自己的设备(奥利奥)之外的其他设备中从相机 Intent 检索图片时出现问题

标签 java android android-camera-intent

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

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

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

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();
        }
    }

关于java - 在除我自己的设备(奥利奥)之外的其他设备中从相机 Intent 检索图片时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55883910/

相关文章:

Android 相机 - SD 卡不可用

java - 为什么应用程序看不到 Camera2API 权限?

java - Intent.ACTION_GET_CONTENT 和 Intent.ACTION_PICK 之间的区别

java - 如何迭代 JSONObject 的 N 层?

Java正则表达式搜索值

java - 用大括号澄清一种方法内的单独代码部分

android - 每次我尝试导出签名的 apk 时,ADT 都会排除一堆类

android - Android Studio Gradle构建脚本不起作用

java - Thymeleaf:无法传递隐藏 th:field 的预定义值

android - 将 SQLite 数据库同步到 App Engine 数据存储区?