Android: Share Image intent not working with Faceb

2020-05-24 16:37发布

Hi I have the following code to share an image:

// Share
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");

Uri uri = Uri.parse(getFilesDir() + File.separator + "myGoal.jpg");
        share.putExtra(Intent.EXTRA_STREAM, uri);

startActivity(Intent.createChooser(share, "Share Image"));

It works to share the image to Dropbox but if I pick the Facebook option, I get Facebook's status update dialog with no image attached and if I try to update my status with 'Test' it doesn't work. No errors. Just not working.

I know it's not the image because it uploads to my Dropbox properly and I can pull up the image and look at it.

Do I have to attach the image to the intent differently for it to work with Facebook?

Any ideas? I'm debugging on a physical device.

4条回答
劫难
2楼-- · 2020-05-24 16:59

What i have done to post image on facebook is instead of passing it directly i create a function and write it like this :

 private void ShareWall(String message) {
        Bundle parameters = new Bundle();
                // share msg
        parameters.putString("message", message);
                // shre image which you have define above
        parameters.putString("picture", postImage);

        try {
            facebook.request("me");
            String response = facebook.request("me/feed", parameters, "POST");
            Log.d("response: ", response);
            if (response == null || response.equals("")) {
                response.equals("");
                showToast("No Response.");

            } else {
                showToast("Message has been posted to your walll!.");
            }
            finish();
        } catch (Exception e) {
            showToast("Message failed to posdt on wall.");
            e.printStackTrace();
            finish();
        }

    }

Hope this help you.

查看更多
老娘就宠你
3楼-- · 2020-05-24 17:03

Here is a solution without using external file write:

Drawable mDrawable = myImageView1.getDrawable();
Bitmap mBitmap = ((BitmapDrawable)mDrawable).getBitmap();
String path = MediaStore.Images.Media.insertImage(getContentResolver(), mBitmap, "Image I want to share", null);
Uri uri = Uri.parse(path);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share Image"));

In this case, my image comes from an ImageView myImageView.

查看更多
Deceive 欺骗
4楼-- · 2020-05-24 17:09

You can still share images (but not text) from your app to Facebook even if you are not using the Facebook SDK.

Just make sure that you use Uri.fromFile instead of Uri.parse and it will work:

DO NOT USE: intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(pathToFile));

USE: intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(pathToFile)));

查看更多
混吃等死
5楼-- · 2020-05-24 17:25

So I figured out the problem.

I was saving the picture to internal storage with getFilesDir() which put the picture into my apps sandbox and made inaccessible to the other apps.

I replaced my code with the following:

String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + 
                "/MyApp/";

File dir = new File(file_path);
dir.mkdirs();
File file = new File(dir, "myPic.png");
FileOutputStream fOut = new FileOutputStream(file);

screenshot.compress(Bitmap.CompressFormat.PNG, 100, fOut);

fOut.flush();
fOut.close();

// Share
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");

share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
share.putExtra(Intent.EXTRA_TEXT, "My Image");

startActivity(Intent.createChooser(share, "Share Image"));

Works perfectly fine now.

查看更多
登录 后发表回答