簡體   English   中英

Android 是否有與 Java 的 Desktop.getDesktop() 類似的類,用於打開文件或提示用戶選擇應用程序關聯?

[英]Does Android have a similar class to Java's Desktop.getDesktop() for opening files or prompting the user to choose an application association?

我們正在嘗試做什么:我們正在嘗試將現有的 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 的完整上下文,您可以在此處查看: ( 為什么 Android 打開文件意圖會在打開圖像時掛起? )。

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

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM