使用GridView中的Parse加载图像

时间:2015-11-09 04:49:27

标签: java android parse-platform

我试图创建一个应用程序,它在ParseObject中抓取所有ParseFiles并连续显示在GridView中。

我已经通过一项代表新闻Feed的活动完成了这一切,一切正常。

但是,当我尝试在其他活动中实施相同的方法时,它无法正常工作。

我使用AsyncTask在后台加载图片。

这是我的代码:

ThumbnailItems.java(POJO类)

public class ThumbnailItems {
    Bitmap thumb;
    // Empty constructor.
    public ThumbnailItems(){

    }
    // Constructor.
    public ThumbnailItems(Bitmap thumb){
        super();
        this.thumb = thumb;
    }
    // Getter.
    public Bitmap getImage(){
        return thumb;
    }
    // Setter.
    public void setImage(Bitmap thumb){
        this.thumb = thumb;
    }
}

我的AsyncTask:

private class RemoteDataTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        thumbItemsList = new ArrayList<ThumbnailItems>();
        try {
            ParseQuery<ParseObject> postQuery = new ParseQuery<ParseObject>("Posts");

            postQuery.orderByDescending("createdAt");
            objectList = postQuery.find();
            for (ParseObject posts : objectList){

                final ThumbnailItems thumbItems = new ThumbnailItems();
                // Locate user profile picture.
                ParseFile thumbFile = (ParseFile) posts.get("author_img");
                thumbFile.getDataInBackground(new GetDataCallback() {
                    @Override
                    public void done(byte[] bytes, ParseException e) {
                        if (e == null) {
                            Bitmap userImage = Bitmap.createScaledBitmap(
                                    BitmapFactory.decodeByteArray(bytes, 0, bytes.length)
                                    , 150, 150, false);
                            thumbItems.setImage(userImage);
                        } else {
                            Log.d("ParseException", "Error: " + e.getMessage());
                            e.printStackTrace();
                        }
                    }
                });
                thumbItemsList.add(thumbItems);
            }
        } catch (ParseException e){
            Log.e("ParseException", "Error: " + e.getMessage() + " with code: " + e.getCode());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View activityView = layoutInflater.inflate(R.layout.activity_profile, null, false);
        gridView = (GridView) activityView.findViewById(R.id.gridView);
        gridViewAdapter = new GridViewAdapter(getApplicationContext()
                , R.layout.profile_viewpager_tab1_children
                , thumbItemsList);
        gridView.setAdapter(gridViewAdapter);
    }
}

(我已经看过this教程,但我真的希望与我的代码保持一致)

1 个答案:

答案 0 :(得分:0)

我认为使用Like的击球手。 并且不要使用AsyncTask在后台加载图像。

ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                "ClassName");
        query.findInBackground(new FindCallback<ParseObject>() {

            @Override
            public void done(List<ParseObject> list, ParseException e) {
                // TODO Auto-generated method stub
                if (e == null) {
                    // hear u will get responce.
                    for (ParseObject parseObject : list) {
                        ParseFile image = (ParseFile) parseObject.get(""); // hear
                                                                            // set
                                                                            // column
                                                                            // name
                                                                            // same
                                                                            // as
                                                                            // parse
                                                                            // table
                        if (image != null) {
                            // hear store image into your custom array.
                            // Like...
                            Log.e("get Image ",
                                    " IMAGE URL IS " + image.getUrl());
                        }
                    }

                    // and hear u can set your adapter
                    // and yr grid view
                    // in adapter to use Picasso libary for image shower.
                    // how to use picasso lib.
                    Picasso.with(getApplicationContext()).load("image url")
                            .fit().into(imageview);
                } else {
                    // hear is yr fail responce
                    // e.getMessage()
                }

            }
        });