如何在我的Android应用程序中将我上传的图像中的名称,标题等添加到Facebook?

时间:2013-09-22 08:53:04

标签: android facebook facebook-graph-api facebook-sdk-3.0

我正在尝试开发一个应用程序,将图像上传到我的墙上。我这样做了,但是我不能添加捆绑中的名称,标题等。因为newUploadPhotoRequest()方法只需要3个参数(sessoin,bitmap,callback)。请给我准确的完整代码。谢谢你

我的代码如下: 此方法用于发布图像:

public void image_load(){

Session session = Session.getActiveSession();

        if (session.isOpened())
        {       Toast.makeText(MainActivity.this, "IN image load IF", Toast.LENGTH_SHORT).show();
            Bundle postParams = new Bundle();

            postParams.putString("name", "Name here.");
            postParams.putString("caption", "Caption here.");
            postParams.putString("description", "Description here.");
            postParams.putString("link", "https://developers.facebook.com/android");

            byte[] data = null;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            Bitmap bi = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
            //Bitmap bi = BitmapFactory.decodeFile("/sdcard/viewitems.png");
            bi.compress(Bitmap.CompressFormat.PNG, 100, baos);
            data = baos.toByteArray();

            //postParams.putString("method", "photos.upload");
            postParams.putByteArray("picture", data);

            Request.Callback callback = new Request.Callback() 
            {
                public void onCompleted(Response response) 
                {
                    FacebookRequestError error = response.getError();

                    if (error != null) 
                        Toast.makeText(MainActivity.this , error.getErrorMessage(), Toast.LENGTH_SHORT).show();

                    else 
                        Toast.makeText(MainActivity.this, "Posted successful on your wall", Toast.LENGTH_SHORT).show();
                }
            };

            //Request request = new Request(session, "feed", postParams, HttpMethod.POST, callback);
            //RequestAsyncTask task = new RequestAsyncTask(request);
            //task.execute();
            Request request = Request.newUploadPhotoRequest(session, bi, callback);
            request.executeAsync();
        }
        else{
            Toast.makeText(MainActivity.this, "login first", Toast.LENGTH_SHORT).show();
        }

1 个答案:

答案 0 :(得分:1)

你可以尝试这个,这会为你上传的照片增加一个标题

Bitmap image = BitmapFactory.decodeResource(getResources(),
                R.drawable.ic_launcher);
        Request.Callback callback = new Request.Callback() {

            @Override
            public void onCompleted(Response response) {
                if (response.getError() != null) { 
                    Log.d("Upload","photo upload problem. Error="
                                    + response.getError());
                } 

                Object graphResponse = response.getGraphObject().getProperty(
                        "id");
                if (graphResponse == null || !(graphResponse instanceof String)
                        || TextUtils.isEmpty((String) graphResponse)) { 
                    Log.d("Upload", "failed photo upload/no response");
                } else {

                    Log.i("Upload", "Successfull");
                }

            }
        };

        Request request = Request.newUploadPhotoRequest(
                Session.getActiveSession(), image, callback);
        Bundle params = request.getParameters();
        params.putString("name", "The Caption");
        request.setParameters(params);
        request.executeAsync();