Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试检索从Google登录下载的位图时出错_Java_Android_Google Signin_Skia - Fatal编程技术网

Java 尝试检索从Google登录下载的位图时出错

Java 尝试检索从Google登录下载的位图时出错,java,android,google-signin,skia,Java,Android,Google Signin,Skia,因此,我正在从谷歌登录api下载个人资料图片,并将其保存到一个隐藏文件中。问题是,当我试图检索它时,它抛出我:D/skia:---无法创建带有消息“未实现”的图像解码器。。但是,当我从FireBaseStorage检索图像并将其保存到隐藏文件时,我可以毫无问题地检索它 我尝试了BitmapFactory.decodeByteArray(),但后来我收到一条消息,告诉我skia无法解码该文件,它返回null 我用于检索配置文件图片并调用将保存文件的方法的方法 private void getUse

因此,我正在从
谷歌登录api
下载个人资料图片,并将其保存到一个隐藏文件中。问题是,当我试图检索它时,它抛出我:
D/skia:---无法创建带有消息“未实现”的图像解码器。
。但是,当我从
FireBaseStorage
检索图像并将其保存到隐藏文件时,我可以毫无问题地检索它

我尝试了BitmapFactory.decodeByteArray(),但后来我收到一条消息,告诉我skia无法解码该文件,它返回null

我用于检索配置文件图片并调用将保存文件的方法的方法

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());
        }
    }
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);
        }
    }
保存文件

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());
        }
    }
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);
        }
    }
检索并返回位图

 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;
        }
    }

最后一个方法应该返回位图,并且它正在执行此操作。当图像来自Google登录api时,它就不起作用了

正如pskink在帖子评论中所说,我不得不使用compress()而不是copyPixelToBuffer()。以下是我更新的方法:

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());
        }
    }

试图验证哪些字节被传递到
BitmapFactory#decodeStream
方法?如果是这样的话,那就是前8个字节()?我得到-64-49-42-1-64-52-40-1,这段代码不在维基百科页面上。你不需要
copyPixelsToBuffer()
-你需要
compress()
代替它谢谢!!你能为我的问题写一个答案,这样我就可以把它标记为一个解决方案吗?那么,写一个自我回答,好运气,不要用
ByteArrayOutputStream
-直接使用
FileOutputStream
-只需将
profilePic
传递给
SaveBitmapToFile.saveBitmap
方法,而不是
stream.toByteArray()
my saveBitmap()方法将字节数组作为参数