0

我正在尝试将来自 url 的照片发布到 facebook。因此,我有一种方法可以将位于提供的 url 中的图像转换为位图,之后我想将图像发布到用户的 facebook 照片。这是我的代码:

将位于 URL 中的图像转换为位图图像的方法:

public Bitmap downloadImage(String url) {
   Bitmap bm = null;
   try {
       URL aURL = new URL(url);
       URLConnection conn = aURL.openConnection();
       conn.connect();
       InputStream is = conn.getInputStream();
       BufferedInputStream bis = new BufferedInputStream(is);
       bm = BitmapFactory.decodeStream(bis);
       bis.close();
       is.close();
   } catch (IOException e) {
       Log.e("Hub","Error getting the image from server : " + e.getMessage().toString());
   } 
   return bm;
}

这是我尝试在 facebook 中发布图像的地方:

private void postToFacebook(byte[] data) {
   Session session = Session.getActiveSession();

   if (session != null) {

       Bundle postParams = new Bundle();
       postParams.putString("caption", "Teste caption");
       postParams.putByteArray("picture", data);

       Request.Callback callback = new Request.Callback() {
           public void onCompleted(Response response) {
               JSONObject graphResponse = response.getGraphObject().getInnerJSONObject();
               String postId = null;
               try {
                   postId = graphResponse.getString("id");
               } catch (JSONException e) {
                   Log.i("", "JSON error "+ e.getMessage());
               }
               FacebookRequestError error = response.getError();
               if (error != null) {
                   Toast.makeText(getContext(), error.getErrorMessage(), Toast.LENGTH_SHORT).show();
                   } else {
                       Toast.makeText(getContext(), postId, Toast.LENGTH_LONG).show();
               }
           }
       };

           Request request = new Request(session, "me/photos", postParams, HttpMethod.POST, callback);

       RequestAsyncTask task = new RequestAsyncTask(request);
       task.execute();
   } else {
    Toast.makeText(getContext(), "Not logged", Toast.LENGTH_SHORT).show();
   }

但是在这一行:response.getGraphObject(),我得到了空指针异常。我试图在后台线程中执行,如下所示:

class ConvertImageTask extends AsyncTask<URL, Integer, byte[]> {
AsyncTask<Params,Progress,Result>
@Override
protected void onPreExecute() {
    super.onPreExecute(); // do nothing here
}

@Override
protected byte[] doInBackground(URL... params) {
byte[] data;
    Bitmap bi = downloadImage(getPhotoUrl());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bi.compress(Bitmap.CompressFormat.PNG, 100, baos);
    data = baos.toByteArray();
return data;
}
@Override
protected void onPostExecute(byte[] result) {
    postToFacebook(result);
}

但错误仍在继续。

我怎样才能解决这个问题?谢谢!

4

1 回答 1

1

我浏览过的页面显示“图片”参数是一个 URL,而不是您可以从计算机上传的数据。

要上传我正在使用的照片:

Request request = Request.newUploadPhotoRequest(Session.getActiveSession(),
                  BitmapFactory.decodeStream(this.getAssets().open("YourFile.png")), 
              new Request.Callback(){ 
            @Override
            public void onCompleted(Response response) {
                FacebookRequestError error = response.getError();
                if(error != null){
                    Log.i(this.getClass().getName(),"Error publishing.");
                }else{
                    JSONObject graphResponse = response.getGraphObject().getInnerJSONObject();
                    String postId = null;
                    try {
                        postId = graphResponse.getString("id");
                    } catch (JSONException e) {
                        Log.i("Facebook error", "JSON error " + e.getMessage());
                    }

                    //Logged in Succesfully!        
                }
            }
});
Bundle params = request.getParameters();
params.putString("name", "The title for the photo.");
request.setParameters(params);

request.executeAsync();

我正在传递我的资产文件夹中的 png,但您可以根据需要调整该部分。查看您的代码,它应该是直截了当的。

于 2014-04-10T00:57:59.027 回答