2017-03-31 76 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(); 
      } 
相關問題