Android只使用意图与Facebook共享图像

时间:2014-06-18 10:09:43

标签: android facebook image android-intent

众所周知,使用Android共享意图对话框将图像+文本分享到Facebook似乎有问题。然而,对于我的生活,我无法让简单的纯图像共享工作。

在尝试了所有内容后,我看到Android相机滚动共享似乎有效。因此,我尝试过的最新事情就是:

1) contentUri_internal_fileprovider是保存在内部存储中的jpeg图像。 Facebook共享对话框变为灰色并返回到我自己的动作中)

    shareIntent = new Intent(Intent.ACTION_SEND);            
    shareIntent.setType("image/jpeg");                        
    shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri_internal_fileprovider);
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);                                                         
    startActivity(shareIntent.createChooser(shareIntent, "..."));

2 contentUri_camera_fromfile是保存在相机存储中的jpeg图像。 Facebook共享对话框打开但不显示任何图像)

    shareIntent = new Intent(Intent.ACTION_SEND);            
    shareIntent.setType("image/jpeg");                        
    shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri_camera_fromfile);
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);                                                         
    startActivity(shareIntent.createChooser(shareIntent, "..."));

有关如何恢复相机胶卷图像路径的详细信息:

      contentPathReal_camera_fromfile = MediaStore.Images.Media.insertImage(getContentResolver(), myBitmap, myTitle, myDesc);
      file_camera_fromfile = new File(contentPathReal_camera_fromfile);
      contentUri_camera_fromfile = Uri.fromFile(file_camera_fromfile);

1 个答案:

答案 0 :(得分:0)

这是我尝试过的,ByteArray,它对我有用。我尝试使用putByteArray("photo", bytes.toByteArray());

private void publishStory() {
    Log.i("", "Got in publish story");



    final ProgressDialog progressDialog = new ProgressDialog(FacebookShareActivity.this);
    progressDialog.setMessage("Loading...");
    progressDialog.setCanceledOnTouchOutside(false);
    final Session session = Session.getActiveSession();

    if (session != null){
        progressDialog.show();
        // Check for publish permissions    
        List<String> permissions = session.getPermissions();
        if (!isSubsetOf(PERMISSIONS, permissions)) {
             progressDialog.dismiss();
            pendingPublishReauthorization = true;
            Session.NewPermissionsRequest newPermissionsRequest = new Session
                    .NewPermissionsRequest(this, PERMISSIONS);
        session.requestNewPublishPermissions(newPermissionsRequest);
            return;
        }

        final Bundle postParams = new Bundle();
        byte[] data = null;
        Log.i("","Share pic :: "+sharePicture);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeFile(sharePicture, options);
        if(bitmap != null) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        postParams.putByteArray("photo", bytes.toByteArray());
        postParams.putString("message", "I am supporting the "+candidateParty+
            " candidate "+candidate_name+" , competing for " +
            constituency+ " via BallotPetty®. - https://play.google.com/store/apps/details?id=com.cabot_technologies.elike");

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


                if (error != null) {
                    // Functionality : Do u Really wish to log out??
                    AlertDialog.Builder alertDialog = new AlertDialog.Builder(FacebookShareActivity.this);
                    alertDialog.setTitle("Alert");
                    alertDialog.setMessage(error.getErrorMessage());
                    alertDialog.setCancelable(false);

                    // Setting Positive Button
                    alertDialog.setPositiveButton(("Ok"),
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
//                                      Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
//                                      startActivityForResult(intent, requestCode);
                                    dialog.dismiss();
                                    finish();
                                }
                            });

                    // Showing Alert Message
                    alertDialog.show();
            }
                else{

                    Toast.makeText(FacebookShareActivity.this, R.string.success_facebook, Toast.LENGTH_SHORT).show();
                    finish();

                }
            }
        };

        /*Request request = new Request(session, "343394662461234/feed", postParams, 
                              HttpMethod.POST, callback);*/


          Request request = new Request(session, "me/photos", postParams, 
                     HttpMethod.POST, callback);
          RequestAsyncTask task = new RequestAsyncTask(request);

          task.execute();

        }
        else
        {
            // Functionality : Do u Really wish to log out??
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(FacebookShareActivity.this);
            alertDialog.setTitle("Alert");
            alertDialog.setMessage("Null Image");
            alertDialog.setCancelable(false);

            // Setting Positive Button
            alertDialog.setPositiveButton(("Ok"),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
 //                             Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
 //                             startActivityForResult(intent, requestCode);
                            dialog.dismiss();
                            finish();
                        }
                    });

            // Showing Alert Message
            alertDialog.show();
        }
     }
}