将图像从Parse加载到图像视图

时间:2015-10-02 06:40:32

标签: android parse-platform

我将ParseFile加载到arraylist照片中。 但现在我想在ImageView中显示它?

ArrayList<Photo> photos = new ArrayList<>();
    try {
        ParseQuery<ParseObject> query = new ParseQuery<>("photo");
        query.setLimit(2000);
        List<ParseObject> listA = query.find();
        for (ParseObject mPhoto : listA) {
            Photo newPhoto = new Photo();
            newPhoto.setImage((ParseFile) mPhoto.get("imageFile"));
            newPhoto.setTrade_id((String) mPhoto.get("trade_id"));
            photos.add(newPhoto);
        }
    } catch (com.parse.ParseException e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }

我怎么做?

3 个答案:

答案 0 :(得分:3)

试试这段代码。这将向您展示如何为1 image显示1 ParseFile。如果您想在arraylist中显示所有图片,则应将Bitmap object保存到arraylist而不是ParseFile object

 ParseFile fileObject = (ParseFile) object
    .get("imageFile");
fileObject
    .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);

                // initialize
                ImageView image = (ImageView) findViewById(R.id.image);

                // Set the Bitmap into the
                // ImageView
                image.setImageBitmap(bmp);

            } else {
                Log.d("test",
                    "Problem load image the data.");
            }
        }
    });

另一种方法是你需要获得图像的网址 String imageUrl = parseFileObject.getUrl();

然后使用PicassoUniversalImageLoader作为加载图片

注意:您应该将AsyncTask与Parse一起用于提高性能和同步

希望这个帮助

答案 1 :(得分:1)

你有图像数组,所以如果要显示所有图像,请将ArrayList<photo>传递给CustomAdpater,然后使用ListView即可显示整个内容

像 的 list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <ImageView
        android:id="@+id/icon"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:contentDescription="@string/image"
        android:paddingLeft="10dp"
        android:paddingRight="10dp" />
</RelativeLayout>

CustomAdpater.java

public class CustomAdpater extends ArrayAdapter<RowItem> {

    Context context;

    public CustomAdpater(Context context, int resourceId,
            ArrayList<Photo> items) {
        super(context, resourceId, items);
        this.context = context;
    }

    /*private view holder class*/
    private class ViewHolder {
        ImageView imageView;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        Photo rowItem = getItem(position);

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();


        holder.imageView.setImageResource(rowItem.getImage());

        return convertView;
    }
}

答案 2 :(得分:1)

简单地将图像设置为imageView的方法是设置位图变量。使用Photo类的Getter方法并将其设置为Bitmap变量。

Bitmap yourImage = newPhoto.getImage(); //Gets the image and set to yourImage variable
ImageView imageView = (ImageView) findViewById(R.id.yourId)
imageView.setImageBitmap(yourImage);

您可以根据需要使用BitmapFactory类的属性来解码和存储变量..