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

3

我正在编写一个与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下载的是大尺寸的个人资料图片,然后只将它们缩小到小尺寸吗?如果是这样,我该如何设置它只下载小尺寸的图片呢?

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

谢谢


也许你也应该回收你的图像。 - Renan Bandeira
嗨,LoneDuck,我也遇到了同样的问题...如果你解决了这个问题,能帮帮我吗? - sandeep
1个回答

0
你应该通过缩小下载的图像来减少内存消耗。
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/

希望能对你有所帮助 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接