Android 插入图片 / 没有此文件或目录 /

huangapple go评论88阅读模式
英文:

Android Insert Image / No such File or Directory /

问题

我正在尝试将目录 "drawable" 中的图片插入到我的相册,但无法成功。

dataset1Image1 = BitmapFactory.decodeResource(getResources(), R.drawable.dataset1soleil);
MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), dataset1Image1, null, null);

我得到了这个错误:

插入图片失败
    java.io.FileNotFoundException: 没有这个文件或目录
        at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:144)
        at android.content.ContentProviderProxy.openAssetFile(ContentProviderNative.java:621)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1009)
        at android.content.ContentResolver.openOutputStream(ContentResolver.java:751)
        at android.content.ContentResolver.openOutputStream(ContentResolver.java:727)
        at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:995)
        at ***com.example.testbackup.ui.dataset1.Dataset1Fragment.createOnClicBtnEnreg(Dataset1Fragment.java:217)
        at com.example.testbackup.ui.dataset1.Dataset1Fragment.access$300(Dataset1Fragment.java:49)
        at com.example.testbackup.ui.dataset1.Dataset1Fragment$1.onClick(Dataset1Fragment.java:109)***
        at android.view.View.performClick(View.java:5721)
        at android.widget.TextView.performClick(TextView.java:10955)
        at android.view.View$PerformClick.run(View.java:22620)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:7331)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

为什么找不到该文件?

英文:

I'm trying to insert a picture from my directory "drawable" into my gallery but it does not work.

dataset1Image1=BitmapFactory.decodeResource(getResources(),R.drawable.dataset1soleil );
                MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), dataset1Image1,null, null);

I got this error :

Failed to insert image
    java.io.FileNotFoundException: No such file or directory
        at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:144)
        at android.content.ContentProviderProxy.openAssetFile(ContentProviderNative.java:621)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1009)
        at android.content.ContentResolver.openOutputStream(ContentResolver.java:751)
        at android.content.ContentResolver.openOutputStream(ContentResolver.java:727)
        at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:995)
        at ***com.example.testbackup.ui.dataset1.Dataset1Fragment.createOnClicBtnEnreg(Dataset1Fragment.java:217)
        at com.example.testbackup.ui.dataset1.Dataset1Fragment.access$300(Dataset1Fragment.java:49)
        at com.example.testbackup.ui.dataset1.Dataset1Fragment$1.onClick(Dataset1Fragment.java:109)***
        at android.view.View.performClick(View.java:5721)
        at android.widget.TextView.performClick(TextView.java:10955)
        at android.view.View$PerformClick.run(View.java:22620)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:7331)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Why is the file not found?

答案1

得分: 0

以下是翻译好的部分:

方法MediaStore.Images.Media.insertImage的第二个参数是
>imagePath 字符串:要插入的图像的路径)

但你使用了dataset1Image1,它是BitmapFactory.decodeResource返回的Bitmap

这是为了你的问题'为什么找不到文件'

<br>
我不知道细节,但我建议你查看这个链接:https://stackoverflow.com/questions/25758159/get-absolute-path-of-android-drawable-image/

英文:

The method MediaStore.Images.Media.insertImage's seconds parameter is
>imagePath String: The path to the image to insert)`

but you use dataset1Image1 which is BitmapFactory.decodeResource that return Bitmap

That's for your question 'why the file is not found'

<br>
I don't know the detail, but I recommend you check this https://stackoverflow.com/questions/25758159/get-absolute-path-of-android-drawable-image/

huangapple
  • 本文由 发表于 2020年4月8日 20:16:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/61100477.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定