gpt4 book ai didi

Android Studio MediaStore Storage 无法访问文件夹

转载 作者:行者123 更新时间:2023-12-02 13:30:47 25 4
gpt4 key购买 nike

尝试下载和保存文件时,会引发错误

Primary directory file not allowed for content://media/external/downloads;
allowed directories are [Download]

我该如何解决它,也许没有权限?
val multipartBuilder = MultipartBody.Builder().setType(MultipartBody.FORM)

multipartBuilder.addFormDataPart("oid", orderId)

val client = OkHttpClient.Builder()
.connectTimeout(120, TimeUnit.SECONDS)
.writeTimeout(120, TimeUnit.SECONDS)
.readTimeout(120, TimeUnit.SECONDS)
.build()

val request = Request.Builder()
.url(Api.API_URL + "order/shopOrderPrintAgreement")
.addHeader("Authorization", "Bearer ${Api.token}")
.post(multipartBuilder.build())
.build()

val response = client.newCall(request).execute()

/**
*TODO починить для android 10
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val resolver = context!!.contentResolver
val contentValues = ContentValues().apply {
put(MediaStore.DownloadColumns.DISPLAY_NAME, "agreement_$orderId")
put(MediaStore.DownloadColumns.MIME_TYPE, "application/pdf")
put(MediaStore.DownloadColumns.RELATIVE_PATH, "file/Peshkariki")
}
// MediaStore.Images.Media.EXTERNAL_CONTENT_URI
val uri = resolver.insert(
MediaStore.Downloads.EXTERNAL_CONTENT_URI,
contentValues
)

if (uri != null) {
try {
resolver.openOutputStream(uri).use {
it?.let {
val sink = it.sink().buffer()
sink.writeAll(response.body()!!.source())
sink.close()
}
}

mainActivity.runOnUiThread {
dialog?.dismiss()
try {
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(
uri, "application/pdf"
)
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)

if (isActivityForIntentAvailable(intent)) {
startActivity(intent)
} else {
val webIntent = Intent(Intent.ACTION_VIEW)
webIntent.setDataAndType(
uri, "text/html"
)
webIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
webIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivity(webIntent)
}
} catch (e: Exception) {
e.printStackTrace()
Toast.makeText(
mainActivity,
"Не удалось открыть файл",
Toast.LENGTH_SHORT
).show()
}
}
} catch (e: Exception) {
Crashlytics.logException(e)
}
} else {
log("OrderClientFragment error with uri == null")
Crashlytics.log("OrderClientFragment error with uri == null ")
}

我在 list 中声明并在我的项目中使用的所有权限的列表
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

追溯
java.lang.IllegalArgumentException: Primary directory file not allowed for content://media/external/downloads; allowed directories are [Download]
at com.android.providers.media.MediaProvider.ensureFileColumns(MediaProvider.java:2117)
at com.android.providers.media.MediaProvider.ensureUniqueFileColumns(MediaProvider.java:1928)
at com.android.providers.media.MediaProvider.insertFile(MediaProvider.java:2495)
at com.android.providers.media.MediaProvider.insertInternal(MediaProvider.java:3254)
at com.android.providers.media.MediaProvider.insert(MediaProvider.java:2903)
at android.content.ContentProvider$Transport.insert(ContentProvider.java:309)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:154)
at android.os.Binder.execTransactInternal(Binder.java:1021)
at android.os.Binder.execTransact(Binder.java:994)
2020-05-01 19:05:44.698 7774-7992/com.app.peshkariki E/AndroidRuntime: FATAL EXCEPTION: Thread-20
Process: com.app.peshkariki, PID: 7774
java.lang.IllegalArgumentException: Primary directory file not allowed for content://media/external/downloads; allowed directories are [Download]
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:170)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
at android.content.ContentProviderProxy.insert(ContentProviderNative.java:481)
at android.content.ContentResolver.insert(ContentResolver.java:1828)
at com.app.peshkariki.i.i$n0.run(OrderClientFragment.kt:778)

778线,这是
val uri = resolver.insert(
MediaStore.Downloads.EXTERNAL_CONTENT_URI,
contentValues
)

好像项目中用到了所有必要的权限,所以我什至不知道该用什么方法来解决这个问题

最佳答案

利用:

put(MediaStore.DownloadColumns.RELATIVE_PATH, "Download/Peshkariki")

代替
put(MediaStore.DownloadColumns.RELATIVE_PATH, "file/Peshkariki")

Primary directory file not allowed

关于Android Studio MediaStore Storage 无法访问文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61546796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com