欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

安卓应用加入分享和其它应用打开列表。QQ,微信,文件管理等等

程序员文章站 2022-04-15 18:44:19
...

AndroidManifest.xml

注:我是测试.doc文件所以mimeType=“application/msword”,其它文件可以自行百度mimeType。如果分享和打开列表没有自己的应用就尝试更改mimeType和scheme

<activity android:name="com.example.lwyread.activity.ShareActivity"
            android:allowTaskReparenting="true"
            >
            <intent-filter>
                //其它方式打开
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="file"/>
                <data android:scheme="content"/>
                <data android:mimeType="application/msword"/>

            </intent-filter>
            <intent-filter>
                //分享
                <action android:name="android.intent.action.SEND"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE" />

                <!-- 	指定分享类型 -->
                <data android:mimeType="application/msword"/>
            </intent-filter>
        </activity>

Activity中,只测试了微信,QQ,文件管理的打开和分享

  private void init(){
        Intent intent = getIntent();
        String action = intent.getAction();//action
        String type = intent.getType();//类型
        String filePath = "";
        System.out.println(intent);
        if (Intent.ACTION_SEND.equals(action) && type != null) {
            Uri uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
            //如果是媒体类型需要从数据库获取路径
            filePath = getRealPathFromURI(uri);

        } else if (Intent.ACTION_VIEW.equals(action) && type != null) {
            Uri uri = intent.getData();
            if (uri != null) {
                String scheme = uri.getScheme();
                System.out.println(scheme);
                String host = uri.getHost();
                System.out.println(host);//media或者别的
                String port = uri.getPort() + "";
                System.out.println(port);
                String path = uri.getPath();
                System.out.println(path);
                String query = uri.getQuery();
                System.out.println(query);
                if (host.equals("media")) {
                    filePath = getFilePathFromUri(this, uri);
                } else {
                    filePath = getFilePathFromUri2(this, uri);
                }

            }
        }
        File f = new File(filePath);
        Toast.makeText(this, filePath, Toast.LENGTH_LONG).show();

    }

    /**
     * 微信intent
     * Intent { act=android.intent.action.VIEW dat=content://com.tencent.mm.external.fileprovider/external/tencent/MicroMsg/Download/测试.doc
     * typ=application/msword flg=0x10400001 pkg=com.example.lwyread cmp=com.example.lwyread/.activity.ShareActivity }
     * QQintent
     * Intent { act=android.intent.action.VIEW cat=[android.intent.category.DEFAULT]
     * dat=content://com.tencent.mobileqq.fileprovider/external_files/storage/emulated/0/Android/data/com.tencent.mobileqq/Tencent/QQfile_recv/测试.doc
     * typ=application/msword flg=0x1 pkg=com.example.lwyread cmp=com.example.lwyread/.activity.ShareActivity (has extras) }
     * 文件管理intent
     * Intent { act=android.intent.action.VIEW dat=content://media/external/file/887 typ=application/msword flg=0x1 cmp=com.example.lwyread/.activity.ShareActivity }
     */
    //这种//media/external/file/887
    public static String getFilePathFromUri(Context context, Uri uri) {
        if (null == uri) return null;
        final String scheme = uri.getScheme();
        String data = null;
        if (scheme == null)
            data = uri.getPath();
        else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
            data = uri.getPath();
        } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
            Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.ImageColumns
                    .DATA}, null, null, null);
            if (null != cursor) {
                if (cursor.moveToFirst()) {
                    int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                    if (index > -1) {
                        data = cursor.getString(index);
                    }
                }
                cursor.close();
            }
        }
        return data;
    }
    //QQ微信这种//com.tencent.mm.external.fileprovider/external/tencent/MicroMsg/Download/测试.doc
    public static String getFilePathFromUri2(Context context, Uri uri) {
        try {
            List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);
            if (packs != null) {
                String fileProviderClassName = FileProvider.class.getName();
                for (PackageInfo pack : packs) {
                    ProviderInfo[] providers = pack.providers;
                    if (providers != null) {
                        for (ProviderInfo provider : providers) {
                            if (uri.getAuthority().equals(provider.authority)) {
                                if (provider.name.equalsIgnoreCase(fileProviderClassName)) {
                                    Class<FileProvider> fileProviderClass = FileProvider.class;
                                    try {
                                        Method getPathStrategy = fileProviderClass.getDeclaredMethod("getPathStrategy", Context.class, String.class);
                                        getPathStrategy.setAccessible(true);
                                        Object invoke = getPathStrategy.invoke(null, context, uri.getAuthority());
                                        if (invoke != null) {
                                            String PathStrategyStringClass = FileProvider.class.getName() + "$PathStrategy";
                                            Class<?> PathStrategy = Class.forName(PathStrategyStringClass);
                                            Method getFileForUri = PathStrategy.getDeclaredMethod("getFileForUri", Uri.class);
                                            getFileForUri.setAccessible(true);
                                            Object invoke1 = getFileForUri.invoke(invoke, uri);
                                            if (invoke1 instanceof File) {
                                                String filePath = ((File) invoke1).getAbsolutePath();
                                                return filePath;
                                            }
                                        }
                                    } catch (NoSuchMethodException e) {
                                        e.printStackTrace();
                                    } catch (InvocationTargetException e) {
                                        e.printStackTrace();
                                    } catch (IllegalAccessException e) {
                                        e.printStackTrace();
                                    } catch (ClassNotFoundException e) {
                                        e.printStackTrace();
                                    }
                                    break;
                                }
                                break;
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 通过Uri获取文件在本地存储的真实路径
     */
    private String getRealPathFromURI(Uri contentUri) {
        String[] proj = {MediaStore.MediaColumns.DATA};
        Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
        if (cursor.moveToNext()) {
            return cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA));
        }
        cursor.close();
        return null;
    }

从网上找到不同教程,自己整合了一下。为自己后续提供方便。

相关标签: 安卓 安卓