Android只使用intent向facebook分享一张图片



很多人都知道,使用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);

这是我所尝试的,使用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();
        }
     }
}

相关内容

最新更新