Android save photo on disk in private mode

2019-06-11 02:47发布

问题:

I'm able to save a Image in disk using :

String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
        String fileName = imageId + ".png";
        String filePath = baseDir + File.separator + fileName;

        File file = new File(filePath);
        if(!file.exists())
        {
            out = new FileOutputStream(baseDir + File.separator + fileName);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        }

But all the images saved are visible in the gallery of the phone .. How can I hide them to the user ? Is there a private folder for my appliaction ?

Thanks

回答1:

public File getAlbumStorageDir(Context context, String albumName) {
    // Get the directory for the app's private pictures directory. 
    File file = new File(context.getExternalFilesDir(
            Environment.DIRECTORY_PICTURES), albumName);
    if (!file.mkdirs()) {
        Log.e(LOG_TAG, "Directory not created");
    }
    return file;
}

I think this is what you want. Read https://developer.android.com/training/basics/data-storage/files.html#WriteExternalStorage for more information.



回答2:

Save it to the internal storage using Context.getFilesDir() instead of Environment.getExternalStorageDirectory().

From the docs:

Files saved here are accessible by only your app by default.

Check this link out.