简体   繁体   English

如何从 Android Oreo 及更高版本的下载文件夹上传文件?

[英]How to upload file from downloads folder in Android Oreo and above?

I am trying to write the code to upload file from Android Oreo and Above.我正在尝试编写代码以从 Android Oreo 及更高版本上传文件。 First I am running a intent to get the uri of the file.首先,我正在运行一个意图来获取文件的 uri。

intent_upload.action = Intent.ACTION_GET_CONTENT
return Intent.createChooser(intent_upload, pickerTitle)

But when I select file from downloads folder, it returns a null filepath from following code.但是当我从下载文件夹中选择文件时,它从以下代码返回一个空文件filepath It works perfectly for devices below Android Oreo but I cannot find any solution for android o and above.它适用于 Android Oreo 以下的设备,但我找不到适用于 android o 及以上版本的任何解决方案。

Please help请帮忙

val id = DocumentsContract.getDocumentId(uri)
val contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), java.lang.Long.valueOf(id));
return getDataColumn(context, contentUri, null, null)

Code for getDataColumn is as follow getDataColumn代码如下

fun getDataColumn(context: Context, uri: Uri?, selection: String?,
                          selectionArgs: Array<String>?): String? {

        var cursor: Cursor? = null
        val column = "_data"
        val projection = arrayOf(column)

        try {
            cursor = context.contentResolver.query(uri!!, projection, selection, selectionArgs, null)
            val temp = context
            if (cursor != null && cursor.moveToFirst()) {
                val index = cursor.getColumnIndexOrThrow(column)
                return cursor.getString(index)
            }
        }catch (e:Exception){

        } finally {
            cursor?.close()
        }
        return null
    }

It is happening for API 26 and above. API 26 及更高版本正在发生这种情况。

My motive is to upload a pdf file from the downloads folder in Android.我的动机是从 Android 的下载文件夹上传 pdf 文件。

I know this is past due.我知道这已经过期了。 I also faced this issue and I was able to find a solution from this gist.我也遇到了这个问题,我能够从这个要点中找到解决方案。 FileUtils.java . 文件实用程序.java Just use this util class in your project只需在您的项目中使用这个 util 类

Just leaving this here for people who face this issue in the future.只是把这个留在这里给将来面临这个问题的人。 Wasted a couple of hours working on this.浪费了几个小时的工作。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Uri uri = data.getData();
    File originalFile = new File(FileUtils.getRealPath(this,uri));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Android Oreo (8.1) 或更高版本中从 URI 获取文件路径 - How to Get File Path from URI in Android Oreo (8.1) or above 如何在具有 Oreo 及以上操作系统的 Android 设备中从 uri 获取文件路径 - How to get the file path from uri in Android devices having Oreo and above OS 在 android 11(sdk 30) 中,如何从下载文件夹上传 pdf 文件? 我收到权限被拒绝的 EACCES 消息 - in android 11(sdk 30) ,how to upload pdf file from downloads folder? i am getting permission denied EACCES message 无法从 Android 上的下载文件夹中读取文件 - Cannot read a file from Downloads folder on Android 如何将文件下载到Android中的内部下载文件夹中 - How to download file into internal downloads folder in Android 如何在android Oreo及以上版本中跟踪用户 - How to track user in android Oreo and above version 如何在Android Oreo或更高版本上设置闹钟? - How to set Alarm on android Oreo or Above? Android:从“下载”文件夹中删除下载的文件快捷方式 - Android: Remove Downloaded file shortcut from "Downloads" folder 无法从 Android 应用程序的下载文件夹中读取文件 - Unable to read file from the Downloads folder for an android app 从 Android 的下载文件夹中获取 pdf 或 doc 文件 - Getting pdf or doc file from Downloads Folder in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM