Android 按URL打开文件

Android 按URL打开文件,android,Android,我有FileOpener类,用于按URL打开文档: public class FileOpener { private FileOpener() { } private static String url; private static Uri uri; private static Intent intent; public static void open(Context context, FileDTO fileDTO) {

我有
FileOpener
类,用于按URL打开文档:

public class FileOpener {


    private FileOpener() {
    }

    private static String url;
    private static Uri uri;
    private static Intent intent;

    public static void open(Context context, FileDTO fileDTO) {
        fileDTO.setUrl("https://calibre-ebook.com/downloads/demos/demo.docx");
        setupBase(fileDTO);
        checkFileTypeByUrl();

        try {
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }

    private static void checkFileTypeByUrl() {

    String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url));
    intent.setDataAndType(uri, mimeType);
    }

    private static void setupBase(FileDTO fileDTO) {
        url = fileDTO.getUrl();
        uri = Uri.parse(url);
        intent = new Intent(Intent.ACTION_VIEW, uri);
    }
}
在activity/fragment中,如下所示:

    @Override
    public void openOrDownloadFile(FileDTO file) {
        FileOpener.open(getContext(), file);
    }
这对于
PDF/Txt
文档很好,但是对于
MsWord/MsExcel/MsPowerPoint
have
ActivityNotFoundException
(我的设备上有
word/excel/powerpoint
,可以从其他应用程序打开)

Stacktrace:

W/System.err:android.content.ActivityNotFoundException:无活动 找到用于处理Intent{act=android.Intent.action.VIEW的 dat= typ=application/vnd.openxmlformats-officedocument.wordprocessingml.document flg=0x4000000}


有什么问题吗?

尝试使用web视图,您可以从url加载文件

WebView webview = (WebView)findViewById(R.id.containWebView);
webview.setWebViewClient(new AppWebViewClients());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setUseWideViewPort(true);
webview.loadUrl("http://docs.google.com/gview?embedded=true&url="
            + "YOUR_DOC_URL_HERE"); 

 public class AppWebViewClients extends WebViewClient {



@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    // TODO Auto-generated method stub
    view.loadUrl(url);
    return true;
}

@Override
public void onPageFinished(WebView view, String url) {
    // TODO Auto-generated method stub
    super.onPageFinished(view, url);

}
}

尝试使用web视图,您可以从url加载文件

WebView webview = (WebView)findViewById(R.id.containWebView);
webview.setWebViewClient(new AppWebViewClients());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setUseWideViewPort(true);
webview.loadUrl("http://docs.google.com/gview?embedded=true&url="
            + "YOUR_DOC_URL_HERE"); 

 public class AppWebViewClients extends WebViewClient {



@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    // TODO Auto-generated method stub
    view.loadUrl(url);
    return true;
}

@Override
public void onPageFinished(WebView view, String url) {
    // TODO Auto-generated method stub
    super.onPageFinished(view, url);

}
}

如果您看到应用程序打开文件的操作\u视图意图过滤器示例:

<intent-filter android:label="app">
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="https"
          android:host="www.example.com">
</intent-filter>

您可以看到有一个
android:scheme=“https”
,这意味着此应用程序将只接受URI具有
https
方案的
ACTION\u视图。
在您的情况下,word应用程序只有
文件
方案,不会处理
http
https
URI


因此,首先您必须下载文件,然后才能将其作为本地文件打开。

如果您看到应用程序打开文件的操作\u视图意图过滤器示例:

<intent-filter android:label="app">
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="https"
          android:host="www.example.com">
</intent-filter>

您可以看到有一个
android:scheme=“https”
,这意味着此应用程序将只接受URI具有
https
方案的
ACTION\u视图。
在您的情况下,word应用程序只有
文件
方案,不会处理
http
https
URI


因此,首先您必须下载该文件,然后才能将其作为本地文件打开。

首先您必须下载该文件,然后只有您才能继续打开它。您好,请共享您的实现代码。首先您必须下载该文件,然后只有您可以继续打开它。您好,请共享您的实现代码。