2013-05-08 59 views
1

我已经使用Facebook SDK完成了文本共享。我被困在从SD卡上传图片。在将图像解析为FB SDK之前,首先将图像转换为位图,然后我应该与Facebook共享。但是这个逻辑不起作用。它永远不会从SD卡上传图像。任何人都可以指导我如何从SD卡上传图片?提前致谢。如何从SD卡上传图片到facebook

Bitmap bi = BitmapFactory.decodeStream(fis); 
Bitmap bi =BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() +"/Android/test.jpg", options); 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
bi.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
+0

请参阅http://stackoverflow.com/questions/8710515/reading-an-image-file-into-bitmap-from-sdcard – ozbek 2013-05-08 06:42:43

+0

可以请您分享fb共享的其余代码。我需要做同样的工作,仍然没有得到解决方案 – 2014-01-22 02:03:04

回答

0

尝试。

private Facebook facebook = new Facebook("319*********897"); 
byte[] byteArray; 

从SD卡获取图像并转换为byteArray。

然后使用此功能在脸书上分享图片。

public void shareOnFacebook() { 
    // mDuckHuntView.pause(); 

    facebook.authorize(this, new String[] { "publish_stream", 
      "user_photos", "publish_checkins", "publish_actions" }, 
      new DialogListener() { 
       public void onComplete(Bundle values) { 
        try { 

         Bundle bundle = new Bundle(); 
         bundle.putString(
           "name", 
           "text here"); 
         bundle.putByteArray("file", byteArray); 
         bundle.putByteArray(Facebook.TOKEN, values 
           .getString(Facebook.TOKEN).getBytes()); 
         String response = facebook.request("me/photos", 
           bundle, "POST"); 

         Log.d("UPDATE RESPONSE", "" + response); 
         JSONObject json = Util.parseJson(response); 
         if (!json.isNull("id")) { 
          // err("Facebook Image link submitted."); 

         } else { 
          // err("Facebook Error: " + response); 
         } 

        } catch (Exception e) { 


        } catch (FacebookError e) { 

        } 
       } 

       public void onFacebookError(FacebookError e) { 

       } 

       public void onError(DialogError e) { 

       } 

       public void onCancel() { 

       } 

      }); 
} 

为此,您需要使用facebook sdk软件包。

+0

谢谢它工作正常。现在我可以分享来自SD卡的图像。 – RAAAAM 2013-05-22 07:20:30

+0

对话框打开,登录后图像不张贴。当我调试它。光标不会完成。你可以共享完整的代码登录和分享fb图像。或任何提示来解决我的问题。 – 2014-01-21 15:45:17

1

尝试下面的代码,并检查您的位图切忌空..

Bundle bundle = new Bundle(); 
bundle.putString("message", "post image.."); 

byte[] data = null; 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
<<your bitmap>>.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
data = baos.toByteArray(); 

bundle.putString(Facebook.TOKEN, facebook.getAccessToken()); 
bundle.putString("method", "photos.upload"); 
bundle.putByteArray("picture", data); 
bundle.putString("message", "post image.."); 

mAsyncRunner.request(null,bundle,"POST",new RequestListener() { 
    @Override 
    public void onMalformedURLException(MalformedURLException e, Object state) { 
     Log.d("request RequestListener", "debug onMalformedURLException"); 
    } 
    @Override 
    public void onIOException(IOException e, Object state) { 
    Log.d("request RequestListener", "debug onIOException"); 
    } 
    @Override 
    public void onFileNotFoundException(FileNotFoundException e, Object state) { 
    Log.d("request RequestListener", "debug onFileNotFoundException"); 
    } 
    @Override 
    public void onFacebookError(FacebookError e, Object state) { 
    Log.d("request RequestListener", "debug onFacebookError"); 
    } 
    @Override 
    public void onComplete(String response, Object state) { 
    Log.d("request RequestListener", "debug onComplete"); 
    } 
}, null);