【问题标题】:How to publish facebook photo with multipart/form-data如何使用 multipart/form-data 发布 facebook 照片
【发布时间】:2014-01-10 21:32:45
【问题描述】:

使用 Android 应用程序,我正在尝试使用 graph-api 参考将照片发布到 facebook 墙上: https://developers.facebook.com/docs/graph-api/reference/user/photos/

Bundle params = new Bundle();
params.putString("source", "{image-data}");
/* make the API call */
new Request(
    session,
    "/me/photos",
    params,
    HttpMethod.POST,
    new Request.Callback() {
        public void onCompleted(Response response) {
            /* handle the result */
        }
    }
).executeAsync();

我可以轻松地将图片传递图像 url 上传到“源”,但我想从我的 FileInputStream 发送 multipart/form-data 而不上传到服务器。

谁能解释我如何从“照片,编码为表单数据”中生成一个字符串? 这个方法我试过了,好像不行:

static final String GetMultipartFormData(InputStream fileInputStream)
{
    StringBuilder sb = new StringBuilder();

    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";

    try
    {                       
        sb.append(twoHyphens + boundary + lineEnd);
        sb.append("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"fileName.jpg\"" + lineEnd);
        sb.append(lineEnd);

        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
        BufferedReader br = new BufferedReader(inputStreamReader);
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }                           

        sb.append(lineEnd);
        sb.append(twoHyphens + boundary + twoHyphens + lineEnd);

        fileInputStream.close();            

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

    return sb.toString();
}

非常感谢。

【问题讨论】:

    标签: android facebook facebook-graph-api publish multipartform-data


    【解决方案1】:

    它以这种方式对图像进行编码:

    Bitmap photo; //This is the bitmap of your photo
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    

    并将图像传递给 Bundle:

    params.putByteArray("source",byteArray);
    

    【讨论】:

      猜你喜欢
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-07
      • 1970-01-01
      • 2017-10-13
      • 1970-01-01
      • 2018-12-26
      相关资源
      最近更新 更多