Android app出现再第三方应用打开列表,和分享应用列表并获取保存文件

如何调用系统分享

   Intent shareIntent = new Intent(Intent.ACTION_SEND);
   shareIntent.putExtra(Intent.EXTRA_STREAM, getUriForFile(DownLoadTemplateActivity.this, shareFile));
   shareIntent.setType("*/*");//此处可发送多种文件
   startActivity(Intent.createChooser(shareIntent, "分享到:"));

使用第三方应用打开和调用系统分享如何有自己的app

代码如下

      <activity android:name=".module.receivefile.ReceiveFileActivity">
			<!--使用三方应用打开-->
            <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:scheme="http" />
                <data android:scheme="https" />
                <data android:mimeType="*/*" />
            </intent-filter>
			<!--分享-->
  			<intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="*/*" />
            </intent-filter>
        </activity>

重点是 action 和 category data里面的属性根据需要可以更改

如何接收数据:以发送文件为例

  val intent = intent
        val action = intent.action
        val type = intent.type
        if (Intent.ACTION_SEND == action && type != null) { // 分享到其他应用
            val uri = intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)
            uri?.let {
                val realPathFromUri = Uri2PathUtil.getFilePathFromContentUri(this, it)
           		// todo
            }
        } else if (Intent.ACTION_SEND_MULTIPLE == action && type != null) { // 多个文件分享
        	
        } else if (Intent.ACTION_VIEW == action) { // 其他应用打开
            val uri = intent.data
            uri?.let {
                val realPathFromUri = Uri2PathUtil.getFPUriToPath(this, it)
                // todo
            }
        }

根据 Uri 获取文件路径

public class Uri2PathUtil {


    public static String getFilePathFromContentUri(Activity context, Uri selectedVideoUri) {
        String filePath = "";
        String[] filePathColumn = {MediaStore.MediaColumns.DATA};

//      Cursor cursor = contentResolver.query(selectedVideoUri, filePathColumn, null, null, null);
//      也可用下面的方法拿到cursor
        Cursor cursor = context.managedQuery(selectedVideoUri, filePathColumn, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            filePath = cursor.getString(columnIndex);
            try {
                //4.0以上的版本会自动关闭 (4.0--14;; 4.0.3--15)
                if (Integer.parseInt(Build.VERSION.SDK) < 14) {
                    cursor.close();
                }
            } catch (Exception e) {
                Log.e("转换地址", "error:" + e);
            }
        }
        return filePath;
    }

    public static String getFPUriToPath(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;
    }

}

拷贝文件

kotlin拷贝文件还是很方便的 overwriteFile是否覆盖同名文件 srcFile 源文件
targetFile 目标文件

 val copyTo = srcFile.copyTo(targetFile, overwriteFile)
发布了119 篇原创文章 · 获赞 28 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/ldxlz224/article/details/103614440