gpt4 book ai didi

java - 尝试检索从 Google 登录下载的位图时出现 Skia 错误

转载 作者:行者123 更新时间:2023-12-02 10:16:23 25 4
gpt4 key购买 nike

因此,我从 Google SIgn-in 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);
}
}

检索并返回位图

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

关于java - 尝试检索从 Google 登录下载的位图时出现 Skia 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54648483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com