0

我的 ImageLoader 类-------->

    private Bitmap decodeFile(File f){
        try {
            //decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);


            //Find the correct scale value. It should be the power of 2.

            final int REQUIRED_SIZE=Wall.width;
            final int REQUIRED_SIZE1=Wall.height;
//            final int REQUIRED_SIZE=250;
//         
            int width_tmp=o.outWidth, height_tmp=o.outHeight;
            int scale=1;
//            while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
////                scale*=2;
            while(true){
                if((width_tmp/2<REQUIRED_SIZE)||(height_tmp/2<REQUIRED_SIZE1))
                    break;
                width_tmp/=2;
                height_tmp/=2;
                scale*=2;
            }

            //decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
            o2.inSampleSize=scale;
            o2.inJustDecodeBounds = false;
//            o2.inSampleSize=2;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);


        } catch (FileNotFoundException e) {}
        return null;
    }

我想使用图形 api 在我的应用程序中显示 facebook 墙流。

墙流包含以下内容 1) 个人资料图片 2) 名称、发布的消息、发布的图像 3) 喜欢、评论选项 我从 facebook graph api https://graph.facebook.com/me/home?access_token= "+访问令牌;

在这个我得到了 page1 的数据,我使用“分页”来获取第二页的数据,同样用于获取第三页。

但是当我尝试显示第一页时,因为我得到了更多的图像

Bitmap size exceeds VM Budget error:Out of Memory error

我想知道,其他 facebook 应用程序如何管理这个问题,我观察到他们一次只加载较少数量的记录。如果我以分页方式获取数据,如何做这些事情?

public class Wall extends Activity {

    private Button btnSettings,btnLogout,btnUpdate,btnWall,btnCheckin,btnPubfinder,btnCamera;

    public String accesstoken,wallres,str_likevariable="",webserv,currentweb;

    protected static JSONArray jsonArray;
    JSONObject jsonObject = null;
    MyProgressDialog dialog;


     private PullToRefreshListView listView;

     ArrayList<String> msg=new ArrayList<String>();
     ArrayList<String> name=new ArrayList<String>();
     ArrayList<String> id=new ArrayList<String>();
     ArrayList<String> objid=new ArrayList<String>();
     ArrayList<String> profimg=new ArrayList<String>();
     ArrayList<String> img=new ArrayList<String>();
     ArrayList<String> pic=new ArrayList<String>();
     ArrayList<String> comment=new ArrayList<String>();
     ArrayList<String> likes=new ArrayList<String>();
     ArrayList<String> weburl=new ArrayList<String>();
     ArrayList<String> like_or_unlike=new ArrayList<String>();
     ArrayList<String> date=new ArrayList<String>();




     ImageLoader imageLoader;
     Handler mHandler;
     View footerView;

     private int previousTotal = 0,j=0;

     public  static int width,height;
     public boolean loading = true,isScrolling,scroll=true,boolisLoad=true,boolDialog=true,addFooter=false;
     MySimpleArrayAdapter adapter;

     static {


       StrictMode.setThreadPolicy( new StrictMode.ThreadPolicy.Builder().permitAll().build());
   }


  //onCreate 
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.wall);

        //To get device dimensions
        android.view.WindowManager w = getWindowManager(); 
        Display d = w.getDefaultDisplay(); 
       width = d.getWidth(); 
        height = d.getHeight(); 
      System.out.println(width);
      System.out.println(height);

        btnSettings=(Button)findViewById(R.id.settings);
        btnLogout=(Button)findViewById(R.id.logout);
        btnCamera=(Button)findViewById(R.id.camera);
        btnUpdate=(Button)findViewById(R.id.update);
        btnWall=(Button)findViewById(R.id.wall);
        btnCheckin=(Button)findViewById(R.id.checkin);
        btnPubfinder=(Button)findViewById(R.id.pubfinder);

       ButtonListener listener=new ButtonListener();

       btnSettings.setOnClickListener(listener);
       btnLogout.setOnClickListener(listener);
       btnUpdate.setOnClickListener(listener);
       btnWall.setOnClickListener(listener);
       btnCheckin.setOnClickListener(listener);
       btnPubfinder.setOnClickListener(listener);
       btnCamera.setOnClickListener(listener);
       //access token 
        accesstoken=Login.mFacebook.getAccessToken();

        Log.e("accesstoken",accesstoken);

        //first page webservice
        webserv="https://graph.facebook.com/me/home?access_token="+accesstoken;
        Log.e("firstweb",webserv);

        //pullToRefresh Listview
        listView= (PullToRefreshListView ) findViewById(R.id.walldata);
        listView.setDivider(null);
        //footer view
        footerView = ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listfooter, null, false);
//        this.mHandler = new Handler();
        listView.addFooterView(footerView);
//
//        this.mHandler.postDelayed(m_Runnable,5000);


        SessionStore.restore(Login.mFacebook, Wall.this);

        SessionEvents.removeAllAuthListener();




        listView.setOnRefreshListener(new OnRefreshListener() {

//          @Override
            public void onRefresh() {
                // Your code to refresh the list contents goes here

                scroll=true; //to keep the scroll at position where it has hits the load more data 

                pic.clear();
                id.clear();
                name.clear();
                msg.clear();
                img.clear();
                profimg.clear();      
                objid.clear();
                comment.clear();
                weburl.clear();
                adapter.clear();
                likes.clear();
                like_or_unlike.clear();
                date.clear();

                addFooter=true; // to add the footer view again after removing in pullToRefresh

                 previousTotal = 0;
                 loading = true;

                listView.removeFooterView(footerView);
                listView.setAdapter(null);

                j=0;

                webserv="https://graph.facebook.com/me/home?access_token="+accesstoken;
                 Log.e("inpull",webserv);

                System.out.println(listView.getCount());
                 doInBack dob=new doInBack();
                 dob.execute(); 
                System.out.println(listView.getCount());

                    Log.e("hi","doback called");


            }
        });
//        


        listView.setOnScrollListener(new  OnScrollListener() {

            private int threshold = 0;
//          
            public void onScrollStateChanged(AbsListView view, int scrollState) {
////                
                  if (scrollState != 0) {  
                    isScrolling = true; 
                }
                else {   
                   isScrolling = false;  //To load the data when the scroll is in offstate
                  adapter.notifyDataSetChanged();


                }  

            }


          public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) 
          {     


              // TODO Auto-generated method stub

              if (loading) {
                  if (totalItemCount > previousTotal) {
                    Log.e("in loading","in load");
                      loading = false;
                      previousTotal = totalItemCount;
                  }
              } 

                   if (!loading && (firstVisibleItem + visibleItemCount+2) == totalItemCount+1){


                       System.out.println(firstVisibleItem);
                       System.out.println(visibleItemCount);
                       System.out.println(totalItemCount);
                       scroll=false;

                       Log.v("in gridview loading more","grid load");
//                      
                       doInBack dob=new doInBack();
                       dob.execute();  
                       adapter.notifyDataSetChanged();
//                       doback(webserv);
                    loading = true;
                   }
              }
//          }



      });

        doInBack dob=new doInBack();
            dob.execute();     
//        doback(webserv);
        Log.e("hi","doback called");



        }
/

    private class ButtonListener implements View.OnClickListener{

        public void onClick(View v) {
            if(v.equals( btnSettings)){
            Intent myintent=new Intent(Wall.this,Settings.class);
            startActivity(myintent);
            finish();
            }

        else if(v.equals(btnLogout)){
            try {
                String res=Login.mFacebook.logout(Wall.this);

                Log.e("response",res);
                System.out.println(res);
                System.out.println(Login.mFacebook.isSessionValid());

                //if(res.equals("true"))
                //{
                    Intent in=new Intent(Wall.this,Login.class);
                    startActivity(in);
                    finish();
                //}
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }

        else if(v.equals(btnUpdate)){
            Update.i=0;
            Intent myintent=new Intent(Wall.this,Update.class);
            startActivity(myintent);
            finish();
            }   


        else if(v.equals(btnCamera)){
            Intent myintent=new Intent(Wall.this,CameraActivity.class);
            startActivity(myintent);
            finish();
            }


        else if(v.equals(btnWall)){
//          Intent myintent=new Intent(Wall.this,Wall.class);
//          startActivity(myintent);
//          finish();
            }


        else if(v.equals(btnCheckin)){
            Intent myintent=new Intent(Wall.this,Checkin.class);
            startActivity(myintent);
            finish();
            }


        else if(v.equals(btnPubfinder)){
            Intent myintent=new Intent(Wall.this,PubFinder.class);
            startActivity(myintent);
            finish();
            }

        }
    }

    class doInBack extends AsyncTask<URL, Integer, Long>
    {
        protected void onPreExecute() 
        { if(boolDialog){
            dialog=MyProgressDialog.show(Wall.this, null,null);
        }
        }
//      currentweb= webserv;
        @Override
        protected Long doInBackground(URL... arg0) {

            currentweb=webserv;



//      
            Log.e("hi","doback parsing");


            try
            {   
//              if(urlval>0){
                    wallres=UrltoValue.getValuefromUrl(currentweb);
                    Log.e("wallrespages",wallres);
    }
                JSONObject jobj1=new JSONObject(wallres);

                JSONObject jobj2=jobj1.getJSONObject("paging");
                webserv= jobj2.getString( "next");

                 jsonArray = jobj1.getJSONArray("data");


                 for(int i=0;i<jsonArray.length();i++){

                     jsonObject = jsonArray.getJSONObject(i);





                    if(jsonObject.has("message")||jsonObject.has("picture")) {

                        try{


//                        msg[j]=jsonObject.getString("message");
                            if(jsonObject.has("message"))
                            {

                                msg.add(jsonObject.getString("message"));   
                            }
                            else{
                                msg.add("");    
                            }
                        }
                        catch(Exception e){
                            e.printStackTrace();
                        }

                        try{

//                            msg[j]=jsonObject.getString("message");
                                if(jsonObject.has("picture"))
                                {
                                    String firstpicture=jsonObject.getString("picture");
                                    String secondpicture=firstpicture.replaceAll("_s.jpg", "_n.jpg");
                                     Log.e("picurl",secondpicture);
                                     pic.add(secondpicture);

                                }
                                else{
                                    pic.add("");
                                }
                            }
                            catch(Exception e){
                                e.printStackTrace();
                            }





                        objid.add(jsonObject.getString("id"));



                          JSONObject jobj=jsonObject.getJSONObject("from");
                           name.add(jobj.getString("name"));
                           id.add(jobj.getString("id"));

                           if(jsonObject.getString("type").equals("checkin")){
                               name.set(i,jobj.getString("name")+" "+"is at"+" "+jsonObject.getString("name"));
                            }
                       Log.e("id",id[j]);
                           profimg.add("http://graph.facebook.com/"+id.get(j)+"/picture?type=square");

                           JSONObject commentjobj=jsonObject.getJSONObject("comments");
                            comment.add(commentjobj.getString("count"));

                            if(jsonObject.has("likes")) {

                                   Log.e("likeornot","likre");
                                JSONObject likesjobj=jsonObject.getJSONObject("likes");
                                likes.add(likesjobj.getString("count"));

                               String postid=jsonObject.getString("id");
//                                  graph_or_fql = "fql";
                                    String query = "SELECT likes.user_likes FROM stream WHERE post_id = \'" + postid + "'";

//                                                        Log.d("finalThreadID", finalThreadID);
                                    Bundle params = new Bundle();
                                    params.putString("method", "fql.query");
                                    params.putString("query", query);
//                                                        Utility.mAsyncRunner.request(null, params, new LikesListener());

                                String fqlResponse = Login.mFacebook.request(params);

                                   System.out.println(fqlResponse);

                                   JSONArray JOLikeresponse=new  JSONArray(fqlResponse);

                                  if(JOLikeresponse.length()!=0){
                                   JSONObject JOLikeObject = JOLikeresponse.getJSONObject(0);




                                if ( JOLikeObject.has("likes")) {
                                    String optlike,optlikesarray;
                                    JSONObject optLikes=JOLikeObject;
                                    JSONArray optLikesArray;
                                    try{

                                         optLikes = JOLikeObject.getJSONObject("likes");
                                         optlike="like";
                                    }
                                    catch(Exception e){

                                         optlike="unlike";
                                    }
//                                  
                                    if(optlike.equals("like")){

                                    if (optLikes.has("user_likes")) {
                                        String getUserLikes = optLikes.getString("user_likes");

                                        if (getUserLikes.equals("true")) {
                                            like_or_unlike.add("Unlike");
                                        } else if (getUserLikes.equals("false")) {
                                            like_or_unlike.add("Like");
                                        }
                                    }
                                    else {

                                           like_or_unlike.add("Like");
                                    }
                                } else {

                                       like_or_unlike.add("Like");
                                }

                                }
                                //if likes object is not there in like response
                                else {

                                       like_or_unlike.add("Like");
                                }
                                  }

                               //if the like response Array length is zero   
                                else {

                                       like_or_unlike.add("Like");
                                }//FQL query object

                            }

                    //If likes are not there        
                               else{
                                   likes.add("0");
                                   like_or_unlike.add("Like");
                               }


                            weburl.add(currentweb);

                           Log.e("comment", comment.get(j));



                           String getCreatedTime = jsonObject.getString("created_time");

                           SimpleDateFormat formatter = getDateFormat();
                           ParsePosition pos = new ParsePosition(0);
                           long then = formatter.parse(getCreatedTime, pos).getTime();
                           long now = new Date().getTime();

                           long seconds = (now - then)/1000;
                           long minutes = seconds/60;
                           long hours = minutes/60;
                           long days = hours/24;

                           String friendly = null;
                           long num = 0;
                           if (days > 0) {
                               num = days;
                               friendly = days + " day";
                           } else if (hours > 0) {
                               num = hours;
                               friendly = hours + " hour";
                           } else if (minutes > 0) {
                               num = minutes;
                               friendly = minutes + " minute";
                           } else if(seconds>0) {
                               num = seconds;
                               friendly = seconds + " second";
                           }
                           else{
                               friendly = "few seconds";
                           }
                           if (num > 1) {
                               friendly += "s";
                           }
                           String postTimeStamp = friendly.toLowerCase() + " ago";
                           Log.e("date",postTimeStamp );
                           date.add(postTimeStamp);

                           j++;
                    }
                    }







            }

            catch(Exception e)
            {
                e.printStackTrace();
            }


            return null;
        }

        protected void onPostExecute(Long result) { 

            try
            {   if(addFooter){
                 listView.addFooterView(footerView);
            }

                addFooter=false;
                System.out.println(scroll);
                if(scroll){

                 adapter=new MySimpleArrayAdapter(Wall.this,R.layout.wall,pic,name,msg,id,profimg,comment,objid,weburl,likes, like_or_unlike,date);
                listView.setAdapter(adapter);
                  listView.postDelayed(new Runnable() {

//                      @Override
                        public void run() {

                            listView.onRefreshComplete();
                        }
                    }, 2000);
                if(boolDialog){
                dialog.dismiss();
                }
                }

         else{
//           adapter=new MySimpleArrayAdapter(Wall.this,R.layout.wall,pic,name,msg,id,profimg,bitmap,comment,objid,weburl);
             adapter.notifyDataSetChanged();
              listView.postDelayed(new Runnable() {

//                  @Override
                    public void run() {

                        listView.onRefreshComplete();
                    }
                }, 2000);
            }
                if(boolDialog){
                dialog.dismiss();
                }
            }



            catch(Exception e)
            {
                e.printStackTrace();
                if(boolDialog){
                    dialog.dismiss();
                    }
            }
            boolDialog=false;       
    }

    }

    private static SimpleDateFormat getDateFormat() {
        return new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");
    }





public class MySimpleArrayAdapter extends ArrayAdapter<String> {
    private Activity context;
    ArrayList<String> namear,msgar,idar,profimage,postimage,commentsnum,objectid,urlString,likescount,like_or_ulike,datesofpost;
    TextView name1, message1,comments,commentsnumber, likesnumber,likes,dateofpost;
    ImageView profimg,postimg;
  ImageLoader imageLoader;
  Bitmap[] bitdata;
//  ViewHolder holder ;

//  View rowView;

  public MySimpleArrayAdapter(Activity c,int i,ArrayList<String> postpic, ArrayList<String> names,ArrayList<String> msg,ArrayList<String> id,ArrayList<String> proimg,ArrayList<String> comment,ArrayList<String> objid,ArrayList<String> web,ArrayList<String> likecount,ArrayList<String> unlike_or_like,ArrayList<String> dates) {

      super(c, i, names);
    Log.e("adapter","adap");
    this.context = c;
    this.namear = names;
    this.msgar = msg;
    this.idar = id;
    this.profimage=proimg;
    this.postimage=postpic;
//    this.bitdata=bit;
    this.commentsnum=comment;
    this.objectid=objid;
    this.urlString=web;
    this.likescount=likecount;
    this.like_or_ulike=unlike_or_like;
    this.datesofpost=dates;
   this.imageLoader = new ImageLoader(context);
  }
//  public View getView(final int position, View convertView, ViewGroup parent) {
////        View view;
//      TextView title1,id,name1,dispdate,loc;
//      ImageView image,delete,arrow;
//      View view = convertView;
////        view = inflater.inflate(R.layout.myfav_row, parent, false);
//      LayoutInflater inflator = context.getLayoutInflater();
//      view = inflator.inflate(R.layout.myfav_row, null);


  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {


            // code to load images

      View rowView=convertView ;
      LayoutInflater inflator =   getLayoutInflater();
      rowView = inflator.inflate(R.layout.walldata, null);

     name1 = (TextView) rowView.findViewById(R.id.name);
     message1 = (TextView) rowView.findViewById(R.id.msg);
     profimg= (ImageView) rowView.findViewById(R.id.profile_pic);
     postimg= (ImageView) rowView.findViewById(R.id.picpost);
     comments = (TextView) rowView.findViewById(R.id.comment);
     likes = (TextView) rowView.findViewById(R.id.like);
     commentsnumber = (TextView) rowView.findViewById(R.id.commentnumber);
     likesnumber = (TextView) rowView.findViewById(R.id.likesnumber);
     dateofpost = (TextView) rowView.findViewById(R.id.datepost);
//     rowView.setTag(holder);
     Log.e("user",idar.get(position));


    Log.e("adapter","adap");
     name1.setText(namear.get(position));
    if(msgar.get(position)!=""){
         message1.setText(msgar.get(position));

    }
    else
    {
         message1.setVisibility(View.GONE);
    }
    if(!isScrolling){
    if(!postimage.get(position).equals(""))
    {try{
         imageLoader.DisplayImage(postimage.get(position).replace(" ", "%20"), postimg) ;
//      Bitmap b= imageLoader.getBitmap(postimage.get(position));
//      postimg.setImageBitmap(b);

    }
    catch(Exception e){
        e.printStackTrace();
    }
    }
    else
    {
         postimg.setVisibility(View.GONE);
    }
    }
    try{
      imageLoader.DisplayImage(profimage.get(position).replace(" ", "%20"),  profimg) ;

    }
    catch(Exception e){
       e.printStackTrace();
    }
    dateofpost.setText(datesofpost.get(position));
     commentsnumber.setText(commentsnum.get(position));
     likesnumber.setText(likescount.get(position));
     likes.setText(like_or_ulike.get(position));

     likes.setOnClickListener(new OnClickListener() {


        public void onClick(View v) {
//          if(likes.getText().toString().equals("Like")){
               TextView t=(TextView)v;
              TextView likescountmodify = null;
              View parent = (View) t.getParent();
                if (parent != null) {
                    likescountmodify =  (TextView)parent.findViewById(R.id.likesnumber);

                }

                int i=  Integer.parseInt(likescount.get(position));

           if(like_or_ulike.get(position).equals("Like")){
                Log.e("inlike","like");
                like_or_ulike.set(position, "Unlike");
                t.setText(like_or_ulike.get(position));
                UrltoValue.getValuefromUrl("https://graph.facebook.com/"+objectid.get(position)+"/likes?access_token="+accesstoken+"&method="+"post");
//             listView.getAdapter().getItemAt(position);
               j=i+1;
               String s=Integer.toString(j);
            likescount.set(position, s);
            likescountmodify.setText(likescount.get(position));

            }
            else{
                Log.e("unlike","unlike");
                like_or_ulike.set(position, "Like");
                t.setText(like_or_ulike.get(position));
                UrltoValue.getValuefromUrl("https://graph.facebook.com/"+objectid.get(position)+"/likes?access_token="+accesstoken+"&method="+"DELETE");

                   j=i-1;
                 String s=Integer.toString(j);
                likescount.set(position, s);
                likescountmodify.setText(likescount.get(position));
            }
        }
    });


     comments.setOnClickListener(new OnClickListener() {


        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent myintent=new Intent(Wall.this,Comments.class);
            myintent.putExtra("name", namear.get(position));
            myintent.putExtra("profimg", profimage.get(position));
            myintent.putExtra("message", msgar.get(position));
            myintent.putExtra("postpic", postimage.get(position));
            myintent.putExtra("objectid", objectid.get(position));
            myintent.putExtra("commentsnum",commentsnum.get(position));
            myintent.putExtra("webservice", urlString.get(position));
            startActivity(myintent);
            finish();

        }
    });

    return rowView;

  }
}

请帮忙

提前致谢

4

3 回答 3

0

您应该尝试将Bitmap您显示的 s 的大小限制为接近图像视图的大小,这样当您实际想要显示较低分辨率的图像时,您就不会因为在内存中放置大图像而浪费内存。我通常使用此功能将图像文件的大小调整为更接近我想要的大小。

// decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
    try {
        // decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
                if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
                        break;
                width_tmp /= 2;
                height_tmp /= 2;
                scale *= 2;
        }

        // decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
        // TODO: Do something
    }
    return null;
}
于 2013-01-07T15:00:44.967 回答
0

// 使用 Option 类而不是 BitmapFactory.Options

Options opts = new Options(); 
                    opts.inJustDecodeBounds = true; 
                    BitmapFactory.decodeFile(path, opts); 
                    Log.e("optwidth",opts.outWidth+""); 
                    opts.inJustDecodeBounds = false; 
                    if(opts.outWidth>500){ 
                            opts.inSampleSize = 4; 
                            mBitmap = BitmapFactory.decodeFile(path, opts); 
                    } 
                    else mBitmap = BitmapFactory.decodeFile(path, opts);

注意:它会降低图像的质量,但据我所知,它肯定可以消除位图的内存不足错误。有关更多详细信息,请查看此链接:处理大型位图

于 2013-01-18T05:45:59.530 回答
0

他们可能正在缓存记录或帖子,并在该时刻显示(加载)用户可见的记录。有关如何执行此操作,请查看 developer.android.com 中的此示例应用程序 ->

有效地显示位图

于 2013-01-07T06:41:46.197 回答