【问题标题】:Encode facebook url image to BASE 64 string in android在android中将facebook url图像编码为BASE 64字符串
【发布时间】:2014-02-21 09:51:17
【问题描述】:

让我解释一下我的问题。

我正在尝试在应用 Imageview 中设置 Facebook 个人资料图片,类似这样的东西 ..

// 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);

现在,当我尝试将此字符串发送到我的服务器时,然后在服务器端我收到断开的链接......相反,我必须说它不起作用..

我需要做两件事

  • 将 Facebook 个人资料图像编码为编码的 BASE 64 字符串,以便将其发送到服务器。
  • 获取图像类型(即 PNG/JPG ...)或以一种特定格式压缩和发送该图像。

期待对此提出任何建议。 谢谢!

【问题讨论】:

    标签: android facebook base64 encode


    【解决方案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

    就是这样..你很好..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-24
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 2018-03-19
      • 1970-01-01
      相关资源
      最近更新 更多