Social Icons

2010年11月20日 星期六

將View的內容截取出來

因為程式中會需要截取MapView的內容,並上傳到facebook,所以將截取View內容的方式紀錄下來。

Bitmap bitmap = Bitmap.createBitmap(mapView.getWidth(), mapView.getHeight(), Bitmap.Config.ARGB_8888); 
Canvas canvas = new Canvas(bitmap); 
mapView.draw(canvas);

重點其實只有:
(一)以View的size來建立一個Bitmap。
(二)以這個bitmap來建立canvas。
(三)將mapView的內容畫在canvas上。

然後,就可以將這個Bitmap轉化成PNG的格式,可以儲存下來,或者上傳到網路上,夠簡單了吧。

2010年11月3日 星期三

使用facebook api

在我的程式中有使用facebook api,雖然不是很難,不過也把我使用的方法紀錄和分享下來。

  • login和授權:
facebook api已經將登入和授權整個UI都包好了,使用時也是非常方便。
mFacebook = new Facebook;
...
        if (mFacebook.isSessionValid() == false) {
         mFacebook.authorize(ctx, APP_ID, FB_PERMISSIONS, new DialogListener() {
    @Override
    public void onCancel() {
    }
    @Override
    public void onComplete(Bundle values) {
     post();
    }
    @Override
    public void onError(DialogError e) {
     sendNotify(false);
    }
    @Override
    public void onFacebookError(FacebookError e) {
     sendNotify(false);
    }
         });
        } else {
         post();
        }
當mFacebook.isSessionValid()為true時代表授權還沒有過期,是不需要在要求使用者登錄的,可以繼續我們上傳的程序。

  • Post message:
下面是post message的範例。
  final String GRAPH_PATH = "me/feed";
  final String POST_MESSAGE = "message";
  Bundle params = new Bundle();
  String msgVal = null;
  if (this.msg != null) {
   msgVal = this.title+"\n"+this.msg;
  } else {
   msgVal = this.title;
  }
  params.putString(POST_MESSAGE, msgVal);
  
  try {
   String resp = mFacebook.request(GRAPH_PATH, params, "POST");
   JSONObject json = Util.parseJson(resp);
   postId = json.getString("id");

  • Post photo:
重點在於如何將photo變成binary傳送到facebook;另外縮圖也很重要,facebook上傳照片的時間很長,如果能先縮圖可以減少上傳時間。
  final String GRAPH_PATH = "me/photos";
  final String POST_MESSAGE = "message";
  Bundle params = new Bundle();
  
  String msgVal = null;
  if (this.msg != null) {
   msgVal = this.title+"\n"+this.msg;
  } else {
   msgVal = this.title;
  }
  params.putString(POST_MESSAGE, msgVal);
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  travelMap.compress(Bitmap.CompressFormat.PNG, 100, out);
  params.putByteArray("picture", out.toByteArray());   
        
  try {
   String resp = mFacebook.request(GRAPH_PATH, params, "POST");
   JSONObject json = Util.parseJson(resp);
   postId = json.getString("id");

  • Thread:
因為facebook api會花一些時間等待,所以最好把整個程序放在背景執行會比較好。
    private void post() {
     Thread thread = new Thread()
     {
         @Override
         public void run() {
          boolean bRet = false;
      switch(hi.getType()) {
      case HistoryDB.HISTORY_PHOTO_TYPE:
       bRet = postPhoto();
       break;
           
      case HistoryDB.HISTORY_POI_TYPE:
       bRet = postFeed();
       break;
           
      default:
       break;
      }
          
          Message msg = new Message();
          if (bRet == true)
           msg.arg1 = 1;
          else
           msg.arg1 = 0;
          handler.sendMessage(msg);
         }
     };
     thread.start();
    }
    
    Handler handler=new Handler() {
        @Override
        public void handleMessage(Message msg) {
         if (msg.arg1 == 1) {
          sendNotify(true);
         } else {
          sendNotify(false);
         }
        }
    };