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

Android Q make photos saved in getExternalFilesDir() visible in Gallery

我想要的只是我的应用程序照片可以在 phone 图库和我的应用程序内看到。

我想显示我的应用程序制作的所有以前的图像,并且这样做(我删除了一些关于填充 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。我尝试了其他方法,但没有任何效果。

File directory = getExternalFilesDir(Environment.DIRECTORY_DCIM);

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

无法被MediaStore扫描。

并且由于图库应用程序大多会在 MediaStore 中查询图像,因此他们将看不到这些图像。

将它们存储在不同的地方或以不同的方式。