Issue
When trying to download and save a file, it throws an error
Primary directory file not allowed for content://media/external/downloads;
allowed directories are [Download]
How can I fix it, maybe there is no permission?
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 of all permissions that I have declared in manifest and use in my project
<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" />
Traceback
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 line, this is
val uri = resolver.insert(
MediaStore.Downloads.EXTERNAL_CONTENT_URI,
contentValues
)
It seems that all the necessary permissions are used in the project, so I don't even know which way to look to solve this problem
Solution
use:
put(MediaStore.DownloadColumns.RELATIVE_PATH, "Download/Peshkariki")
instead of
put(MediaStore.DownloadColumns.RELATIVE_PATH, "file/Peshkariki")
Primary directory file not allowed
Answered By - Kasım Özdemir
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.