2011-06-25 151 views
1

我正嘗試使用圖形api將照片上載到Facebook,並繼續獲取OutOfMemory異常。 對上傳的代碼是這樣的:使用Android API使用Graph API將圖片上傳到Facebook

private void PostPhoto(SessionHandler facebook, Uri photoUri) 
{ 
    Bundle params = new Bundle(); 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

    String realPath = getRealPathFromURI(photoUri); 
    Bitmap bitmap = BitmapFactory.decodeFile(realPath); 

    bitmap.compress(CompressFormat.JPEG, 100, bos); 

    params.putByteArray("picture", bos.toByteArray()); 


    try 
    { 
     facebook.mFacebook.request("me/photos", params, "POST");    

    } catch (FileNotFoundException fileNotFoundException) 
    { 

    } catch (MalformedURLException malformedURLException) 
    { 

    } catch (IOException ioException) 
    { 

    } 

} 

異常的decodeFile功能,有時在Facebook的SDK本身時,它調用此行引起了:

return Util.openUrl(url, httpMethod, parameters); 

請求函數內。

該應用獲取ACTION_SEND意圖的圖片內容:// Uri。 getRealPathFromURI()用於將content:// uri轉換爲真實的SD卡路徑。

任何想法?

+1

你能顯示錯誤的堆棧跟蹤嗎? – Kenny

回答

1

試試這個,它爲我工作:

byte[] data = null; 
try { 
    ContentResolver cr = mainActivity.getContentResolver(); 
    InputStream fis = cr.openInputStream(photoUri); 
    Bitmap bi = BitmapFactory.decodeStream(fis); 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    bi.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
    data = baos.toByteArray();    
} catch (FileNotFoundException e) { 
    e.printStackTrace(); 
}  
Bundle params = new Bundle(); 
params.putString("method", "photos.upload");   
params.putByteArray("picture", data); 

這是使用REST API,Facebook的SDK爲調用它,只是從改變你的請求行的方法:

facebook.mFacebook.request("me/photos", params, "POST"); 

到:

facebook.mFacebook.request(params); 

並希望這將解決問題。

+0

看起來像這樣的代碼沒有得到內存不足的異常。該代碼確實到達RequestListener的onComplete,但圖片不顯示在Facebook中。試圖將請求中的null更改爲「我/照片」,並且在配置文件或相冊中仍然沒有任何內容。 – Idan

+1

@Idan - 你可以發佈你從facebook獲得的回覆 – Kenny

+0

回覆:不支持的方法,photos.upload – Idan

相關問題