简体   繁体   English

如何从ParseObject获取图像?

[英]How to get Image from ParseObject?

I am using this code to get Strings of names and ages from my ParseObject in Parse. 我正在使用此代码从我在Parse中的ParseObject获取名称和年龄的字符串。 I assumed I can do the same for imagefiles as well. 我以为我也可以对图像文件做同样的事情。 But this code doesn't work for the images. 但是此代码不适用于图像。 I may have done it wrong. 我可能做错了。 or maybe I need to do something totally different. 也许我需要做一些完全不同的事情。 Can someone help me to get my imagefile from parseObject ? 有人可以帮我从parseObject获取图像文件吗? Thank you 谢谢

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;
}
}
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.");
        }
    }
});

Also, add this to the imports: 另外,将其添加到导入中:

import com.parse.ParseException;

An easier and better option would be to get the file URL and load it into the ImageView via a third party library such as Picasso or Glide . 一个更简单,更好的选择是获取文件URL并通过诸如PicassoGlide的第三方库将其加载到ImageView中。 This way, your code is less cumbersome, your ListView will scroll smoothly and your won't encounter OOM error too often. 这样,您的代码就不再那么麻烦,您的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);

There is no need to create the new String object url , you could do it directly, I just did it to make it easier to understand. 无需创建新的String对象url ,您可以直接执行此操作,我只是这样做以使其更易于理解。

Also, it would be better to save the URL to the MyPerson object when you run the ParseQuery, this will make your images load faster 另外,最好在运行ParseQuery时将URL保存到MyPerson对象,这将使图像加载更快

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM