Chrome 你如何发布(分享)一个新闻项目图片和项目;在WhatsApp的风格的URL?

eufgjt7s  于 2023-06-03  发布在  Go
关注(0)|答案(1)|浏览(202)

当你分享一个新闻项目(从chrome浏览器)到Whatsapp,通常发布的是一个缩略图图像连同页面URL,如下所示:

我正在尝试在名为ReceiveAShare1.0的测试应用程序中复制这种行为

清单如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" >

    <uses-permission
        android:name="android.permission.INTERNET">
    </uses-permission>

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.ReceiveAShare10"
        tools:targetApi="31" >
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTask" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            
            <intent-filter>
                <action android:name="android.intent.action.SEND" />

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

                <data android:mimeType="image/*" />
            </intent-filter>
            
            <intent-filter>
                <action android:name="android.intent.action.SEND" />

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

                <data android:mimeType="text/plain" />
            </intent-filter>
            
            <intent-filter>
                <action android:name="android.intent.action.SEND_MULTIPLE" />

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

                <data android:mimeType="image/*" />
            </intent-filter>
            
        </activity>
    </application>

</manifest>

MainActivity如下:

package com.mo.receiveashare10;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ClipboardManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ImageView receivedImage;
    TextView receivedText;
    EditText typedInText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        receivedImage = (ImageView) findViewById(R.id.received_image);
        receivedText = (TextView) findViewById(R.id.received_text);
        typedInText = (EditText) findViewById(R.id.type_in_edit_text);

        handleIntent(getIntent());
    } 

    public void handleIntent(Intent intent) {
        // if the intent action = ACTION_SEND and its MIME type = "text/plain" then display the text in the Text Received Box
        if (Intent.ACTION_SEND.equals(intent.getAction())  && intent.getType().equals("text/plain")) {
            handleReceivedText(intent);

        } else if (Intent.ACTION_SEND.equals(intent.getAction()) && intent.getType().startsWith("image/") ) {
            handleReceivedImage(intent);
        } else if (Intent.ACTION_SEND_MULTIPLE.equals(intent.getAction()) && intent.getType().startsWith("image/") ) {
            handleReceivedMultipleImages(intent);
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        handleIntent(intent);
    }

    public void handleReceivedText(Intent intent) {

        Log.i("Receive A Share", "in the handleReceivedText() method");

        String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
        if (sharedText != null) {
            receivedText.setText(sharedText);
        }
    }

    public void handleReceivedImage(Intent intent) {

        Log.i("Receive A Share", "In the handleReceivedImage() method");

        Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);

        Log.i("Receive A Share 1.0", "The Uri from intent is : " + imageUri.toString());

        // Glide code:
        // Glide code:
        Glide.with(this)
                .load(imageUri.toString())
                .fitCenter()
                .into(receivedImage);
    }

    public void handleReceivedMultipleImages(Intent intent) {
        ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
        if (imageUris != null) {
            // update the UI to show the multiple shared images
        }

    }
}

问题是我没有得到图片和url链接同时

这是我在测试应用程序中得到的结果:

如何复制WhatsApp帖子的行为?换句话说,我如何才能从在chrome浏览器中查看的新闻网站中接收共享意图,从中我可以提取图像URL?
我已经进行了长期的探索和试验,但没有成功。任何帮助将是非常欢迎!

vsikbqxv

vsikbqxv1#

代码中有一些明显的错误。我很抱歉-例如,第一个if之后的if条件永远无法测试。我也仔细阅读了action Intent.ACTION_SEND_MULTIPLE的文档,我想我已经有了一个解决这个问题的方法,当它被完全测试后,我会发布。

相关问题