Reputation: 333
I am receiving bytes of data like
JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQp4nNVZyY7jNhC991foHCBOVXERBQQBJFu+D9BAfiALkEOAzGV+f15Ra2shqaQz6IFhWzJp8tX+WKIbV19e/qmo+pFw6VnwWTf6+fn36tcfqr/joL4+//nSvb44fwtVze7mq9ffqp+eXDFVr3/8TDy+hAxZcuSpxl3AXYOrFvcdrpju+H7gyuG7pydmeHL4I7OwwZdlvdHRhv0vr3+99K8vnw5BkL3JCYjpdSfPgMGBG2zm5uVbAGhx4fHucH/H+5HazEMlTdlmQG655ycTWdGBTli1AdXgApqYJZ7gbH8Xs/mfHSCKaqUVj18zuvGBb+4S3BA3rqORsOU5NBjTMa/n446SYGoqNZSEuH0nDZaGgegprW4hHcb9BGMGd5fBjIPnXNOQC/i8oKG30OgOGMvdBGg9xy+zyOssmDU5zho0Ngna1qVmlQ7msTMwEpgp6qoWw608YMhRmwIDcwcgfQyVa1o0HpmiLAZr3gf/DE8hDNuNEHrpkxuLLTWf5pLFdyAb5Fz50Grr5IYkOUnlqZZNLeIautUZ1NNrcLqV9xtYCKrrY9JSCZroeNFeuHvgOzMb0cuH8SSYC3uzSYKvm5stA7/Kt/dSUYAtqBMeh7yGeBKcz3nDBK2VbgfwLi3KFsIXvwd5cFBjQh8AltwUn6XmnHwkINQ6OGQnD9XKNQ1t/50EZ93NXAMXDdBCEWOl3moluZ0p9I2YA5+DIZILMpfiP43wC6o1m1WmmCDWYiy9MVgDCXqMJGjKWCiHYlaN/5Re556JZMOZTCkl2KbZ0Y67AoOYJqZpJHLjYvLuBqgH4xSr+izMfob+glI6ior7UaAktLrOVvW3MZewkqb7spiz3mWJ2BX30CKhxU+reBcrsxk4j8RAiGOaaHfzVo6hm8xkrcQZOJxJkhTd2V31/yjOYE2pM5TGP7hz
this bytes are related to pdf.
and String cannot able to store this much of data. So I am using stringbuffer to store this. But stringbuffer also cannot able to store. so how to store this received data.
how to convert this bytes and show in web view.
Update:
I am using the following code.
byte[] decodedString = Base64.decode(ByteCode.toString());
wv.loadData(decodedString.toString(), "application/pdf", "utf-8");
where ByteCode is a type StringBuffer holding the response bytecode from service.
Upvotes: 7
Views: 13116
Reputation: 3356
It is not possible to preview pdf document in Android webview. It requires third-party library.
build.Gradle
compile 'com.github.barteksc:android-pdf-viewer:2.7.0'
dialog_pdf_viewer
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) 2017.
~ Samet Öztoprak
~ All rights reserved.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:id="@+id/dialog_pdf_viewer_close"
style="@style/ExitButtonImageViewStyle"
android:src="@drawable/popup_exit" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/white"
android:orientation="vertical">
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<View style="@style/HorizontalLine" />
<com.pozitron.commons.customviews.ButtonFont
android:id="@+id/dialog_pdf_viewer_button"
style="@style/ButtonPrimary2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="@string/agreed" />
</LinearLayout>
DailogPDFViewer.java
public class DialogPdfViewer extends Dialog {
PDFView pdfView;
byte[] decodedString;
public interface OnDialogPdfViewerListener {
void onAgreeClick(DialogPdfViewer dialogFullEula);
void onCloseClick(DialogPdfViewer dialogFullEula);
}
public DialogPdfViewer(Context context, String base64, final DialogPdfViewer.OnDialogPdfViewerListener onDialogPdfViewerListener) {
super(context);
setContentView(R.layout.dialog_pdf_viewer);
findViewById(R.id.dialog_pdf_viewer_close).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onDialogPdfViewerListener.onCloseClick(DialogPdfViewer.this);
}
});
findViewById(R.id.dialog_pdf_viewer_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onDialogPdfViewerListener.onAgreeClick(DialogPdfViewer.this);
}
});
decodedString = Base64.decode(base64.toString(), Base64.DEFAULT);
pdfView = ((PDFView) findViewById(R.id.pdfView));
pdfView.fromBytes(decodedString).load();
setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
onDialogPdfViewerListener.onCloseClick(DialogPdfViewer.this);
}
return true;
}
});
}
}
Upvotes: 5
Reputation: 3679
The WebView does not contain a PDF plugin that allow you to display a PDF document.
One solution is to use an Intent object to launch a third-party app (such as Adobe Acrobat) which can handle the PDF document. However, this will transfer control over to the third-party app.
If you insists on displaying the PDF document within your own activity using a WebView, you can use the following trick. You can use Google Docs to open your PDF document and then load the URL of Google Docs using the WebView :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
WebView webView=new WebView(MainActivity.this);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginState(PluginState.ON);
//---you need this to prevent the webview from
// launching another browser when a url
// redirection occurs---
webView.setWebViewClient(new Callback());
String pdfURL = "http://dl.dropboxusercontent.com/u/37098169/Course%20Brochures/AND101.pdf";
webView.loadUrl("http://docs.google.com/gview?embedded=true&url=" + pdfURL);
setContentView(webView);
}
private class Callback extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(
WebView view, String url) {
return(false);
}
}
Upvotes: 1
Reputation: 4869
From your comments, it appears that your data is not too long for a string, merely too long for logging to display the string. (Logging has a limit of around 4000 characters).
You seem to be working along the right lines
However, trying to just load that data into a web view is not supported on most (if not all) versions of Android.
I suggest that instead you
Upvotes: 3