当前制作WebView HTML应用程序和我到本地PDF文件的链接(路径是相对的)不起作用。我只是希望它在外部应用程序中打开(不在WebView中)。我从本网站尝试了多个PDF意图,但没有成功。从Android Studio在外部应用程序中打开本地PDF HTML文档
我本来的Override
的public Boolean launchPDF
以上,但一直得到
:method does not override or implement method from a supertype
我需要这是对所有PDF毯子声明,因为我有很多。
的AndroidManifest.xml:
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
myWebView.loadUrl("file:///android_asset/index.html");
}
public boolean launchPDF(WebView view, String url) {
if (urlIsPDF(url)) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "application/pdf");
try {
view.getContext().startActivity(intent);
} catch (ActivityNotFoundException e) {
//User does not have pdf viewer installed
}
} else {
myWebView.loadUrl(url);
}
return true;
}
public boolean urlIsPDF(String url) {
return url.endsWith(".pdf");
}
};
“我的本地PDF文件的链接” - 这是什么意思?你的HTML来自'assets /'。如果您的PDF文件也在'assets /'中,您将无法按照您尝试的方式查看它们。 – CommonsWare
我有一个文件夹结构将PDF文件堆在资产文件夹(它复制我的Web服务器)。我需要它离线,因为这将主要被停用。我怎么能让他们在当地工作呢? –
只是因为我使用相同的文件来访问服务器上的文件夹中的这些文件。我可以手动将它们放在设备上的文件夹,然后一些如何将任何pdf(* .pdf)的链接重定向到文件夹(例如下载或AppPdfs –