我正在尝试使用网络视图显示保存在Firebase存储中的PDF文件。但是我收到的视图只有顶部是空白和灰色的。
public class PerformanceFragment extends Fragment {
private WebView webView;
private String postUrl = "https://firebasestorage.googleapis.com/v0/b/sas-android-73b28.appspot.com/o/sequence.pdf?alt=media&token=9fc1de4e-1059-4b2b-865e-1c44453a7eb8";
public PerformanceFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_performance, container, false);
webView = root.findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + postUrl);
webView.setHorizontalScrollBarEnabled(false);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
getActivity().setTitle("Loading...");
getActivity().setProgress(progress * 100);
if(progress == 100)
getActivity().setTitle(R.string.app_name);
}
});
return root;
}
}
fragment_performance.xml
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".PerformanceFragment">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</android.support.v4.widget.NestedScrollView>
我已通过身份验证,但什么也没有显示。下面是屏幕截图:1
任何帮助将不胜感激。
答案 0 :(得分:1)
final ProgressDialog pDialog = new ProgressDialog(getActivity());
pDialog.setTitle("Course PDF");
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
webView=view.findViewById(R.id.pdfviewr);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
pDialog.show();
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
pDialog.dismiss();
}
});
String url="";
String pdf = "https://firebasestorage.googleapis.com/v0/b/mindlabz-6f809.appspot.com/o/course_content%2FCracking%20the%20Coding%20Interview.pdf?alt=media&token=12a7fb3d-92d8-49f6-bd13-f88ec634c06c";
try {
url=URLEncoder.encode(pdf,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
webView.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url="+url);
return view;
}
只需尝试此代码
URL转换为UTF-8,它也可以从firebase加载。
答案 1 :(得分:0)
尝试一下:-
WebView wv = root.findViewById(R.id.webView);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
wv.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + postUrl);
,您可以使用显示进度条,因为加载pdf可能需要一些时间。