Shubham
Shubham

Reputation: 1235

sharing failed, please try again (only in whatsapp)

when i am sharing content to whatsapp,it returns back to share page with toast notification "Sharing failed, Please try again"

my code

if (url.startsWith("share://")) {
            Uri requestUrl = Uri.parse(url);
            String pContent = requestUrl.toString().split("share://")[1];
            Toast toast=Toast.makeText(getApplicationContext(),pContent, Toast.LENGTH_LONG);
            toast.setMargin(50,50);
            toast.show();
            StringBuilder sb = new StringBuilder();
            String [] parts = pContent.split("<br />");
            for (int i = 0; i < parts.length; i++) {
                String part = parts[i];
                sb.append(part);
                sb.append('\n');
            }
            Intent share = new Intent();
            share.setAction(Intent.ACTION_SEND);
            share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            share.putExtra(android.content.Intent.EXTRA_TEXT, (Serializable) sb);
            share.setType("*/*");
            try {
            startActivity(Intent.createChooser(share, "Share On"));
            } catch (android.content.ActivityNotFoundException ex) {
                toast = Toast.makeText(getApplicationContext(), "whatsapp not installed", Toast.LENGTH_LONG);
                toast.setMargin(50,50);
                toast.show();
            }
            return true;

and my logcat

08-01 14:37:42.081 1472-1514/com.example.myactivity I/MaliEGL: [Mali]window_type=1, is_framebuffer=0, errnum = 0
08-01 14:37:42.081 1472-1514/com.example.myactivity I/MaliEGL: [Mali]surface->num_buffers=4, surface->num_frames=3, win_min_undequeued=1
08-01 14:37:42.081 1472-1514/com.example.myactivity I/MaliEGL: [Mali]max_allowed_dequeued_buffers=3

Upvotes: 7

Views: 18962

Answers (5)

Begmyrat Mammedov
Begmyrat Mammedov

Reputation: 376

I was suffering with this issue, and found correct answer that worked for me. You just need provider.

"You can't get uri by using Uri uri5 = Uri.parse(myuri); on android 8.0 and above , you have to use file provider in your application to get uri for a file, also you need to add Intent.FLAG_GRANT_READ_URI_PERMISSION to your share intent, then only the third application can get access to the file

for sample res->xml->provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

create dummy class which extends FileProcider in our case GenericFileProvider.java

import android.support.v4.content.FileProvider;
public class GenericFileProvider extends FileProvider {
}

in your manifest.xml add these lines inside application tag

<provider
            android:name=".GenericFileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
</provider>

now you can get Uri for File using the bellow code in your application

Uri screenshotUri = FileProvider.getUriForFile(SelectedImageActivity1.this, getApplicationContext().getPackageName() + ".provider", file);

"

Reference: solution

Upvotes: 0

Sirop4ik
Sirop4ik

Reputation: 5263

There is my approach.

private fun openShareDialog(iC: Context, //
                            iPath: String)
{
    MediaScannerConnection.scanFile( //
            iC.applicationContext, //
            arrayOf(iPath), null //
    ) { _, iUri ->
        var shareIntent = Intent(Intent.ACTION_SEND).apply {
            putExtra(Intent.EXTRA_STREAM, iUri)
            type = "image/*"
            addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            putExtra(Intent.EXTRA_TEXT, iC.getString(R.string.screenshot_sharing_text))
        }

        shareIntent = Intent.createChooser(shareIntent, iC.resources.getText(R.string.send_to)) //
                .apply {
                    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                }

        iC.startActivity(shareIntent)
    }
}

This is applicable for image sharing as well as for video sharing(just don't forget to change type to (for example) video/*.

If you need to use sharing as Pending Intent (for example as action button in Notification), so you can use this class

It is applicable

class ShareScreenshotService : IntentService(SHARE_VIDEO_RECORD_SERVICE)
{
    override fun onCreate()
    {
        super.onCreate()
        startService(Intent(this, ShareScreenshotService::class.java))
    }

    override fun onHandleIntent(intent: Intent?)
    {
        if (intent != //
            null && intent.hasExtra(EXTRA_SHARE_VIDEO_RECORD_PATH))
        {
            val path = intent.getStringExtra(EXTRA_SHARE_VIDEO_RECORD_PATH)
            Logger.log(Log.ERROR, TAG, path!!)

            openShareDialog(this, path)

            PushNotificationManager.getInstance(this).getVideoRecordingNotificator(this).closeNotification()
        }
    }

    private fun openShareDialog(iC: Context, //
                                iPath: String)
    {
        MediaScannerConnection.scanFile( //
                iC.applicationContext, //
                arrayOf(iPath), null //
        ) { _, iUri ->
            var shareIntent = Intent(Intent.ACTION_SEND).apply {
                putExtra(Intent.EXTRA_STREAM, iUri)
                type = "image/*"
                addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                putExtra(Intent.EXTRA_TEXT, iC.getString(R.string._screenshot_sharing_text))
            }

            shareIntent = Intent.createChooser(shareIntent, iC.resources.getText(R.string._send_to)) //
                    .apply {
                        addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    }

            iC.startActivity(shareIntent)
        }
    }

    companion object
    {
        private val TAG = ShareScreenshotService::class.java.simpleName
        private const val SHARE_VIDEO_RECORD_SERVICE_REQUEST_CODE = 3

        const val EXTRA_SHARE_VIDEO_RECORD_PATH = "_extra_share_video_record_path"
        const val SHARE_VIDEO_RECORD_SERVICE = "_share_video_record_service"

        @JvmStatic
        fun pendingIntent(context: Context, //
                          iPath: String): PendingIntent
        {
            val intent = Intent(context, ShareScreenshotService::class.java)
            intent.putExtra(EXTRA_SHARE_VIDEO_RECORD_PATH, iPath)

            return PendingIntent.getService( //
                    context, //
                    SHARE_VIDEO_RECORD_SERVICE_REQUEST_CODE, //
                    intent, //
                    PendingIntent.FLAG_UPDATE_CURRENT //
            )
        }
    }
}

Usage

NotificatonBuilder.addAction(R.drawable.ic_share, iC.getString(R.string.share), ShareScreenshotService.pendingIntent(iC, iImagePath))

Also don't forget to add Service to Manifest file

Upvotes: 0

Nikul Vaghani
Nikul Vaghani

Reputation: 59

share.setType("text/plain"); and try again

Upvotes: 1

Mardann
Mardann

Reputation: 2163

Had the same problem - solution was in defining the MIME type: when trying to share an intent with text and an attached image setting sharingIntent.setType("*/*") would work fine, but would fail when sharing only text as described above.

Solution: if sharing ONLY text set sharingIntent.setType("text/plain")

public void sendShareToWhatsAppIntent() {

    //setup intent:
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);

    //setup image extra, if exists:
    Bitmap picBitmap = getMyBitmap();
    if (picBitmap != null) {
        String url = MediaStore.Images.Media.insertImage(context.getContentResolver(), picBitmap, "", "");
        sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));
        sharingIntent.setType("*/*");
    } else {
    //if no picture, just text set - this MIME
        sharingIntent.setType("text/plain");
    }

    //setup sharing message
    String message = "My Message - hey whatsapp!"

    sharingIntent.putExtra(Intent.EXTRA_TEXT, message.toString());

   //target WhatsApp:
   sharingIntent.setPackage("com.whatsapp");


    if (sharingIntent.resolveActivity(context.getPackageManager()) != null) {
        startActivity(sharingIntent);
    } else {
        Log.w(TAG, "sendShareIntent: cant resolve intent");
        Toast.makeText(context, "whatsapp not installed", Toast.LENGTH_SHORT).show();
    }

}

Upvotes: 8

Ali Sabir
Ali Sabir

Reputation: 1

In my side Its working fine on below Android 6.0 devices. I was facing this issue on Android 6.0. And Issue was just "External Storage permission was not granted by user. " Now check for External Storage Permission before initiating sharing intent...

Upvotes: -1

Related Questions