0

I am using the following function for making request and decoding URI from onActivity result . The following function works but freezes the whole screen for many seconds before generating the final file:

// Request code:

fun filePickerRequest3SAF(activity: AppCompatActivity) {
    RequestIntentBuilder(IntentInit.OPEN_DOCUMENT)     // Intent(Intent.ACTION_OPEN_DOCUMENT)
        .addOpenableCategory()//requestIntent.addCategory(Intent.CATEGORY_OPENABLE)
        .setFilteringMimeType("video/*")
        .addFlagForReadPermission() //requestIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) and requestIntent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
        .buildAndStartActivityForResult(activity)
}


//response function. here I pass received intent's URI and activity context



fun getPathFromUriOrEmpty(uri: Uri?, context: Context?): String {
    if (context == null || uri == null) return ""
    return getFileFromUriOrDefault(uri, context)?.path ?: ""
}

fun getFileFromUriOrDefault(uri: Uri?, context: Context?, default:File? = null): File? {
    if (context == null || uri == null) return default
    val resolver = context.contentResolver
    val tmpFile = File(context.cacheDir, getFileNameFromUriOrNull(uri, resolver) ?: "temp_file_name.${getExtensionFromUriOrDefault(uri, context)}")
    return try {
        val inputStream = context.contentResolver.openInputStream(uri)
        val outputStream = FileOutputStream(tmpFile)
        outputStream.use { fileOut -> inputStream?.copyTo(fileOut) }
        tmpFile
    } catch (t: Throwable) {
        t.printStackTrace()
        default
    }
}



is there a way to do it better, apart from just making a file and copying it as whole? My app is supposed to upload videos of size > 1-2 gb, so is there a way we can provide the URI / file to the upload service without actually making a copy of file? I am assuming the file upload service will also be making multiple copies to upload

Ps: I intent to support android versions KitKat to android 12/+ , so not thinking of using legacy storage or other such flags if they can be avoided with SAF as a unified solution across different android versions

ansh sachdeva
  • 1,220
  • 1
  • 15
  • 32
  • 2
    "so is there a way we can provide the URI / file to the upload service without actually making a copy of file?" -- what is "the upload service"? For example, if it is using OkHttp (with or without Retrofit), you can create a `RequestBody` that works with a `Uri`: https://stackoverflow.com/a/56308643/115145 – CommonsWare Nov 07 '21 at 16:23
  • 1
    `addFlagForReadPermission()` Better remove as it makes no sense. – blackapps Nov 07 '21 at 16:50
  • @CommonsWare i plan to use the following upload service library which works with File from uri : https://github.com/gotev/android-upload-service – ansh sachdeva Nov 07 '21 at 21:06
  • @blackapps if i have to upload a 2+ gb video, i need to access the video location for more than a few minutes. Are you sure I won't be needing the permission? i thought the permission was for the scenarios where we need access to a file uri for longer time and therefore used it. – ansh sachdeva Nov 07 '21 at 21:09
  • 1
    YOU cannot grant a permission. The provider will give you permission to use the selected uri. Size of file does not matter. Time need to upload does not matter. Remove it. – blackapps Nov 07 '21 at 21:13
  • 1
    "i plan to use the following upload service library which works with File from uri" -- they [demonstrate using `ACTION_OPEN_DOCUMENT`](https://github.com/gotev/android-upload-service/wiki/Getting-Started-with-4.x#file-picker) without making a copy of the content. Have you tried that? – CommonsWare Nov 07 '21 at 21:38
  • @CommonsWare hmm.. I tried the same example which did not work for open_documents request which led me to research more about the SAF. Guess I shall be reporting the example to library owner . However,I would like to ask an alternate question, is its possible to directly upload chunks of a file without actually copying them in a memory? I mean, we would need to make a buffer somehow to temporarily store a chunk , so we can use cache for that, but otherwise is creating a whole copy is still required? – ansh sachdeva Nov 08 '21 at 11:14
  • "is its possible to directly upload chunks of a file without actually copying them in a memory?" -- in general? Yes. The OkHttp solution I pointed to earlier effectively does that. – CommonsWare Nov 08 '21 at 11:50
  • Thanks, will look into it and add an answer that works for me. I am guessing it will probably be the one you are suggesting – ansh sachdeva Nov 08 '21 at 12:43

0 Answers0