使用fileprovider将图像保存到图库

时间:2017-03-31 15:19:26

标签: android

我试图找出是否必须将图像保存到外部存储器然后再保存到图库,因此需要WRITE_EXTERNAL_STORAGE,或者我可以使用Fileprovider。

这样的事情可能吗?

    Uri uri = FileProvider.getUriForFile(getApplicationContext(), Config.CAPTURE_IMAGE_FILE_PROVIDER, imageFile);
    ContentValues values = new ContentValues();    
    values.put(MediaStore.Images.Media.DATA, uri.toString());
    ...
    ContentResolver cr = getContentResolver();
    Uri mediaStoreUri = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

如何将READ_URI_PERMISSION授予Gallery(Mediastore)?

1 个答案:

答案 0 :(得分:0)

请参阅https://stackoverflow.com/a/44455957/966789(Android为Android 6.0 Marshmallow添加了新的权限模型)。

...并在onRequestPermissionsResult()中处理结果:

            File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            File file = new File(path, "sample.JPG");
            try {
                file.createNewFile();
            }
            catch (IOException e) {
                Toast.makeText(this.getApplicationContext(),"createNewFile:"+e.toString(),Toast.LENGTH_LONG).show();
            }