2017-03-31 101 views
0

我想知道是否必须将图像保存到外部存储器,然后再保存到图库,因此需要WRITE_EXTERNAL_STORAGE,或者我可以使用Fileprovider来完成。使用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授予图库(Mediastore)?

回答

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(); 
      }