How to open PDF file in android emulator - android

I would like to know how to open a PDF file?
What all changes do we need to make in the manifest file.
Can I open it in a browser? What all permissions would be required for it?

If there is an application installed that handles the application/pdf mime type then you can open it. You cannot specify that it only be opened in browser unless browser is the only application that handles it. Following is the code snippet for opening a pdf 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(OpenPdf.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}

Related

How to open a 3D-PDF file with an external app via Android Intent

I have downloaded a 3D-PDF file within my Android app, which I want to open in an external app via an Intent. I'm using "3D PDF Reader" as the external app.
This is the source code in Java:
Uri fileUri = FileProvider.getUriForFile(MainActivity.this,
BuildConfig.APPLICATION_ID + ".provider", file);
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(fileUri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
} catch (ActivityNotFoundException e) {
showMessage("Keine App zur Anzeige von PDF-Dokumenten gefunden!");
}
And the error message is: "Sorry, 3D content could not be loaded from this PDF. The document is either secured or contains no 3D content."
I am able to show normal PDF files in an external app with this code. When I use the app "Total Commander" (a file manager app) to click on the 3D-PDF, which was downloaded by my app, I am able to load the 3D-PDF file in the external app ("3D PDF Reader"). So it must be possible to achieve my goal.
Any help is very much appreciated.

Android open PDF from URI starting content

I am trying to open up PDF using intent that takes the following:
Here is the code that I have:
Intent intent = new Intent(Intent.ActionVIEW)
intent.setDataAndType(Uri.parse("CONTENTURI + FILENAME","application/pdf"
try { startActivity(intent)} catch excpetion and so on.
It pops up with whatever applications I have installed, from Adobe Reader, Google PDF reader, POLARIS(as I am using Galaxy Tab 3 for testing), and none of those work. Each say unsupported document.
Does download and show the right solution in this case, or any ideas? Thanks in advance!
Following code triggers the VIEW action for pdf files (open the default PDF viewer, choose between applications capable to view PDFs or get an error if you don't have any installed).
File file = new File("your pdf path here");
if (file.exists()) {
Uri pdfPath = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(pdfPath, "application/pdf");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
//if user doesn't have pdf reader instructing to download a pdf reader
}
}
NOTE: The PDF should not be saved local to application, or else the third party app won't have access. You should use media location.

Android - Open a file with specific app using intent

How can I open a .pdf file with a specific app like adobe reader using an intent?
Something like:
Intent intent = getPackageManager().getLaunchIntentForPackage("com.package.name");
intent.setData(Uri.fromFile(file)); //? - set file to open
startActivity(intent);
I don't want to select an app from a list, the intent must open the file using the specified app.
If you want to open other application then you have to give package name.
Check below code.
try {
Intent mIntent = new Intent(Intent.ACTION_VIEW);
mIntent.setDataAndType(Uri.fromFile(file), "application/pdf");
mIntent.setPackage("com.adobe.reader");
startActivity(Intent.createChooser(mIntent, "View PDF"));
} catch (Exception e) {
//App not found
e.printStackTrace();
}
If you want to open anyother PDF Viewer app then just change PackageName Instead of "com.adobe.reader"
You can use this answer on another thread. It is for ACTION_SEND, but you can adapt it for ACTION_VIEW in your case.

File not exist occurs only for XLSX file in android

I have trying to open XLSX file in android using intent filter to select existing XLSX reader application but always getting file not found exception. The same code works fine for XLS file. I added WRITE_EXTERNAL_STORAGE permission in manifest and file path being correct only. File also available in SD Card. My tablet have sheet to go and OfficeSuite application to read XLSX files. Even i can able to open my XLSX file by clicking on it. But trying to open via my application gives problem. For XLSX file my code always goes to else part. Thanks in advance.
private void openSpreadSheet(String fileCompletePath) {
File file = new File(fileCompletePath);
if (file.exists()) {
try {
Log.d(TAG, "File exist");
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/vnd.ms-excel");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
Toast.makeText(this,
"No application found to display spread sheet.",
Toast.LENGTH_SHORT).show();
}
} else {
Log.d(TAG, "File not exist");
}
}
If i remove if condition, then intent filter asked for choose application after choosing application, that selected application shows this error File not found(/Duplicate.xlsx:open failed:ENOENT(No such file or directory)).
Found Solution
Issue comes because I gave wrong file path.

Displaying PDF in a view in Android

In Android I need to display a PDF file, which is in the asset folder, in a view. Can anyone help me out with the sample code?
I don't think its possible to display it in a view. You will have to make an Intent and then call an external program to do so.
File sd = new File("example.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(sd));
intent.setDataAndType(Uri.fromFile(sd), "application/pdf");
try {
getContext().startActivity(intent);
} catch(ActivityNotFoundException e){
// Show a message that a PDF viewer is not installed
Toast.makeText("No PDF reader available",Toast.LENGTH_LONG).show();
}

Categories

Resources