- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 Fragment 中,我尝试从相机中捕获图像并使用它。我想用 ActivityResultContracts.TakePicture()
但是当我在捕获后尝试使用此图像时,我得到:
W/ImageView: Unable to open content: content://com.myniprojects.pixagram/my_images/default_image.jpg
java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)
我看过这个问题
ActivityResultContracts.TakePicture()但我不知道我做错了什么。这是我所做的一切:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.myniprojects.pixagram"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
在
res/xml
我创建了文件
file_paths
:
<paths>
<files-path
name="my_images"
path="images/" />
</paths>
在 fragment 中:
class AddFragment : Fragment(R.layout.fragment_add)
{
private lateinit var imagePath: File
private lateinit var newFile: File
private lateinit var uri: Uri
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
{
super.onViewCreated(view, savedInstanceState)
//...
imagePath = File(requireContext().filesDir, "images")
newFile = File(imagePath, "default_image.jpg")
uri = getUriForFile(
requireContext(),
requireContext().applicationContext.packageName,
newFile
)
binding.butMakeNewImage.setOnClickListener {
takePicture.launch(uri)
}
}
private val takePicture = registerForActivityResult(ActivityResultContracts.TakePicture()) { isSaved ->
if (isSaved)
{
binding.imgSelected.setImageURI(uri)
}
}
}
如果有帮助,这是完整的错误日志
2021-01-16 16:15:38.871 9433-9433/com.myniprojects.pixagram W/ImageView: Unable to open content: content://com.myniprojects.pixagram/my_images/default_image.jpg
java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)
at android.os.ParcelFileDescriptor.openInternal(ParcelFileDescriptor.java:315)
at android.os.ParcelFileDescriptor.open(ParcelFileDescriptor.java:220)
at androidx.core.content.FileProvider.openFile(FileProvider.java:566)
at android.content.ContentProvider.openAssetFile(ContentProvider.java:1740)
at android.content.ContentProvider.openTypedAssetFile(ContentProvider.java:1922)
at android.content.ContentProvider.openTypedAssetFile(ContentProvider.java:1989)
at android.content.ContentProvider$Transport.openTypedAssetFile(ContentProvider.java:539)
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1698)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1514)
at android.content.ContentResolver.openInputStream(ContentResolver.java:1198)
at android.graphics.ImageDecoder$ContentResolverSource.createImageDecoder(ImageDecoder.java:282)
at android.graphics.ImageDecoder.decodeDrawableImpl(ImageDecoder.java:1743)
at android.graphics.ImageDecoder.decodeDrawable(ImageDecoder.java:1736)
at android.widget.ImageView.getDrawableFromUri(ImageView.java:1023)
at android.widget.ImageView.resolveUri(ImageView.java:992)
at android.widget.ImageView.setImageURI(ImageView.java:561)
at androidx.appcompat.widget.AppCompatImageView.setImageURI(AppCompatImageView.java:120)
at com.myniprojects.pixagram.ui.fragments.AddFragment$takePicture$1.onActivityResult(AddFragment.kt:105)
at com.myniprojects.pixagram.ui.fragments.AddFragment$takePicture$1.onActivityResult(AddFragment.kt:30)
at androidx.activity.result.ActivityResultRegistry.doDispatch(ActivityResultRegistry.java:361)
at androidx.activity.result.ActivityResultRegistry.dispatchResult(ActivityResultRegistry.java:321)
at androidx.activity.ComponentActivity.onActivityResult(ComponentActivity.java:631)
at androidx.fragment.app.FragmentActivity.onActivityResult(FragmentActivity.java:164)
at android.app.Activity.dispatchActivityResult(Activity.java:8412)
at android.app.ActivityThread.deliverResults(ActivityThread.java:5580)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5628)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2473)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:8347)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
2021-01-16 16:15:38.871 9433-9433/com.myniprojects.pixagram W/ImageView: resolveUri failed on bad bitmap uri: content://com.myniprojects.pixagram/my_images/default_image.jpg
在
docs我们可以阅读
An
ActivityResultContract
to take a picture saving it into the providedcontent-Uri
.Returnstrue
if the image was saved into the givenUri
.
true
尝试使用 Uri 中的此图像后,何时出现错误?
最佳答案
我终于想出了怎么做。我不确定,但我认为问题中的文件/Uri 有问题。以下是如何捕获图像的分步解决方案。
implementation "androidx.activity:activity-ktx:1.2.0-rc01"
implementation "androidx.fragment:fragment-ktx:1.3.0-rc01"
implementation "com.github.bumptech.glide:glide:4.11.0"
<uses-permission android:name="android.permission.CAMERA" />
<application>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
</application>
res/xml/file_paths.xml
(com.example.name
应该改为正确的包名)<paths>
<external-path name="my_images"
path="Android/data/com.example.name/files/Pictures" />
</paths
Manifest.permission.CAMERA
被授予。如果没有请求许可。ActivityResultLauncher
像这样:private lateinit var uri: Uri
private val takePicture = registerForActivityResult(ActivityResultContracts.TakePicture()) { isSaved ->
if (isSaved)
{
glide
.load(uri)
.into(binding.imgSelected)
}
}
takePicture
(例如点击按钮)binding.butMakeNewImage.setOnClickListener {
val photoFile = File.createTempFile(
"IMG_",
".jpg",
requireContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES)
)
uri = FileProvider.getUriForFile(
requireContext(),
"${requireContext().packageName}.provider",
photoFile
)
takePicture.launch(uri)
}
permission denial
API 级别低于
21
,尝试这样做:
val resultLauncher = registerForActivityResult(
object : ActivityResultContracts.TakePicture()
{
override fun createIntent(
context: Context,
input: Uri
): Intent
{
val intent = super.createIntent(context, input)
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP)
{
intent.clipData = ClipData.newRawUri("", input)
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
return intent
}
}) { success ->
if (success)
{
// TODO
}
}
resultLauncher.launch(photoUri)
关于android - ActivityResultContracts.TakePicture 返回 "No such file or directory",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65751435/
在 androidx.activity 版本 1.2.0-alpha05 中,TakePicture 合约的 API 已更改: The TakePicture contract now returns
我可以找到很多答案,such as this one ,关于如何使用 Java 和旧版 API 创建让用户在相机和画廊之间进行选择的 Intent 。 然而,虽然它是 relatively simpl
我可以找到很多答案,such as this one ,关于如何使用 Java 和旧版 API 创建让用户在相机和画廊之间进行选择的 Intent 。 然而,虽然它是 relatively simpl
我想创建一个自定义 ActivityResultContract 来请求具有自定义响应的粗略和精细位置。 class LocationPermission : ActivityResultContra
使用 androids 新的 Activity 结果 API 时,回调只为我触发一次。 class MyFragment : Fragment() { private val intentLau
根据this documentation from Google about launching an activity to get a result : While the underlying
在 Fragment 中,我尝试从相机中捕获图像并使用它。我想用 ActivityResultContracts.TakePicture()但是当我在捕获后尝试使用此图像时,我得到: W/ImageV
新registerForActivityResult方法使得请求权限变得非常简单,如下所示: registerForActivityResult(ActivityResultContracts.Req
截至1.2.0-beta01的 androidx.activity:activity-ktx ,不能再launch使用 Activity.registerForActivityResult() 创建的
我是一名优秀的程序员,十分优秀!