Search code examples
javaandroidgoogle-signinskia

Skia error when trying to retrieve a bitmap downloaded from Google Sign-in


So, I am downloading the profile picture from the Google SIgn-in api and I save it to a hidden file. The problem is that when I try to retrieve it, it throws me: D/skia: --- Failed to create image decoder with message 'unimplemented'. However when I retrieve an image from FireBaseStorage and save that one to the hidden file I can retrieve it whithout any problems.

I tried BitmapFactory.decodeByteArray(), but then I had a message telling me skia wasn't able to decode the file and it returned null.

The method I use to retrieve the profile picture and call the method that will save the file

private void getUsersPic() {
        Bitmap profilePic;

        try {
            InputStream in = new URL(AppData.getUser().getPicture()).openConnection().getInputStream();
            profilePic = BitmapFactory.decodeStream(in);

            int size = profilePic.getRowBytes()*profilePic.getHeight();

            ByteBuffer b = ByteBuffer.allocate(size);
            byte[] bytes = new byte[size];

            profilePic.copyPixelsToBuffer(b);

            b.position(0);
            b.get(bytes, 0, bytes.length);

            SaveBitmapToFile.saveBitmap(bytes , AppData.getUser().getName()+AppData.getUser().getLastName());

        } catch(Exception e) {
            System.out.println("Get profile pic: "+e.toString());
        }
    }

Save the file

public static void saveBitmap(byte[] bitmap, String key) {

        String path = AppData.getAppContext().getFilesDir()+"/.image"+"/";

        File fileDir = new File(path);

        if(!fileDir.isDirectory())
            fileDir.mkdirs();


        try {
            File bitmapDir = new File(fileDir+"/"+key);
            bitmapDir.createNewFile();
            FileOutputStream stream = new FileOutputStream(bitmapDir);

            stream.write(bitmap);
            stream.close();

        } catch (IOException e) {
            System.out.println("Problem creating file "+e.toString()+ " Directory: "+fileDir);
        }
    }

Retrieve and return a bitmap

 public static Bitmap getBitmap(String key) {

        File file = new File(AppData.getAppContext().getFilesDir()+"/.image/"+key);

        try {
            BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
            return BitmapFactory.decodeStream(buf);//BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

        } catch(Exception e) {
            System.out.println("Exception getting bitmap: "+e.toString());
            return null;
        }
    }

The last method should return a Bitmap and it is doing it. It is just not working when the image comes from the Google Sign-in api.


Solution

  • As pskink said in the comment of the post, I had to use compress() instead of copyPixelToBuffer(). Here is my updated method:

    private void getUsersPic() {
            Bitmap profilePic;
    
            try {
                InputStream in = new URL(AppData.getUser().getPicture()).openConnection().getInputStream();
                profilePic = BitmapFactory.decodeStream(in);
    
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                profilePic.compress(Bitmap.CompressFormat.PNG, 100, stream);
    
                SaveBitmapToFile.saveBitmap(stream.toByteArray() , AppData.getUser().getName()+AppData.getUser().getLastName());
    
            } catch(Exception e) {
                System.out.println("Get profile pic: "+e.toString());
            }
        }