Search code examples
androidfacebookfacebook-graph-apifacebook-sdk-3.0

How can I add name, caption etc in my uploaded image to facebook in my android application?


I am trying to develop an app that will uplaod an image to my wall. I did that, but I can't add name, caption etc which are in a bundle. Because newUploadPhotoRequest() method only takes 3 parameters(sessoin,bitmap,callback). Pls give me exact full code. Thank u

My code bellow : This method is for posting an image:

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();
        }

Solution

  • U can just try this, this will add a caption with your uploaded photo

    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();