上传使用图形API与Android SDK中的图片至Facebook [英] Uploading pictures to Facebook using Graph API with Android SDK

查看:128
本文介绍了上传使用图形API与Android SDK中的图片至Facebook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将照片上传至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.

这篇关于上传使用图形API与Android SDK中的图片至Facebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆