1

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.

cesarcarlos
  • 1,271
  • 1
  • 13
  • 33
  • check this https://stackoverflow.com/a/52848396/13940268 – Mohanasundar May 25 '21 at 01:50
  • implement glide library `implementation 'com.github.bumptech.glide:glide:4.12.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'` – Mohanasundar May 25 '21 at 01:51
  • @Mohanasundar I tried with the example you sent, but String path = MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), resource, "", null); causes an error: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=25464, uid=10078 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission() – cesarcarlos May 31 '21 at 22:50
  • The problem is you don't have permission for WRITE_EXTERNAL_STORAGE see my answer to get runtime storage permission – Mohanasundar Jun 01 '21 at 04:59

1 Answers1

0

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;
        }
    }
Mohanasundar
  • 157
  • 1
  • 3
  • 13