androidandroid-fileproviderandroid-storage

When do we need to use FileProvider?


I'm quite confused about when to use FileProvider when we can save a bitmap to file like this

fun saveBitmapToFile(bitmap: Bitmap) {
   val fileName = generaUniqueName()
   val subdirectory = File(application!!.filesDir, Constants.IMAGE_DIRECTORY)
   if (!subdirectory.exists()) {
        subdirectory.mkdirs()
   }
   val imageFile = File(subdirectory, fileName)
   val outputStream = FileOutputStream(imageFile)
   bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
   outputStream.flush()
   outputStream.close()
}

So, is it only necessary when the file we save needs to be accessed from external applications? Or is there something else that makes it important?


Solution

  • is it only necessary when the file we save needs to be accessed from external applications?

    Generally yes. For files that your app uses internally, you normally do not need FileProvider.