當前製作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 –