How to Open PDF in android in its default PDF viewer?
For Opening PDF in android application in its default PDF viewer
- Copy the PDF file in the assets folder.
- then we've to copy it to the phones sdcard or internal memory.
this can be done by using following Code:
private void CopyReadAssets()
{
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), "abc.pdf");
try
{
in = assetManager.open("abc.pdf");
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e)
{
Toast.makeText(rn1.this, "NO Pdf Viewer", Toast.LENGTH_SHORT).show();
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file://" + getFilesDir() + "/14.pdf"),
"application/pdf");
startActivity(intent);
}
private void copyFile(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
}
COPY this method in any Onclick event You want
But If no pdf reader is found the application will be terminated.
Dont forget to add the line in the android manifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
No comments:
Post a Comment