Open PDF in Android with Adobe Reader [duplicate] - android

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Open PDF in Android
I would like to open a PDF file with Adobe Reader from my android application.
I've a PDF file in /mnt/sdcard called test.pdf, and I'm using the next code:
file = new File ("/mnt/sdcard/test.pdf");
if (file.exists())
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
intent.setType("application/pdf");
intent.setPackage("com.adobe.reader");
startActivity(intent);
}
Adobe Reader is opened when this code is executed, but the file is not opened. The file is valid.
What is the problem?

try this.
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

try this code
File file=new File("/mnt/sdcard/test.pdf");
if(file.exists())
{
Uri path=Uri.fromFile(file);
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
try
{
startActivity(intent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(TestActivity.this, "No software for PDF", Toast.LENGTH_SHORT).show();
}
}

Related

Opening a Pdf file?

I want to open pdf in Android
I am currently using code
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + path.get((int) v.getTag()));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
mContext.startActivity(intent);
But the file is not opening currently.
Regards

How to open xls file programmatically in android?

Hi I have opened PDF file using following code
Intent marketIntent = new Intent(Intent.ACTION_VIEW);
marketIntent.setData(Uri.parse("market://details?id=com.adobe.reader"));
startActivity(marketIntent);
Now i want to do same with XLS file
How should i do this??
Please suggest...
Something like this might work and open an XLS in another XLS viewer application:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/vnd.ms-excel");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(OpenPdf.this, "No Application Available to View Excel",
Toast.LENGTH_SHORT).show();
}

How to read a pdf file stored in sdcard using adobe reader installed in the android device?

i new to android application developemnt. I am trying to open the pdf file stored in sd card.I used this code snippet but this opens the viewer in the device but not the file given in the path. Any help appreciated.
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("Environment.getExternalStorageDirectory().getPath()+/sample.pdf"));
intent.setType("application/pdf");
intent.setPackage("com.adobe.reader"); selects the adobe reader directly
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(intent,0);
if (activities.size() >= 0)
{
startActivity(intent);
} else {
Toast.makeText(this, "No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
you can read pdf file from sdcard by giving the path and try the following.
File pdfFile = new File(path);
if(pdfFile.exists())
{
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try
{
startActivity(pdfIntent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(uractivity.this, resource.getString(R.string.noapplicationpdf), Toast.LENGTH_LONG).show();
}
}
You can try something like
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File(filename);
intent.setDataAndType( Uri.fromFile( file ), "application/pdf" );
startActivity(intent);
or
Intent intent = new Intent();
intent.setPackage("com.adobe.reader");
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
startActivity(intent);

Opening pdf file programmatically is going into menu page?

How come every time i try to open a pdf file in my SDCARD using the following code, it doesn't actually open the pdf file itself, but goes into the menu of adobe reader? Is there anything wrong with my code?
Intent intent = new Intent();
File pdfFile = new File("/sdcard/sample.pdf");
Uri path = Uri.fromFile(pdfFile);
intent.setAction(Intent.ACTION_VIEW);
intent.setData(path);
intent.setType("application/pdf");
intent.setPackage("com.adobe.reader");
startActivity(intent);
No, there's nothing wrong. You've set the type to pdf and specified the package as adobe.reader. What that does is fire off an intent to launch the pdf in adobe reader. There's no way to display a pdf directly in your app without using a library (or writing code yourself) to render PDFs and display them.
Note that if you only set the type and not the package, the system will find whatever app is available to display PDFs
EDIT
You can try something like
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File( filename );
intent.setDataAndType( Uri.fromFile( file ), "application/pdf" );
startActivity(intent);
or
Intent intent = new Intent();
intent.setPackage("com.adobe.reader");
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
startActivity(intent);
Here I am giving my pdf file name. You give your file name.
private static String FILE = Environment.getExternalStorageDirectory().getPath()+"/TestPDF.pdf";
File file = new File(FILE);
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}

How to read a pdf in android

I want to read a PDF file in android.
I placed my PDF files in the assets folder.
How can i read the PDF file from there?
PDF Reader Link
I have checked the above link but it does not work for me.
It gives me an error saying that the Activity was not found.
And I also want to open a PDF file in WebView. So is it possible to read PDF in WebView?
u can download PDFbox API to read PDF in android
try this link: http://pdfbox.apache.org/
You need to have a application which can support that mimetype and open it.
In your device/emulator that app is not installed so it is throwing error
Another great solution to read pdf using externally installed application like Adobe Reader.
private void openPDF(final String pathToPDF) {
File file = new File(pathToPDF);
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, "application/pdf");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "Application not found", Toast.LENGTH_SHORT).show();
}
}
Google cached displays formats like pdf, ppt, docx, etc. And you don't need to install any application at all.
Search the link in google and go to Cached.
private void openPDF(final String path) {
File file = new File(path);
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
} else {
uri = Uri.fromFile(file);
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "Application not found", Toast.LENGTH_SHORT).show();
}
}

Categories

Resources