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

问题描述 投票:0回答:1

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

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

Desktop.getDesktop().open(file); // Java version
java android android-file
1个回答
0
投票

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

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

openFile()处理实际的Intent:

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.mistywillow.fileprovider", newFile);
    openFile(contentUri);

});
© www.soinside.com 2019 - 2024. All rights reserved.