Search code examples
androidandroid-intentshareshare-intent

How to generate Product link?


I want share url of product, product name to some user using either what's app or facebook etc.when user clicks on that product same product page in app should be opened if app is installed. if app is not installed it should navigate to play store.Now how to generate that sharable link so that same page in app should be opened when user clicks

Here is my code

// share on social websites
    public void shareItem(String url) {
        Log.e("image",productimage);
        Picasso.with(getApplicationContext()).load(url).into(new Target() {
            @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                Intent i = new Intent(Intent.ACTION_SEND);
                i.setType("*/*");
                i.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(bitmap));
                i.putExtra(Intent.EXTRA_TEXT, name.getText().toString()+ "\n" +productimage);
                startActivity(Intent.createChooser(i, "Share Image"));
            }
            @Override public void onBitmapFailed(Drawable errorDrawable) { }
            @Override public void onPrepareLoad(Drawable placeHolderDrawable) { }
        });
    }
    public Uri getLocalBitmapUri(Bitmap bmp) {
        Uri bmpUri = null;
        try {
            File file =  new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
            FileOutputStream out = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.close();
            //bmpUri = Uri.fromFile(file);
            bmpUri=FileProvider.getUriForFile(getApplication(),BuildConfig.APPLICATION_ID+".provider",file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bmpUri;
    }

Solution

  • you should add an intent filter in manifest file. An intent filter should contain the following elements and attribute values;

    Define ACTION_VIEW intent action so that the intent filter can be reached from Google Search.

    <action android:name="android.intent.action.VIEW" />
    

    We should include the BROWSABLE category in order to be accessible from a web browser. We should also have DEFAULT category for responding to implicit intents

    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    

    Lastly, We should define one or more tags. Each of these tags represents a URI format that resolves to the activity. The following example represents a simple data tag for test.com Android app.

     <data
            android:host="test.com"
            android:scheme="https" />
    

    How to read data from incoming intents

    When you define your intent filter that can handle specific URLs, the system can start your activity via that intent filter.

    Intent intent = getIntent();
    Uri data = intent.getData();
    

    if you pass the query parameter as test.com?productID=123 you can retrieve it from

    data.getQueryParameter("productID");