Android Q 使保存在 getExternalFilesDir() 中的照片在图库中可见

问题描述

我只想让我的应用照片在手机图库和应用内都可以看到。

我想显示我的应用程序制作的所有以前的图像并这样做(我删除了一些关于填充 recyclerView 的代码以使其更具可读性):

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ivPhoto = findViewById(R.id.photo);

        photoPaths = new ArrayList<>();
        File directory = getExternalFilesDir(Environment.DIRECTORY_DCIM);
        File[] files = directory.listFiles();
        if (files != null && files.length > 0) {
            for (File file : files) {
                photoPaths.add(file.getAbsolutePath());
            }
        }
        if (photoPaths.size() > 0) {
            File file = new File(photoPaths.get(0));
            try {
                ExifInterface exif = new ExifInterface(file.getCanonicalPath());
                String text = exif.getAttribute(ExifInterface.TAG_EXIF_VERSION);
                Toast.makeText(this,text,Toast.LENGTH_SHORT).show();
            } catch (IOException exception) {
                exception.printstacktrace();
            }
            Bitmap bitmap = BitmapFactory.decodeFile(photoPaths.get(0));
            ivPhoto.setimageBitmap(bitmap);
        }
}

我用来从相机拍照方法

private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION
                | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                ex.printstacktrace();
            }
            if (photoFile == null) {
                return;
            }
            Uri photoURI;
            Uri photoURIQ;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

                ContentValues values = new ContentValues(2);
                values.put(MediaStore.Images.Media.MIME_TYPE,"image/jpeg");
                values.put(MediaStore.Images.Media.RELATIVE_PATH,Environment.DIRECTORY_DCIM);

                photoURIQ = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoURIQ);
            } else {
                photoURI = getUriForFile(this,"com.example.photoMetadata.fileprovider",photoFile);

                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoURI);
            }
            startActivityForResult(takePictureIntent,REQUEST_IMAGE_CAPTURE);
        }
    }

上面的代码使照片在图库中可见,但当然我的应用程序文件夹中没有图片

我可以更改该代码以使下一张照片在我的应用中可见,但这样它们在图库中将不可见。

我试图从图库中检索照片文件并将其保存到 onActivityResult 中的 getExternalStorageDir() 文件夹,但得到了 EACCESS SecurityExeption。我尝试了其他方法,但没有任何效果

解决方法

文件目录 = getExternalFilesDir(Environment.DIRECTORY_DCIM);

目录 getExternalFilesDir() 是特定于应用程序的目录。

MediaStore 无法对其进行扫描。

而且,由于图库应用通常会在 MediaStore 中查询图像,然后他们将看不到它们。

将它们存放在不同的地方和/或以不同的方式。