如何从ParseObject获取图像?

时间:2015-07-05 22:20:01

标签: android image parse-platform

我正在使用此代码从ParseObject获取姓名和年龄的字符串 在Parse。我以为我也可以为图像文件做同样的事情。但是这段代码对图像不起作用。我可能做错了。或者我可能需要做一些完全不同的事情。有人可以帮助我从parseObject获取我的图像文件吗? 谢谢

public class MyAdapter extends ArrayAdapter<ParseObject> {

protected Context mContext;
protected List<ParseObject> MyPerson;

public MyAdapter (Context context, List<ParseObject> MyPerson){
    super(context, R.layout.scustomlayout, MyPerson);
    mContext = context;
    mPerson = MyPerson;
}

@Override
public View getView (final int position, View convertView, ViewGroup Parent){
    final ViewHolder holder;

    if(convertView == null) {
      convertView = LayoutInflater.from(mContext).inflate(R.layout.personcustomlayout, null);
        holder = new ViewHolder();
        holder.NameMain = (TextView) convertView.findViewById(R.id.NameHP);
        holder.AgeMain = (TextView) convertView.findViewById(R.id.AgeHP);
        holder.ImageMain = (ImageView) convertView.findViewById(R.id.ImageHP);

        convertView.setTag(holder);


    }else {
        holder = (ViewHolder) convertView.getTag();
    }

        ParseObject personObject = mPerson.get(position);


        //name
        String name = personObject.getString("Name");
        holder.NameMain.setText(name);

        //Age
        String age = personObject.getString("Age");
        holder.AgeMain.setText(age);

        ParseFile image = (ParseFile) personObject.get("Image");
        image.getDataInBackground(new GetDataCallback() {
            public void done(byte[] data, ParseException e) {
                if (e == null) {
                    // Decode the Byte[] into bitmap
                    Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
                    // Set the Bitmap into the imageView
                    holder.ImageMain.setImageBitmap(bmp);
                } else {
                    Log.d("test", "There was a problem downloading the data.");
                }
            }
        });

    return convertView;
}

public static class ViewHolder {
    ImageView ImageMain;
    TextView NameMain;
    TextView AgeMain;
}
}

2 个答案:

答案 0 :(得分:3)

ParseFile image = (ParseFile) personObject.get("Image");
image.getDataInBackground(new GetDataCallback() {
    public void done(byte[] data, ParseException e) {
        if (e == null) {
            // Decode the Byte[] into bitmap
            Bitmap bmp = BitmapFactory.decodeByteArray(data, 0,data.length);
            // Set the Bitmap into the imageView
            holder.ImageMain.setImageBitmap(bmp);
        } else {
            Log.d("test", "There was a problem downloading the data.");
        }
    }
});

另外,将此添加到导入:

import com.parse.ParseException;

答案 1 :(得分:2)

更简单,更好的选择是获取文件URL并通过第三方库(例如PicassoGlide)将其加载到ImageView中。这样,您的代码就不那么麻烦了,ListView会顺利滚动,而且您不会经常遇到OOM错误。

ParseFile image = (ParseFile) personObject.get("Image");
String url = image.getUrl();

// With Picasso
Picasso.with(mContext).load(url).into(holder.ImageMain);

// With Glide
Glide.with(mContext).load(url).into(holder.ImageMain);

无需创建新的String对象url,您可以直接创建它,我只是为了让它更容易理解。

此外,在运行ParseQuery时将URL保存到MyPerson对象会更好,这会使您的图像加载速度更快