0

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

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

公共无效图像加载(){

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

1 回答 1

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();
于 2013-11-24T10:26:56.233 回答