从web打开.docx或.doc文件时出现android.content.ActivityNotFoundException

时间:2018-04-09 17:44:00

标签: android android-webview

当我尝试从网络打开.docx或.doc或.txt文件时出现以下错误但我没有看到此异常,如果文件类型为.pdf或.jpg,我可以打开文件

java.lang.RuntimeException: An error occurred while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:318)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:761)
 Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=http://10.0.2.2:8081/... typ=application/msword flg=0x14000000 (has extras) }
    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1809)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1523)
    at android.app.Activity.startActivityForResult(Activity.java:4224)
    at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:68)
    at android.app.Activity.startActivityForResult(Activity.java:4183)
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:751)
    at android.app.Activity.startActivity(Activity.java:4507)
    at android.app.Activity.startActivity(Activity.java:4475)
    at com.example.test.OpenFileTask.openDoc1(OpenFileTask.java:89)
    at com.example.test.OpenFileTask.doInBackground(OpenFileTask.java:24)
    at android.os.AsyncTask$2.call(AsyncTask.java:304)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
    at java.lang.Thread.run(Thread.java:761) 

以下是网址 http://10.0.2.2:8081/test/download/Test.doc?ticket=255c000161_word_l5906nc7ggozg.doc

如果我在浏览器中使用上面的网址

,浏览器会下载该文件

以下是我在 MainActivity.java

中使用的代码
Uri path = Uri.parse(url);

    Intent intent = new Intent(Intent.ACTION_VIEW);
    String mimeType = getMimeType(url);
    intent.setDataAndType(path, mimeType);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    activity.startActivity(intent);

的manifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
    android:allowBackup="true"
    android:icon="@drawable/test_app_icon"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <meta-data android:name="android.webkit.WebView.MetricsOptOut"
               android:value="true" />
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

我无法理解上面的代码如何适用于.pdf / .jpg文件,并且不适用于.doc或.docx或.txt文件。我还没有测试过ppt和xlsx文件

我的设备上安装了Google文档和OfficeSuite应用程序。

我是Android开发的新手,非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

最有可能的是,您安装的应用程序能够查看本地文件,但无法从Internet下载它们。自己下载文件(例如,通过OkHttp),设置FileProvider以使文件可用,然后使用FileProvider.getUriForFile()UriIntent中使用。< / p>