I'm trying to send a photo to the server, but I can't access the photo ONLY on Android 10. Made a READ_EXTERNAL_STORAGE request, clicked "allow", in the settings, access to storage is allowed.
Manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Fragment
private fun haveStoragePermission() =
ActivityCompat.checkSelfPermission(requireActivity(), Manifest
.permission.READ_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED
private fun requestPermission() {
if (!haveStoragePermission()) {
val permissions = arrayOf(
Manifest.permission.READ_EXTERNAL_STORAGE
)
ActivityCompat.requestPermissions(requireActivity(), permissions, STORAGE_REQUEST_PERMISSION)
}
}
The path to the file is displayed in logs. It's absolute, like file:///emulated/0/...photo.jpg , it is convenient, since you can immediately upload it to the server , but Picasso and server does not accept photos , or rather it does not seem to be allowed to read (Picasso displays placeholder due to an error). This code only works if you add requestLegacyExternalStorage to the manifest. Then everything works fine at once , but this attribute disappear in Android 11. I have no idea why exactly 10 Android doesn't work , although with test devices on 7, 8 , 6 everything is OK.
//Get images code
private fun getAllShownImagesPath(activity: Activity): ArrayList<String> {
val listOfAllImages = ArrayList<String>()
val uri: Uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
val projection = arrayOf(
MediaColumns.DATA,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME
)
val cursor = activity.contentResolver.query(
uri, projection, null,
null,"${MediaStore.Images.Media.DATE_TAKEN} DESC"
)
val columnIndexData = cursor!!.getColumnIndexOrThrow(MediaColumns.DATA)
while (cursor.moveToNext()) {
val absolutePathOfImage = cursor.getString(columnIndexData)
listOfAllImages.add("file://$absolutePathOfImage")
}
return listOfAllImages
}