MediaStore.Images.Media.insertImage is returning null when trying to save the image

后端 未结 2 585
心在旅途
心在旅途 2020-12-14 09:22

I am using an custom view and in that i am using an canvas in which a user can draw anything and after that i want to save that image in sd card bt was not able to do that.

2条回答
  •  旧时难觅i
    2020-12-14 09:44

    A bit late answer, but still...

    Not sure which is line 238, but probably the reason is here:

    String imgSaved = MediaStore.Images.Media.insertImage( getContentResolver(), drawView.getDrawingCache(), UUID.randomUUID().toString()+".png", "drawing");

    Since the method is within a click listener getContentResolver may be returning a Resolver different from the one for the application. Either save a private reference to the content resolver, or you could try replacing getContentResolver with MainActivity.this.getContentResolver() :

    String imgSaved = MediaStore.Images.Media.insertImage( MainActivity.this.getContentResolver(), drawView.getDrawingCache(), UUID.randomUUID().toString() + ".png", "drawing");

    Alternatively, it might be a permissioning problem. Make sure in the manifest you have:

    
    

    One final note, the result of insertImage is an URI, imgSaved is not the best name for that variable :)

提交回复
热议问题