java - Android 是否有与 Java 的 Desktop.getDesktop() 类似的类用于打开文件或提示用户选择应用程序关联?

标签 java android android-file

我们正在尝试做的事情:我们正在尝试将现有的 Java 桌面应用程序复制到 Android 应用程序(也基于 Java)。桌面应用程序使用以下桌面类代码来打开传递给它的任何形式的文件类型(例如 docx、xlsx、txt、pdf.jpg 等)。此代码将向关联的应用程序打开文件,或者提示用户选择一个应用程序(如果没有关联或不存在)。

我们的问题:作为 Android 开发和系统类的新手,我们无法正确识别 Android 中是否存在这样的类,也无法使用正确的术语来找到它并可以使用您的帮助。

Desktop.getDesktop().open(file); // Java version

最佳答案

我确实根据 CommonWare 使用 Intent 的建议解决了我的问题(见上文)。但是,我确实遇到了许多 Uri 路径问题,并且在构建 Intent 中使用的 Uri 时需要使用 FileProvider 来处理内部文件路径。

我提供了用于构建传递给 Intent 的 Uri 路径的代码(包装在 openFile(Uri uri) 方法中)。如果它有助于查看我如何进行的完整上下文使用了 Intent,你可以在这里看到它:( Why would an Android Open File intent hang while opening an image? )。

处理实际 Intent 的 openFile():

private void openFile(Uri uri){
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(uri);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivityForResult(intent, 2);
}

TableRow onClick() 从 TableRow 值创建 Uri Uri 是通过 FileProvider 从 File 类调用开始构建的。

row.setOnClickListener(v -> {

    TableRow tablerow = (TableRow) v;
    TextView sample = (TextView) tablerow.getChildAt(1);
    String result = sample.getText().toString();

    File filePaths = new File(getFilesDir().toString());
    File newFile = new File(filePaths, result);
    Uri contentUri = getUriForFile(getApplicationContext(), "com.mydomain.fileprovider", newFile);
    openFile(contentUri);

});

关于java - Android 是否有与 Java 的 Desktop.getDesktop() 类似的类用于打开文件或提示用户选择应用程序关联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60659467/

相关文章:

java - 在框架上定位 GUI 对象

java - android 2.2 中的 jsoup 问题 - 仅在第二次尝试时连接?

android - 截取屏幕截图时更改小部件内容

android - 同一文件上的多个 FileObserver 失败

android - 将文件私下保存到内部存储

android - 下载的文件会自动删除

java - 如何使用 "extend"类在 Android 中使用 "base"PreferenceActivity 类?

java - 使用 SeekableByteChannel 从文件中读取行

android - 将 ABS ViewPager 与自定义 ActionBar View 结合使用

android - 外部播放器 : Uri : Not able to load local media Android