内容uri上的outputStream
我拥有特定用户选择的图像的内容URI,我想在其上打开一个InputStream,以便
在用户选择图像时从该URI中获取位图,我将持久的读取和写入权限:
val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
contentResolver?.takePersistableUriPermission(mediaUri, flags)
内容URI在这种格式:content://com.android.providers.downloads.documents/document/msf%3a24
获得此InputStream的最佳解决方案似乎是:
val inputFile = context.contentResolver.openInputStream(uri)
val bitmap = BitmapFactory.decodeStream(inputFile)
运行第二行的第二行时,会出现错误代码:
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /com.android.providers.downloads.documents/document/msf%3A24: open failed: ENOENT (No such file or directory)
请注意,URI似乎在错误消息中畸形,
我该如何获取输入流?
附录:我正在尝试在工人(androidx.work.coroutineworker)中获取此输入流。
I have the content URI of a specific user selected image on which i want to open an InputStream, in order to get a Bitmap from that URI
When the user select the image, i take the persistable read and write permissions:
val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
contentResolver?.takePersistableUriPermission(mediaUri, flags)
The content URI is in this format: content://com.android.providers.downloads.documents/document/msf%3A24
The best solution to get this InputStream seems to be:
val inputFile = context.contentResolver.openInputStream(uri)
val bitmap = BitmapFactory.decodeStream(inputFile)
An error is thrown when running the second line of code:
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /com.android.providers.downloads.documents/document/msf%3A24: open failed: ENOENT (No such file or directory)
Note that the URI seems to malformed in the error message
How can i get my InputStream ?
Addendum: I'm trying to get this InputStream in a Worker (androidx.work.CoroutineWorker).
User selects the image: https://github.com/ninjinskii/Cavity/blob/master/app/src/main/java/com/louis/app/cavity/ui/addwine/FragmentAddWine.kt#L53
The image URI is stored in the DB: https://github.com/ninjinskii/Cavity/blob/master/app/src/main/java/com/louis/app/cavity/ui/addwine/AddWineViewModel.kt#L86
User trigger the WorkManager in charge for image uploading:
https://github.com/ninjinskii/Cavity/blob/feature/backup-api/app/src/main/java/com/louis/app/cavity/ui/account/worker/UploadWorker.kt#L131
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论