Android从parse.com检索图像并使用它

时间:2015-08-16 18:47:29

标签: android parse-platform

实际上,我想使用这个库“Aphid-FlipView-Library”来做一个翻转动画。 我要翻转的图片来自Parse.com。 但是这样做时我遇到了问题。 请给我一些指示。谢谢!

首先,我创建一个类来设置和获取图像的位图值。

package com.example.bookard;

import android.graphics.Bitmap;

public class UseForFlip {

   private Bitmap Photo;

     public Bitmap getPhoto(){return Photo;}
     public void setPhoto(Bitmap photo){Photo = photo;}

}

而且,下面的代码是rotate Activity的前半部分。我想从Parse.com检索图像并将其添加到名为“notes”的arraylist中。这个arraylist用于旋转Activity的后半部分。

public class rotate extends Activity {    
    UseForFlip forflip;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.rotate);
         forflip = new UseForFlip();
         ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Card");
         query.getInBackground("wxYBHhRlhZ", new GetCallback<ParseObject>() {

             @Override
             public void done(ParseObject object, ParseException e) {
                 // TODO Auto-generated method stub
                 ParseFile fileObject1 = (ParseFile) object.get("Photoin1");
                 fileObject1.getDataInBackground(new GetDataCallback(){

                     @Override
                     public void done(byte[] data, ParseException e) {
                         // TODO Auto-generated method stub

                        forflip.setPhoto(BitmapFactory.decodeByteArray(data, 0,data.length));
                     }
                 });
             }
         });    

          ArrayList<Bitmap> notes = new ArrayList<Bitmap>();
             notes.add(forflip.getPhoto());

             FlipViewController flipView = new FlipViewController(this, FlipViewController.HORIZONTAL);

             flipView.setAdapter(new NoteViewAdapter(this, notes));

             setContentView(flipView);
     }

以下代码是rotate Activity的后半部分。

public class NoteViewAdapter extends BaseAdapter {
    private LayoutInflater inflater;        
    private ArrayList<Bitmap> notes;

    public NoteViewAdapter(Context currentContext, ArrayList<Bitmap> allNotes) {
        inflater = LayoutInflater.from(currentContext);        
        notes = allNotes;
    }

    @Override
    public int getCount() {
        return notes.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View layout = convertView;

        if (convertView == null) {
            layout = inflater.inflate(R.layout.rotate, null);
        }            


        Bitmap note = notes.get(position);

        ImageView tView = (ImageView) layout.findViewById(R.id.picture);

        tView.setImageBitmap(note);

        return layout;
    }
}

我运行代码,但tView中没有任何内容。

我唯一想通知的是,如何将Parse.com中的图像放入arraylist

1 个答案:

答案 0 :(得分:0)

您需要在回调中移动该部分。

     ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Card");
     query.getInBackground("wxYBHhRlhZ", new GetCallback<ParseObject>() {

         @Override
         public void done(ParseObject object, ParseException e) {
             // TODO Auto-generated method stub
             ParseFile fileObject1 = (ParseFile) object.get("Photoin1");
             fileObject1.getDataInBackground(new GetDataCallback(){

                 @Override
                 public void done(byte[] data, ParseException e) {
                     // TODO Auto-generated method stub
                     if(e == null){
                         Bitmap bmp1 = BitmapFactory.decodeByteArray(data, 0,data.length);    
                         ddd.setPhoto(bmp1);
                         ImageView img=(ImageView)findViewById(R.id.xxx);
                         img.setImageBitmap(ddd.getPhoto());
                     }else{
                         Log.d("test","There was a problem downloading the data.");
                     }
                 }

             });
         }
     });    
 }

如您所见,无需使用此ddd课程。

替代解决方案:使用<com.parse.ParseImageView>。用法非常简单,即:

ParseImageView view = (ParseImageView) findViewById(R.id.parse);
ParseFile fileObject1 = ... //file you get from the query
view.setParseFile(fileObject1);
view.loadInBackground();

根据您的修改,我建议:

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.rotate);
     FlipViewController flipView = new FlipViewController(this, FlipViewController.HORIZONTAL);
     NoteViewAdapter mAdapter = new NoteViewAdapter(this);
     flipView.setAdapter(mAdapter);
     setContentView(flipView); //why are you calling setContentView twice? 
     ArrayList<Bitmap> notes = new ArrayList<Bitmap>();

     ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Card");
     query.getInBackground("wxYBHhRlhZ", new GetCallback<ParseObject>() {

         @Override
         public void done(ParseObject object, ParseException e) {
             ParseFile fileObject1 = (ParseFile) object.get("Photoin1");
             fileObject1.getDataInBackground(new GetDataCallback(){

                 @Override
                 public void done(byte[] data, ParseException e) {
                     notes.add(BitmapFactory.decodeByteArray(data,0,data.length));
                     mAdapter.setNotes(notes);
                 }
             });
         }
     });    
 }

然后在适配器中创建此方法:

public void setNotes(ArrayList notes) {
    this.notes = notes;
    notifyDataSetChanged();
}