Search code examples
androidkotlinurifile-writing

Permission denial while writing a file


I'm writing a basic editor to user change content of the file. First i get the lines of file and store it in list like this :

rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { fileUri ->
                        if (fileUri != null) {
                            Consts.file = fileUri
 }
}
val context = LocalContext.current
val inputStream: InputStream? = context.contentResolver.openInputStream(Consts.file!!)
var lineList = mutableListOf<String>()
inputStream?.bufferedReader()?.forEachLine { lineList.add(it) }

and show the lines of it inside a lazy column and change it :

LazyColumn(modifier = Modifier
            .fillMaxSize()
            .padding(8.dp)){
            itemsIndexed(items = lineList){index,item ->
                var textValue by remember { mutableStateOf(item) }
                Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically){
                    Text(style = TextStyle(textDirection = TextDirection.Rtl),text = "${index + 1}")
                    Box(modifier = Modifier
                        .width(1.dp)
                        .height(14.dp)
                        .background(Color.Black))
                    BasicTextField(modifier = Modifier
                        .width(IntrinsicSize.Min)
                        .padding(4.dp)
                        .fillMaxWidth(),value = textValue, onValueChange = {
                        textValue = it
                        lineList = lineList.toMutableList().apply {
                            set(index, it)
                        }
                    })
                }
            }
        }

and after user changed the lines, i want to save the changed content line by line and :

                             
try{
            val outputStream = context.contentResolver.openOutputStream(Consts.file!!)
            outputStream.use {stream->
                val writer = OutputStreamWriter(stream)
                lineList.forEach{line ->
                    writer.write(line)
                }
                writer.flush()
                Log.e("Tag","file saved successfully")
            }
        }catch (e:Exception){
            Log.e("Tag","error while saving file !  ",e)
        }

and added these lines in AndroidManifest.xml :

<!--this is for androids above 10-->
    <uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>
    <!--this is for androids below 10-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

I'll get this error :

java.lang.SecurityException: Permission Denial: writing >com.android.providers.media.MediaDocumentsProvider uri >content://com.android.providers.media.documents/document/document%3A1000000045 from pid=5704, >uid=10191 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()

as error said i added the MANAGE_DOCUMENTS permission in android manifest file and added grantUriPermission like this before writing to the file:

context.grantUriPermission("com.sina.writeeditor" , Consts.file!! , Intent.FLAG_GRANT_WRITE_URI_PERMISSION)

but got same error again.


Solution

  • A file uri obtained with GetContent() is not writable.

    Hence that SecurityException.

    You dont need any permission in manifest using GetContent().

    And no permission in manifest will give you write access.