1

我想使用what's app或facebook等向某些用户分享产品的url,产品名称。当用户单击该产品时,如果安装了应用程序,则应打开应用程序中的相同产品页面。如果未安装应用程序,则应导航到播放商店。现在如何生成该可共享链接,以便在用户单击时打开应用程序中的同一页面

这是我的代码

// 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;
    }
4

1 回答 1

1

您应该在清单文件中添加一个意图过滤器。意图过滤器应包含以下元素和属性值;

定义 ACTION_VIEW 意图操作,以便可以从 Google 搜索访问意图过滤器。

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

我们应该包含 BROWSABLE 类别,以便可以从 Web 浏览器访问。我们还应该有 DEFAULT 类别来响应隐式意图

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

最后,我们应该定义一个或多个标签。这些标签中的每一个都代表一种解析为活动的 URI 格式。以下示例表示 test.com Android 应用程序的简单数据标签。

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

如何从传入的意图中读取数据

当您定义可以处理特定 URL 的意图过滤器时,系统可以通过该意图过滤器启动您的活动。

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

如果您将查询参数作为 test.com?productID=123 传递,您可以从中检索它

data.getQueryParameter("productID");
于 2019-03-15T10:10:51.903 回答