Android - Share content on social media

1k views Asked by At

I have an Android app screen which shows an image, a description and other additional information. I'd like to share this via social media (like Facebook, Twitter, etc). Basically I'd like to send the image, a small text and a URL. I have added a Share button and used this code:

            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            Uri uriToImage = Uri.parse("http://remote.path.to.image.jpg");
            shareIntent.putExtra(Intent.EXTRA_TEXT, "Learn more about this");
            shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
            shareIntent.setType("*/*");
            startActivity(Intent.createChooser(shareIntent, "Share"));

However this does not work. I have tested it with Facebook and while it shows the sheet and lets me choose Facebook, I'm presented with an empty message; the text and image are not attached. If I try with GMail it only attaches the text and mentions that the image could not be attached.

Also how can I include the URL that I want to share?

I'm working with Java.

1

There are 1 answers

0
Mohanasundar On

In Your manifest file add this line

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission
        android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
        tools:ignore="ScopedStorage" />
    <uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        tools:ignore="ScopedStorage" />

Add this line inside appliction tag

<application
        android:requestLegacyExternalStorage="true">

In your java file ask runtime permission for storage

private static final int STORAGE_PERMISSION = 101;
private void requestStoragePermissions() {
        if (!hasStoragePermission(getApplicationContext())) {
            ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION);
        } else {
            imagePick();//start you image activities
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NotNull int @NotNull [] grantResults) {

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == STORAGE_PERMISSION) {
            if (validateGrantedPermissions(grantResults)) {
                imagePick();//start you image activities
            }
            }
        }
    }

    public static boolean hasStoragePermission(Context context) {
        int finePermissionCheck = ContextCompat.checkSelfPermission(context,
                Manifest.permission.READ_EXTERNAL_STORAGE);
        int coursePermissionCheck = ContextCompat.checkSelfPermission(context,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);
        return !(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
                && (finePermissionCheck == PackageManager.PERMISSION_DENIED
                || coursePermissionCheck == PackageManager.PERMISSION_DENIED));
    }

    public static boolean validateGrantedPermissions(int[] grantResults) {
        boolean isGranted = true;
        if (grantResults != null && grantResults.length > 0) {
            for (int grantResult : grantResults) {
                if (grantResult == PackageManager.PERMISSION_DENIED) {
                    isGranted = false;
                    break;
                }
            }
            return isGranted;
        } else {
            return false;
        }
    }