2014-02-21 94 views
0

讓我解釋我的問題。在facebook上編碼facebook url圖像到BASE 64字符串

我想設置的Facebook個人資料圖像中應用的ImageView一些像這樣的事情..

// Getting Facebook URL image and converting same to bitmap 
Bitmap mIcon1; 


URL img_value = new URL("http://graph.facebook.com/"+ userProfileID +"/picture?type=square"); 
BitmapFactory.Options options = new BitmapFactory.Options(); 
mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream(), null, options); 

然後以後。

// I am setting bitmap to imageview .. some thing like .. 

if(mIcon1!=null) { 
    user_picture.setImageBitmap(mIcon1); 
} 

了這裏它是偉大的工作......

現在我需要的是Facebook個人資料圖片保存到我的數據庫位於服務器...

我一個說把一些東西一樣米的表演..

// Created a method for encoding .. 
public static String encodeTobase64(Bitmap image) 
    { 
     Bitmap immagex=image; 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
     byte[] b = baos.toByteArray(); 
     String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT); 

     Log.e("LOOK", imageEncoded); 
     return imageEncoded; 
    } 


// Now trying to use this method to get encoded BASE 64 image in string .. 

String final_image = encodeTobase64(mIcon1); 

現在,當我嘗試這個字符串發送到我的服務器,然後在服務器端,我recieving斷鏈......相反,我必須說,它不工作..

我需要執行兩個東西

  • 編碼Facebook的個人主頁圖像編碼的基礎64串,以便發送相同的服務器。
  • 獲取圖像的類型(即PNG/JPG ...)或壓縮並以一種特定格式發送該圖像。

期待着對此的任何建議。 謝謝!

回答

1

我得到了這個解決方案,並希望這將幫助一些一..

@基本概念: -我們通常使用GETPOST方法同時將數據發送到服務器。 1 GET: -在這個方法中,你只能發送數據的具體數額.. 2- POST: -在這個方法中,你可以發送大量的數據..

問題: -確切的問題是..我正在使用GET方法,同時發送數據到服務器。上面提到的概念對我來說是知道的。但是,有些我是如何犯這個錯誤的。

解決方案: -您只需使用POST方法而不是GET發送數據到服務器。在下述本

完整的解決方案: -

// Define your ASYNC TASK like .. 


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
       new ImageTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 
      } else{ 
       new ImageTask().execute(); 

      } 

// Now here comes your complete Async Task over here .. 

private class ImageTask extends AsyncTask<Void, Integer, Void> { 
      Bitmap mIcon1; 

      @Override 
      protected Void doInBackground(Void... params) { 
        URL img_value = null; 
        Log.d("taking", "2"); 
        try { 
         if(type_of_login.equalsIgnoreCase("facebook")){ 

          img_value = new URL("http://graph.facebook.com/"+ user_id +"/picture?type=square"); 
          System.out.println("Complete URl is:============================= " + img_value); 

         }else{ 

          img_value = new URL("https://plus.google.com/s2/photos/profile/"+ user_id +"?sz=50"); 
          System.out.println("Complete URl is:============================= " + img_value); 

         } 
         //img_value = new URL("http://graph.facebook.com/"+ userProfileID +"/picture?type=square"); 
         BitmapFactory.Options options = new BitmapFactory.Options(); 
         mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream(), null, options); 
         Log.d("taking", "3" + img_value); 
         Log.d("taking", "3" + mIcon1); 
         Log.d("taking", String.valueOf(mIcon1)); 

         ByteArrayOutputStream bao = new ByteArrayOutputStream(); 
         mIcon1.compress(Bitmap.CompressFormat.JPEG, 100, bao); 
         byte [] ba = bao.toByteArray(); 
         encoded_image =Base64.encodeToString(ba,Base64.DEFAULT); 

         System.out.println("Encoded image is : ===== " + encoded_image); 

        } catch (MalformedURLException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        }catch(Exception e){ 
         e.printStackTrace(); 
        } 



       return null; 
      } 

      @Override 
      protected void onPostExecute(Void result) { 



      if(mIcon1!=null) 
      { 
       user_pic.setImageBitmap(mIcon1); 
       get_string_image = encodeTobase64(mIcon1); 
       ByteArrayOutputStream bao = new ByteArrayOutputStream(); 

      } 
      } 
     } 

現在終於我們的夢想方法..

public static String encodeTobase64(Bitmap image) 
     { 
      Bitmap immagex=image; 
      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
      byte[] b = baos.toByteArray(); 
      String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT); 

      Log.e("LOOK", imageEncoded); 
      return imageEncoded; 
     } 

現在,當您需要通過API發送數據,則只需執行的HttpPost代替HttpGet

就是這樣..你是好去與這個..

相關問題