我知道有很多关于 fileProvider 的帖子,但我似乎无法在那里找到我的问题的答案。
我使用以下 Intent 拍摄图像:
private fun openCamera() {
currentFocus?.clearFocus()
val intent = Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA)
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
}
为了完成这项工作,我还有一个观察者:
fun observe(): Flowable<Picture> =
pictureSubject.onBackpressureLatest()
.map { uri ->
appContext.get()?.let {
val cursor = it.contentResolver.query(uri, null, null, null, "date_added DESC, _id DESC")
if (cursor != null) {
if (cursor.moveToNext()) {
val dataColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA)
val filePath = cursor.getString(dataColumn)
val name = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME)
return@map Picture()
.apply {
this.name = cursor.getString(name)
this.path = filePath
}
}
cursor.close()
}
return@map Picture()
}
return@map Picture()
}
然后将这些图片添加到一个称为文件的对象中。
fun listenForGeneralPictures(observe: Flowable<Picture>?) {
observe?.apply {
generalPictureDisposable = concatMap {
if (it.name?.isNotEmpty() == true
&& it.path?.isNotEmpty() == true
&& file != null) {
it.categoryNr = Picture.CAT_GENERAL_PICTURES
file?.generalPictures?.add(it)
Log.d(it.name.toString(), "pictureAfter")
return@concatMap fileRepository.update(file!!)
} else {
return@concatMap Flowable.just(file)
}
}.subscribe({
cachedFile?.generalPictures = it!!.generalPictures
}, errorUtils::onError)
}
}
我正在使用它来获取路径并将图像设置为 Imageview:
override fun showPicture(path: String?) {
val pictureFile = File(path)
ivDraw?.setImageUri(FileProvider.getUriForFile(this, this.applicationContext.packageName + ".provider", pictureFile))
}
但是当我这样做时,我得到一个错误:
System.err: java.io.FileNotFoundException: /external_path/DCIM/Camera/20180801_115654(1).jpg (No such file or directory) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(FileInputStream.java:146) at java.io.FileInputStream.(FileInputStream.java:99) at android.content.ContentResolver.openInputStream(ContentResolver.java:708) at com.insypro.inspector3.ui.custom.picturedraw.DrawImageView.rotateImageToTakenDirection(DrawImageView.kt:108) at com.insypro.inspector3.ui.custom.picturedraw.DrawImageView.setImageUri(DrawImageView.kt:54) at com.insypro.inspector3.ui.picture.PictureViewActivity.showPicture(PictureViewActivity.kt:124) at com.insypro.inspector3.ui.picture.PicturePresenter.processPicture(PicturePresenter.kt:69) at com.insypro.inspector3.ui.picture.PicturePresenter.access$processPicture(PicturePresenter.kt:24) at com.insypro.inspector3.ui.picture.PicturePresenter$loadDataPicture$1.accept(PicturePresenter.kt:51)
我还有一个 fileprovider 文件和 list 中所需的一切:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"
tools:replace="android:resource"/>
</provider>
和路径:
<paths>
<files-path name="files" path="." />
<cache-path name="cache" path="/"/>
<external-path name="external_path" path="." />
<external-files-path name="external_files" path="." />
最佳答案
嗯,首先你需要了解 FileProvider 在哪里 是为了。它用于以安全的方式将本地文件共享给其他 应用程序:https://developer.android.com/reference/android/support/v4/content/FileProvider
您只在您自己的 应用程序中使用图像文件。因此根本不需要使用 FileProvider
。
要显示图像,请使用 BitmapFactory
,注意 your_path
是图像的完整路径,不会被 FileProvider
更改:
val bitmap = BitmapFactory.decodeFile(your_path);
ivDraw.setImageBitmap(bitmap);
关于android - java.io.FileNotFoundException : fileprovider 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51633308/