Skip to content

Instantly share code, notes, and snippets.

@s3va
Last active April 17, 2022 13:35
Show Gist options
  • Save s3va/85c1c330f9786c60903934d7e3e8f479 to your computer and use it in GitHub Desktop.
Save s3va/85c1c330f9786c60903934d7e3e8f479 to your computer and use it in GitHub Desktop.
Android 11 (API 30) ActivityResultContracts.OpenDocument() Write File FuckUp
package tk.kvakva.opendocumentdummyeditor
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Binder
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.DocumentsContract
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.database.getIntOrNull
import androidx.core.database.getLongOrNull
import androidx.core.database.getStringOrNull
class MainActivity : AppCompatActivity() {
private val mEditTextView: EditText by lazy { findViewById(R.id.editTextV) }
private val wButton: Button by lazy { findViewById(R.id.saveBtn) }
private var uri: Uri? = null
private fun writeBtn(w: Boolean) {
wButton.isEnabled=w
if (w) {
wButton.text = getString(R.string.save)
} else {
wButton.text = getString(R.string.read_only)
}
}
private val resultContracts =
registerForActivityResult(ActivityResultContracts.OpenDocument()) { u ->
if (u == null) {
Toast.makeText(this, "No file selected", Toast.LENGTH_SHORT).show()
return@registerForActivityResult
}
uri = u
contentResolver.openInputStream(u)?.bufferedReader()?.use {
mEditTextView.setText(it.readText())
}
contentResolver.query(u, null, null, null, null)?.let { cursor ->
cursor.moveToFirst()
val fn = cursor.getStringOrNull(
cursor.getColumnIndex(
android.provider.OpenableColumns.DISPLAY_NAME
)
)
val did = DocumentsContract.getDocumentId(u)
val doc_id =
cursor.getStringOrNull(cursor.getColumnIndex(DocumentsContract.Document.COLUMN_DOCUMENT_ID))
val mime_type =
cursor.getStringOrNull(cursor.getColumnIndex(DocumentsContract.Document.COLUMN_MIME_TYPE))
val l_modified =
cursor.getLongOrNull(cursor.getColumnIndex(DocumentsContract.Document.COLUMN_LAST_MODIFIED))
val sz =
cursor.getLongOrNull(cursor.getColumnIndex(android.provider.OpenableColumns.SIZE))
val fl =
cursor.getIntOrNull(cursor.getColumnIndex(DocumentsContract.Document.COLUMN_FLAGS))
if (fl != null)
writeBtn((fl and DocumentsContract.Document.FLAG_SUPPORTS_WRITE) != 0)
Log.i(TAG, "---- column: ${cursor.columnNames}")
Log.i(TAG, "getDocumentId: $did")
Log.i(
TAG,
"uri: $u\ndocument_id: $doc_id\nmime_tipe: $mime_type\nlast modified: $l_modified\nsize: $sz\nflags: $fl"
)
for (i: Int in 0 until cursor.columnCount) {
Log.i(TAG, cursor.getColumnName(i) + " : " + cursor.getType(i) + " : ")
}
cursor.close()
when (val p = checkUriPermission(
uri,
Binder.getCallingPid(),
Binder.getCallingUid(),
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)) {
PackageManager.PERMISSION_GRANTED -> {
Toast.makeText(
this,
"Write Permission Granted to: $fn\n(size: $sz)\n",
Toast.LENGTH_LONG
).show()
}
PackageManager.PERMISSION_DENIED ->
Toast.makeText(
this,
"Write Permission Denied!!!! filename: $fn\n(size: $sz)",
Toast.LENGTH_LONG
).show()
else ->
Toast.makeText(
this,
"Down Know what is this permission: $p\nfilename: $fn (size: $sz)",
Toast.LENGTH_LONG
).show()
}
title = "$fn opened"
val cursor2 = contentResolver.query(
u,
arrayOf(DocumentsContract.Document.COLUMN_FLAGS),
null,
null,
null
)
val flags = if (cursor2?.moveToFirst() == true && !cursor2.isNull(0)) {
cursor2.getInt(0)
} else {
0
}
Log.i(
TAG,
"-------- flags: $flags --- fl: $fl -------- isWritable: ${(flags and DocumentsContract.Document.FLAG_SUPPORTS_WRITE) != 0} --------------"
)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<Button>(R.id.loadBtn).setOnClickListener {
resultContracts.launch(arrayOf("text/*"))
}
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
writeBtn(false)
wButton.setOnClickListener {
if (uri != null) {
try {
contentResolver.openOutputStream(uri!!,"rwt")?.bufferedWriter()
?.use { bufferedWriter ->
bufferedWriter.write(mEditTextView.text.toString())
}
Toast.makeText(
this,
"******\nSaved OK!!!!!\n********\nI am almost sure....",
Toast.LENGTH_SHORT
).show()
} catch (e: IllegalArgumentException) {
Log.i(TAG, "--------------------------\nonSaveBtn: ${e.message}")
Log.i(TAG, "onSaveBtn:\n${e.stackTraceToString()}")
Toast.makeText(this, e.stackTraceToString(), Toast.LENGTH_LONG).show()
}
} else {
Toast.makeText(this, "file not chosen", Toast.LENGTH_LONG).show()
}
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putCharSequence(TITLE, title)
outState.putBoolean(WRITABLE_BTN, wButton.isEnabled)
outState.putString(F_URI, uri.toString())
}
/**
* This method is called after [.onStart] when the activity is
* being re-initialized from a previously saved state, given here in
* <var>savedInstanceState</var>. Most implementations will simply use [.onCreate]
* to restore their state, but it is sometimes convenient to do it here
* after all of the initialization has been done or to allow subclasses to
* decide whether to use your default implementation. The default
* implementation of this method performs a restore of any view state that
* had previously been frozen by [.onSaveInstanceState].
*
*
* This method is called between [.onStart] and
* [.onPostCreate]. This method is called only when recreating
* an activity; the method isn't invoked if [.onStart] is called for
* any other reason.
*
* @param savedInstanceState the data most recently supplied in [.onSaveInstanceState].
*
* @see .onCreate
*
* @see .onPostCreate
*
* @see .onResume
*
* @see .onSaveInstanceState
*/
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
savedInstanceState.getCharSequence(TITLE)?.let {
title = it
}
writeBtn(savedInstanceState.getBoolean(WRITABLE_BTN,false))
uri=Uri.parse(savedInstanceState.getString(F_URI))
}
// onst val PICK_PDF_FILE = 2
//
//fun openFile(pickerInitialUri: Uri) {
// val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
// addCategory(Intent.CATEGORY_OPENABLE)
// type = "application/pdf"
//
// // Optionally, specify a URI for the file that should appear in the
// // system file picker when it loads.
// putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri)
// }
//
// startActivityForResult(intent, PICK_PDF_FILE)
//}
}
private const val TAG = "MainActivity"
private const val TITLE = "title"
private const val WRITABLE_BTN = "Writable file"
private const val F_URI = "File uri"
mEditTextView.setText(it.readText())
}
val cursor = contentResolver.query(u, null, null, null, null)
cursor?.moveToFirst()
val fn = cursor?.getStringOrNull(
cursor.getColumnIndex(
android.provider.OpenableColumns.DISPLAY_NAME
)
)
val sz = cursor?.getStringOrNull(cursor.getColumnIndex(android.provider.OpenableColumns.SIZE))
cursor?.close()
when (val p=checkUriPermission(uri,Binder.getCallingPid(),Binder.getCallingUid(), Intent.FLAG_GRANT_WRITE_URI_PERMISSION)) {
PackageManager.PERMISSION_GRANTED -> {
Toast.makeText(
this,
"Write Permission Granted.\n$u\nfilename: $fn (size: $sz)\n",
Toast.LENGTH_LONG
).show()
}
PackageManager.PERMISSION_DENIED ->
Toast.makeText(this, "Write Permission Denied!!!!\n$u\nfilename: $fn (size: $sz)", Toast.LENGTH_LONG).show()
else ->
Toast.makeText(this, "Down Know what is this permission: $p\n$u\nfilename: $fn (size: $sz)", Toast.LENGTH_LONG).show()
}
Log.i(TAG, "**********: \n$u\nfilename: $fn (size: $sz)")
title="$fn opened"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<Button>(R.id.loadBtn).setOnClickListener{
resultContracts.launch(arrayOf("text/*"))
}
findViewById<Button>(R.id.saveBtn).setOnClickListener {
if(uri!=null) {
try {
contentResolver.openOutputStream(uri!!)?.bufferedWriter()?.use { bufferedWriter ->
bufferedWriter.write(mEditTextView.text.toString())
}
Toast.makeText(
this,
"******\nSaved OK!!!!!\n********\nI am almost sure....",
Toast.LENGTH_SHORT
).show()
} catch (e: IllegalArgumentException) {
Log.i(TAG, "--------------------------\nonSaveBtn: ${e.message}")
Log.i(TAG, "onSaveBtn:\n${e.stackTraceToString()}")
Toast.makeText(this, e.stackTraceToString(), Toast.LENGTH_LONG).show()
}
} else {
Toast.makeText(this, "file not chosen", Toast.LENGTH_LONG).show()
}
}
}
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
savedInstanceState.getCharSequence(TITLE)?.let {
title=it
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putCharSequence(TITLE,title)
}
}
private const val TAG = "MainActivity"
private const val TITLE = "title"
@Hawksbillcat
Copy link

good exmaple , you gave me a bigggg hand

@s3va
Copy link
Author

s3va commented Nov 9, 2021

Bug in Android 11. Selecting from "Recent" results in an error when saving text to the selected file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment