如何使用解析中的所有图像

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用解析中的所有图像相关的知识,希望对你有一定的参考价值。

我需要从解析中提取所有图像。这是logCat中的错误:Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference

错误在这里:

String ObjectId =  ParseObjects.get(position).getObjectId();

这是我的代码:

public void PullImage(final ImageView image,int position){
    progressDialog = ProgressDialog.show(mContext, "", "Downloading Image...", true);
    String ObjectId =  ParseObjects.get(position).getObjectId();
    query.getInBackground(ObjectId, new GetCallback < ParseObject > () {
        public void done (ParseObject object, ParseException e){
            if (object != null) {
                ParseFile file = (ParseFile) object.get("ImageFile");
                file.getDataInBackground(new GetDataCallback() {
                    public void done(byte[] data, ParseException e) {
                        if (e == null) {
                            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                            // surface.addImage(bitmap);

                            // float Xplace  = targetImageforpull.getX();
                            // float Yplace  = targetImageforpull.getY();

                            image.setImageBitmap(bitmap);
                            //use this bitmap as you want
                            progressDialog.dismiss();
                        } else {
                            // something went wrong
                        }
                    }
                });
            } else {
                Toast.makeText(mContext, "Exception", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

我知道我的问题在这里:

public void SetParse(){
    query = new ParseQuery<ParseObject>("User");
    //  query.whereEqualTo("Column", bitmap);
     size = 0;
    try {
        size = query.count();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> objects, ParseException e) {
            if (e == null) {
                ParseObjects = objects;
                // The query was successful.
            } else {
                // Something went wrong.
            }
        }
    });
}

所有代码:

public class CustomGrid extends BaseAdapter {
    private Context mContext;
    private Bitmap btimaprecieve;
    List<ParseObject> ParseObjects;

    ParseQuery<ParseObject> query;

    ProgressDialog progressDialog;

    int size;

    public CustomGrid(Context c) {
        mContext = c;
        SetParse();
    }

    //---returns the number of images---
    public int getCount() {
        return size;
    }

    //---returns the ID of an item---
    public Object getItem(int position) {
        return imageId[position];
    }

    public long getItemId(int position) {
        return position;
    }

    //---returns an ImageView view---
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) { // if it's not recycled, initialize some
            // attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(120, 120));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(25, 25, 25, 25);
        } else {
            imageView = (ImageView) convertView;
        }
        PullImage(imageView,position);
     //   imageView.setImageResource(imageId[position]);

        return imageView;
    }

    public void SetParse(){
        query = new ParseQuery<ParseObject>("User");
        //  query.whereEqualTo("Column", bitmap);
         size = 0;
        try {
            size = query.count();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        query.findInBackground(new FindCallback<ParseObject>() {

            public void done(List<ParseObject> objects, ParseException e) {
                if (e == null) {
                   // ParseObjects = objects;
                     ParseObjects = new ArrayList<ParseObject>(objects);
                    if(ParseObjects == null){
                        Log.d("ParseObjects is null ", "ParseObjects is null");
                    }
                    if(objects == null){
                        Log.d("objects is null ", "objects is null");
                    }

                    // The query was successful.
                } else {
                    // Something went wrong.
                }
            }
        });
    }

    public void PullImage(final ImageView image,int position){

        progressDialog = ProgressDialog.show(mContext, "", "Downloading Image...", true);
        String ObjectId =  ParseObjects.get(position).getObjectId();
        query.getInBackground(ObjectId, new GetCallback < ParseObject > () {
            public void done (ParseObject object, ParseException e){
                if (object != null) {

                    ParseFile file = (ParseFile) object.get("ImageFile");

                    file.getDataInBackground(new GetDataCallback() {


                        public void done(byte[] data, ParseException e) {
                            if (e == null) {

                                Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                                // surface.addImage(bitmap);

                                // float Xplace  = targetImageforpull.getX();
                                // float Yplace  = targetImageforpull.getY();

                                image.setImageBitmap(bitmap);
                                //use this bitmap as you want
                                progressDialog.dismiss();
                            } else {
                                // something went wrong
                            }
                        }
                    });

                } else {
                    Toast.makeText(mContext, "Exception", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private Integer [] imageId = {
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
            R.drawable.like_button,
    };
    public Bitmap add(Bitmap bitmap_recieve) {
        return btimaprecieve;
    }
}
答案

我使用滑动(或任何其他lib(凌空,picasa)来加载URL)

    userQuery.getFirstInBackground(new GetCallback<ParseObject>() {
        @Override
        public void done(ParseObject parseObject, ParseException e) {
            if((ParseFile) parseObject.get("photo")!= null){
                Glide.with(context).load(((ParseFile) parseObject.get("photo")).getUrl()).asBitmap().dontAnimate().centerCrop().transform(new CircleTransform(context))
                        .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(hd.logo);
            }
        }
    });

如果你需要位图,你可以从你的img视图加载后减肥。小心,如果你的回调返回ParseArray,你必须通过像ParseObjects.get(position).getObjectId();的位置得到你的图片对象。但是如果你有ParseObject,你必须编写ParseObjects.getObjectId();(没有get(position))。检查你的回调。

以上是关于如何使用解析中的所有图像的主要内容,如果未能解决你的问题,请参考以下文章

如何从Android片段中的相机获取图像

如何从片段中检索gridview中的图像?

从图库中获取图像以在片段中的图像视图中设置? [复制]

如何跳过查询条件片段中的空对象?

如何在 IOS SDK 中解析网站中的所有图像

如何解决片段中的 Viewpager 问题?