Pdf Printing View Issue
I have tried in two ways, 1) Am creating a WebView and loading my pdf document, and my application is almost done with its part of the printing process. But in that am facing print
Solution 1:
The above procedure is very hard.Even am not getting solution for that.After that i come up with a solution and its working perfectly for me. 1) To view PDF file no need to load with webview or external pdf libraries.Just download the pdf file and view it with default pdf viewer.The below code i have used,
To download a file,
import android.app.Activity;
import android.util.Log;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
publicclassFileDownloader {
privatestaticfinalintMEGABYTE=1024 * 1024;
publicstaticvoiddownloadFile(String fileUrl, File directory, Activity activity){
try {
URLurl=newURL(fileUrl);
HttpURLConnectionurlConnection= (HttpURLConnection)url.openConnection();
//urlConnection.setRequestMethod("GET");//urlConnection.setDoOutput(true);
urlConnection.connect();
InputStreaminputStream= urlConnection.getInputStream();
FileOutputStreamfileOutputStream=newFileOutputStream(directory);
inttotalSize= urlConnection.getContentLength();
byte[] buffer = newbyte[MEGABYTE];
intbufferLength=0;
while((bufferLength = inputStream.read(buffer))>0 ){
fileOutputStream.write(buffer, 0, bufferLength);
}
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
privateclassDownloadFileextendsAsyncTask<String, Void, Void> {
@Overrideprotected Void doInBackground(String... strings) {
StringfileUrl= strings[0];
StringfileName= strings[1];
StringextStorageDirectory= Environment.getExternalStorageDirectory().toString();
Filefolder=newFile(extStorageDirectory, "Test");
folder.mkdir();
FilepdfFile=newFile(folder, fileName);
try {
pdfFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
FileDownloader.downloadFile(fileUrl, pdfFile,InventoryStockActivity.this);
returnnull;
}
}
publicvoiddownload(String viewUrl) {
newDownloadFile().execute(viewUrl, "Test.pdf");
Log.d("Download complete", "----------");
}
To view a pdf file;
publicvoidview() {
FilepdfFile=newFile(Environment.getExternalStorageDirectory() + "/Test/" + "Test.pdf");
Uripath= Uri.fromFile(pdfFile);
IntentpdfIntent=newIntent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(pdfIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(InventoryStockActivity.this, "No Application available to view PDF", Toast.LENGTH_SHORT).show();
}
}
In manifest,
<uses-permissionandroid:name="android.permission.INTERNET" /><uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permissionandroid:name="android.permission.READ_EXTERNAL_STORAGE" /><uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permissionandroid:name="android.permission.READ_PHONE_STATE"></uses-permission>
And when its open default pdf viewer, there will be print menu.Just print from there.
Post a Comment for "Pdf Printing View Issue"