Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

How do I handle files on Android device that are created by a PC? API level 29

I would like to do the following:

  • Read and edit a file that is created on the device via USB cable connected to a PC.
  • Write files that are visible when the user opens the device's internal storage in the windows file explorer.

I target Android 10 (API level 29).

The best solution I was able to find for reading files was the deprecated

val containerFile = File(
    getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
    "myFile.csv"
)
// Then I was able to read files from
val inputStream: InputStream = contentResolver.openInputStream(Uri.fromFile(containerFile))!!

This way when I placed "myFile.csv" in the downloads folder, my app was able to read the contents.

As for creating PC-readable files, the only solution I found was to create hidden temporary files, and whenever I had to make them readable from PC, I created an intent as follows:

val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
    addCategory(Intent.CATEGORY_OPENABLE)
    type = "file/csv"
    putExtra(Intent.EXTRA_TITLE, "output.csv")
    // I specify a URI for the directory that should be opened in  the system file picker
    putExtra(
        DocumentsContract.EXTRA_INITIAL_URI,
        getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
    )
}

Is there any better solution?

If not, how do I get a path to a directory which is visible from PC, using API 29?

I know this should be possible, since there are many text editor apps for android, which are doing the same things I want to, but I was not able to find any tutorial. I need a long term solution, I'm very confused... Thank you very much!

question from:https://stackoverflow.com/questions/65847442/how-do-i-handle-files-on-android-device-that-are-created-by-a-pc-api-level-29

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Big thanks to @CommonsWare for the suggestions!

ACTION_OPEN_DOCUMENT_TREE and GET_PERSISTABLE_PERMISSION gives acces to a user selected folder and all the files in it. Anything created by the app is visible from PC.

What I needed was

startActivityForResult(Intent(Intent.ACTION_OPEN_DOCUMENT_TREE), GET_PERSISTABLE_PERMISSION)

and

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (requestCode == GET_PERSISTABLE_PERMISSION && resultCode == Activity.RESULT_OK) {
        //FileHandler.onActivityResult(data)
        data?.data?.also{ uri->
            val contentResolver = applicationContext.contentResolver
            val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
            contentResolver.takePersistableUriPermission(uri, takeFlags)

//          Write a test file in the selected folder
//            val pickedDir = DocumentFile.fromTreeUri(this, uri)
//            val tmpFile = pickedDir.createFile("text/csv", "debugTestFile")
//            val out: OutputStream? = getContentResolver().openOutputStream(tmpFile!!.uri)
//            out?.write(("uFEFF" + "árvízt?r? tük?rfúrógép
").toByteArray()) // adding BOM to the start for Excel to recognize utf8
//            out?.close()
        }
    }
    super.onActivityResult(requestCode, resultCode, data)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...