当前位置: 移动技术网 > 移动技术>移动开发>Android > 荐 Android第三方应用分享:接收显示图片

荐 Android第三方应用分享:接收显示图片

2020年07月17日  | 移动技术网移动技术  | 我要评论

1、前言

本demo实现的功能是接收第三方应用的系统分享内容,并显示出来,先看看效果图:

在这里插入图片描述
(左图为分享页面,右图为接收分享页面)

2、实现原理

通过Intent获取到第三传过来的分享内容,一般是文字描述加链接,获取其中的文字,按自己想要的方式显示出来即可,然后通过其中的地址链接,去获取网页源码,解析其中的图片地址,拿到图片地址将其显示到imageview即可,有些网站无法获取完整源码或拿不到图片地址,则直接取网站的favicon.ico。

3、代码实现

1、在对应的activity中注册拦截:

            <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>

2、获取分享过来的内容:

		Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();
        if (Intent.ACTION_SEND.equals(action) && type != null) {
            if ("text/plain".equals(type)) {
                dealTextMessage(intent);
            } else if (type.startsWith("image/")) {
                dealPicStream(intent);
            }
        } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
            if (type.startsWith("image/")) {
                dealMultiplePicStream(intent);
            }
        }

	void dealPicStream(Intent intent) {
        Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
    }

    void dealMultiplePicStream(Intent intent) {
        ArrayList<Uri> arrayList = intent.getParcelableArrayListExtra(intent.EXTRA_STREAM);
    }

    void dealTextMessage(Intent intent) {
   		//处理文字分享
        String share = intent.getStringExtra(Intent.EXTRA_TEXT);
        String title = intent.getStringExtra(Intent.EXTRA_TITLE);
    }

我们处理文字分享,主要在dealTextMessage方法中,接下来我们去解析其中的地址:

        String regex = "(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";
        String sUrl = filterSpecialStr(regex, share);
        
     /**
     * 参数1 regex:我们的正则字符串
     * 参数2 就是一大段文本,这里用data表示
     */
    private String filterSpecialStr(String regex, String data) {
        //sb存放正则匹配的结果
        StringBuilder sb = new StringBuilder();
        //编译正则字符串
        Pattern p = Pattern.compile(regex);
        //利用正则去匹配
        Matcher matcher = p.matcher(data);
        //如果找到了我们正则里要的东西
        while (matcher.find()) {
            //保存到sb中,"\r\n"表示找到一个放一行,就是换行
            sb.append(matcher.group()).append("\r\n");
        }
        return sb.toString();
    }

拿到内容中的地址后,用Jsoup去获取该网页的源码,然后去源码中拿图片地址:

    /**
     * 获取页面内容图片路径
     */
    public static List<String> getImgSrcListFromHtml(/*String html*/Document document, String url) throws MalformedURLException {
        List<String> list = new ArrayList<>();
        //获取目标
//        Document document = Jsoup.parse(html);
        Elements elements = document.select("img");//img.lazy
        Elements elements2 = elements.select("img.lazy");//img.lazy
        //先找lazy标签,正文配图一般用此标签
        int len = elements2.size();
        for (int i = 0; i < len; i++) {
            list.add(elements2.get(i).attr("data-original"));
        }
        //找不到再找img标签
        if (list.size() == 0) {
            int size = elements.size();
            for (int i = 0; i < size; i++) {
                String u = elements.get(i).attr("src");
                if (!u.startsWith("http")) {
                    u = "http:" + u;
                }
                if (u.endsWith(".jpg")) {
                    list.clear();
                    list.add(u);
                    return list;
                }
                list.add(u);
            }
        }
        //实在找不到就用icon
        if (list.size() == 0) {
            list.add(getIconUrlString(url));
        }
        return list;
    }

拿到图片地址之后想干嘛就干嘛了。

4、完整代码

public class MainActivity extends AppCompatActivity {

    private TextView tvTitle;
    private TextView tvContent;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvTitle = findViewById(R.id.tv_title);
        imageView = findViewById(R.id.image);
        tvContent = findViewById(R.id.tv_content);
        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();
        if (Intent.ACTION_SEND.equals(action) && type != null) {
            if ("text/plain".equals(type)) {
                dealTextMessage(intent);
            } else if (type.startsWith("image/")) {
                dealPicStream(intent);
            }
        } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
            if (type.startsWith("image/")) {
                dealMultiplePicStream(intent);
            }
        }
    }

    void dealTextMessage(Intent intent) {
        String share = intent.getStringExtra(Intent.EXTRA_TEXT);
        String title = intent.getStringExtra(Intent.EXTRA_TITLE);
        String regex = "(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";
        String sUrl = filterSpecialStr(regex, share);
        String[] aUrl = sUrl.split("http");
        if (aUrl.length > 2) {
            sUrl = "http" + aUrl[2];
        }
        final String url = sUrl;

        String[] str;
        if (TextUtils.isEmpty(title)) {
            assert share != null;
            str = share.split(url);
            if (str.length > 0) {
                title = str[0];
            }
        }
        tvTitle.setText(title);
        tvContent.setText(share);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    final List<String> list = getImgSrcListFromHtml(Jsoup.connect(url).get(), url);//Jsoup.connect(url).get()
                    if (list.size() == 0) {
                        return;
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Glide.with(MainActivity.this)
                                    .load(list.get(0))
                                    .into(imageView);
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    void dealPicStream(Intent intent) {
        Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
    }

    void dealMultiplePicStream(Intent intent) {
        ArrayList<Uri> arrayList = intent.getParcelableArrayListExtra(intent.EXTRA_STREAM);
    }

    /**
     * 获取页面内容图片路径
     */
    public static List<String> getImgSrcListFromHtml(/*String html*/Document document, String url) throws MalformedURLException {
        List<String> list = new ArrayList<>();
        //获取目标
//        Document document = Jsoup.parse(html);
        Elements elements = document.select("img");//img.lazy
        Elements elements2 = elements.select("img.lazy");//img.lazy
        //先找lazy标签,正文配图一般用此标签
        int len = elements2.size();
        for (int i = 0; i < len; i++) {
            list.add(elements2.get(i).attr("data-original"));
        }
        //找不到再找img标签
        if (list.size() == 0) {
            int size = elements.size();
            for (int i = 0; i < size; i++) {
                String u = elements.get(i).attr("src");
                if (!u.startsWith("http")) {
                    u = "http:" + u;
                }
                if (u.endsWith(".jpg")) {
                    list.clear();
                    list.add(u);
                    return list;
                }
                list.add(u);
            }
        }
        //实在找不到就用icon
        if (list.size() == 0) {
            list.add(getIconUrlString(url));
        }
        return list;
    }

    /**
     * 参数1 regex:我们的正则字符串
     * 参数2 就是一大段文本,这里用data表示
     */
    private String filterSpecialStr(String regex, String data) {
        //存放正则匹配的结果
        StringBuilder sb = new StringBuilder();
        //编译正则字符串
        Pattern p = Pattern.compile(regex);
        //利用正则去匹配
        Matcher matcher = p.matcher(data);
        //如果找到了我们正则里要的东西
        while (matcher.find()) {
            //保存到sb中,"\r\n"表示找到一个放一行,就是换行
            sb.append(matcher.group()).append("\r\n");
        }
        return sb.toString();
    }

    // 获取Icon地址
    public static String getIconUrlString(String urlString) throws MalformedURLException {
        URL url = new URL(urlString);
        return url.getProtocol() + "://" + url.getHost() + "/favicon.ico";
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_margin="20dp"
        android:background="@color/cardview_light_background"
        tools:ignore="MissingConstraints">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="6dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxEms="20"
                android:singleLine="true"
                android:text="Hello World!"
                android:textColor="#000000"
                android:textSize="18sp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="4dp">

                <TextView
                    android:id="@+id/tv_content"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:maxLines="3"
                    android:padding="3dp"
                    android:text="Hello World!" />

                <ImageView
                    android:id="@+id/image"
                    android:layout_width="60dp"
                    android:layout_height="60dp"
                    android:scaleType="centerCrop" />
            </LinearLayout>
        </LinearLayout>
    </androidx.cardview.widget.CardView>
</LinearLayout>

5、项目下载地址

项目下载地址

本文地址:https://blog.csdn.net/qq_24125575/article/details/107357382

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网