-3

我想将图片从 sdcard 上传到 facebook。我为此尝试了很多,但我无法获得成功。我应该怎么办?

我的代码是:

<tr>
Bundle params = new Bundle();
params.putString("message", "Uploaded on " + now());
params.putByteArray("picture", bytes); //bytes contains photo bytes, no problem here
asyncRunner.request("me/photos", params, "POST", new PostPhotoRequestListener(), null);
</tr>
4

2 回答 2

2

从此LINK复制了解决方案。您应该使用 Facebook API http://developers.facebook.com/docs/guides/mobile/#android。然后使用以下代码:

 byte[] data = null;
 try {
     FileInputStream fis = new FileInputStream(PATH_TO_FILE);
     Bitmap bi = BitmapFactory.decodeStream(fis);
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
     data = baos.toByteArray();  
  } catch (FileNotFoundException e) { 
     e.printStackTrace();
     Log.d("onCreate", "debug error  e = " + e.toString());
  }     

     Bundle params = new Bundle(); 
     params.putString("method", "photos.upload");  
     params.putByteArray("picture", data);

     Facebook facebook = new Facebook(FACEBOOK_APP_ID);
     AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
     mAsyncRunner.request(null, params, "POST", new RequestListener() {

        public void onMalformedURLException(MalformedURLException e, Object state) {
            Log.d("request RequestListener", "debug onMalformedURLException");
        }

        public void onIOException(IOException e, Object state) {
            Log.d("request RequestListener", "debug onIOException");
        }

        public void onFileNotFoundException(FileNotFoundException e, Object state) {
            Log.d("request RequestListener", "debug onFileNotFoundException");
        }

        public void onFacebookError(FacebookError e, Object state) {
            Log.d("request RequestListener", "debug onFacebookError");
        }

        public void onComplete(String response, Object state) {
             Log.d("request RequestListener", "debug onComplete");
        }
     }, null);

确保您的应用程序具有互联网和 sdcard 读取权限

于 2012-05-19T15:57:25.543 回答
0

我在这里回答了一个类似的问题:https ://stackoverflow.com/a/10561377/450534

protected void uploadPhoto() {
    if (!Utility.mFacebook.isSessionValid()) {
        Util.showAlert(this, "Warning", "You must first log in.");
    } else {
        if (targetUri != null) {
            Bundle params = new Bundle();
            try {
                params.putByteArray("photo", Utility.scaleImage(getApplicationContext(), targetUri));
            } catch (Exception e) {
                e.printStackTrace();
            }
            params.putString("caption", txtPhotoCaption.getText().toString());
            Utility.mAsyncRunner.request( "replace this with your ID"+ "/photos&access_token=" + Utility.mFacebook.getAccessToken(), 
                    params, "POST", new PhotoUploadListener(), null);
            txtPhotoCaption.setText("");
            Toast.makeText(getApplicationContext(), "Photo uploaded successfully", Toast.LENGTH_SHORT).show();
            this.finish();
        }
    }
}

请注意,我使用的方法与示例应用程序中使用的方法相同。没有涉及缩放或不缩放图像的利弊。“标题”属性是从 EditText 中提取的。实际图像位于“ targetUri ”字段中,该字段被声明为全局变量。它使用设备上的默认图库来获取图像。为了完整起见,这是从默认图库应用程序中获取图像的代码。(这会提示用户选择应用程序)

btnAddPhoto.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, 0);
            }
        });

它在onActivityResult()这里处理:

注意我还将 ImageView 中的选定图像设置为登录用户的预览

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        Bitmap bitmap = null;
        if (resultCode == RESULT_OK)    {
            targetUri = data.getData();
            Log.e("IMG URI", targetUri.toString());
            try {
                bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
                try {
                    Utility.scaleImage(getApplicationContext(), targetUri);
                    imgDisplaySelectedImage.setImageBitmap(bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

提到的链接中的答案是将照片上传到朋友墙。但是,使用我的 ID 而不是朋友 ID 的相同代码基本上完成了您想要的相同任务。希望这对您也有帮助。

编辑:在第一段代码中,在这里用您的 ID 替换它,您也可以调用"me/photos&access_token=". 其余的保持不变。

于 2012-05-19T12:27:17.530 回答