Android save photo on disk in private mode

心已入冬 提交于 2019-12-23 05:06:42

问题


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.



来源:https://stackoverflow.com/questions/37679334/android-save-photo-on-disk-in-private-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!