在安卓中将facebook url图像编码为BASE 64字符串



让我解释一下我的问题。

我正在尝试在应用程序中设置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字符串,以便将其发送到服务器。
  • 获取图像类型(即 巴布亚新几内亚/JPG ... )或压缩并以一种特定格式发送该图像。

期待对此的任何建议。谢谢!

我得到了这个问题的解决方案,希望这会对某人有所帮助..

@基本概念:-- 我们在将数据发送到服务器时通常使用GETPOST方法。1- 获取:--在此方法中,您只能发送特定数量的数据。2- 帖子 :--在这种方法中,您可以发送大量数据..

问题是

:-确切的问题是..我在将数据发送到服务器时正在使用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

就是这样..你很高兴去这个..

最新更新