[英]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.