gpt4 book ai didi

android - (Android)(Facebook) 多个 ProfilePictureViews 导致 OutOfMemoryError

转载 作者:太空狗 更新时间:2023-10-29 15:09:28 24 4
gpt4 key购买 nike

我正在编写一个与 Facebook 集成的应用程序。我的应用程序可以选择显示好友列表。

列表中的每一项都有一个ProfilePictureView:

<com.facebook.widget.ProfilePictureView
android:id="@+id/fli_profilePicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:is_cropped="true"
app:preset_size="small" />

然后列表适配器将用户的 id 设置到每个项目的 ProfilePictureView 中:

    ProfilePictureView profilePicture = (ProfilePictureView) view.findViewById(R.id.fli_profilePicture);        
profilePicture.setProfileId(friendsList.get(position).getId());

但是这样一来,如果滚动过多,ListView 会抛出 OutOfMemoryError

是不是因为 ProfilePictureView 下载了大版本的个人资料图片,然后只将它们缩小了?如果可以,如何设置只下载小尺寸的图片?

或者还有其他方法可以解决这个问题吗?

谢谢

最佳答案

您可能应该通过缩小下载的图像来减少内存消耗

private Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);

//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE=70;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}

//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}

这里有一个很好的教程,可以在 ListView 中下载图像 http://www.technotalkative.com/android-asynchronous-image-loading-in-listview/

希望对你有帮助:)

关于android - (Android)(Facebook) 多个 ProfilePictureViews 导致 OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18182146/

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