gpt4 book ai didi

android - ActivityResultContracts.TakePicture 返回 "No such file or directory"

转载 作者:行者123 更新时间:2023-12-04 23:46:11 26 4
gpt4 key购买 nike

在 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()但我不知道我做错了什么。这是我所做的一切:
关注 this docks为了体现我添加:
<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 provided content-Uri.Returns true if the image was saved into the given Uri.


那么为什么我得到 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"
  • list :
  • <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被授予。如果没有请求许可。
  • 在 fragment 中创建 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/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com