上传使用图形API与Android SDK中的图片至Facebook图形、上传、图片、Android

2023-09-05 10:22:20 作者:奇遇是恶作剧

我试图将照片上传至Facebook使用图形API以及不断收到内存不足的异常。 在code的上传是这样的:

I am trying to upload a photo to facebook using graph api and keep getting OutOfMemory exception. The code for the upload is this:

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)
    {

    }

}

例外的德codeFILE功能提高了,有时在Facebook的SDK本身时,它调用这一行:

The exception raises on the decodeFile function and sometimes in the facebook SDK itself when it calls this line:

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

请求函数里面。

inside the request function.

该应用程序是获取图片内容://乌里由ACTION_SEND意图。 getRealPathFromURI()被用于转换内容:// URI到真实SD卡路径

The app is getting the picture content:// Uri by the ACTION_SEND intent. getRealPathFromURI() is used to translate the content:// uri to a real sdcard path.

任何想法?

推荐答案

试试这个,它为我工作:

Try this, it works for me:

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有方法来调用它,只是从改变你的请求行:

This is using the Rest API, the facebook sdk has the method for calling it so just change your request line from:

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

facebook.mFacebook.request(params);   

和希望,这将解决这个问题。

and hopefully that will solve the problem.