1

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?

Loulou ChabChab
  • 71
  • 1
  • 10

1 Answers1

0

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'


I don't know the detail, but I recommend you check this Get absolute path of android drawable image

jef
  • 536
  • 1
  • 4
  • 14
  • The method MediaStore.Images.Media.insertImage's seconds parameter is also Bitmap source :( – Loulou ChabChab Apr 09 '20 at 11:49
  • Wait, I just realized it too, seems weird. But the one with `imagePath: String` is the one which throw `FileNotFoundException`. The one with `Bitmap` won't throw Exception, instead will return `The URL to the newly created image, or null if the image failed to be stored for any reason` – jef Apr 09 '20 at 12:01
  • How can i saved the second ? XD – Loulou ChabChab Apr 09 '20 at 19:14