【问题标题】:How to generate Product link?如何生成产品链接?
【发布时间】:2019-03-15 09:48:06
【问题描述】:

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

这是我的代码

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

【问题讨论】:

    标签: android android-intent share share-intent


    【解决方案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");
    

    【讨论】:

    • 在链接中传递产品 ID 的位置以便打开确切的产品
    • 示例 example.com/test?data=12345 来自此 Intent intent = getIntent(); URI 数据 = intent.getData();可以获取参数数据
    • 我认为您正在尝试从 deeplink 获取图像 url 并将其传递给 shareItem(String url) 这个方法对
    • 图像 url 不是必需的先生。 www.domain.com/product id 之类的
    • 检查一旦我编辑了答案,如果您将查询参数作为 test.com?productID=123 传递,您可以从 data.getQueryParameter("productId"); 中检索它;
    猜你喜欢
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-20
    • 2019-12-04
    • 2019-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多