Android - 防止相机保存图片

Android - preventing camera from saving pictures

我的代码旨在将拍摄的照片仅存储在特定于应用程序的目录中。但是,它还将它们存储在画廊中,这会造成很大的不便。有没有办法解决这个问题?这是我的代码:

if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        photoFile = null;
        try {
            photoFile = createImageFile(1);
        } catch (IOException e) {
            // Error occurred when creating a file
            System.out.println(e.getLocalizedMessage());
        }
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

onActivityResult方法:

 public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    switch(requestCode)
    {
        case REQUEST_IMAGE_CAPTURE:
            if(resultCode == RESULT_OK)
            {
                ImageView img;
                if (orientation == 1) {
                    img = (ImageView) findViewById(R.id.image_front);
                } else {
                    img = (ImageView) findViewById(R.id.image_side);
                }
                img.setImageURI(Uri.fromFile(photoFile));
            }
            break;
    }
}

提前致谢。

在保存图像的文件夹中创建一个名为 .nomedia 的空文件。

来自Android documentation

Hiding your files from the Media Scanner

Include an empty file named .nomedia in your external files directory (note the dot prefix in the filename). This prevents media scanner from reading your media files and providing them to other apps through the MediaStore content provider. However, if your files are truly private to your app, you should save them in an app-private directory.