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.
Related
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.
I am trying to view a local pdf in an external pdf viewer using this code:
Uri path = Uri.parse("android.resource://<package-name>/raw/Terms.pdf>");
try
{
Intent intentUrl = new Intent(Intent.ACTION_VIEW);
intentUrl.setDataAndType(path, "application/pdf");
intentUrl.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
getActivity().startActivity(intentUrl);
}
catch (ActivityNotFoundException e)
{
Toast.makeText(getActivity(), "No PDF Viewer Installed", Toast.LENGTH_LONG).show();
}
Even though I have Adobe PDF installed, it throws an ActivityNotFoundExcecption.
Why is that?
Few, if any, PDF viewing apps support the android:resource scheme. You need to provide your PDF file by some other means to PDF viewers, such as via FileProvider, so you can use a scheme (e.g., content) that is more likely to be supported.
I want to download a PDF from a url and also want to trigger catch phrase if no PDF Viewer is detected.
Here's my code:
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(materialPdfUrl)));
} catch (ActivityNotFoundException e) {
openDialog(getString(R.string.error),
getString(R.string.no_pdf_reader));
}
Now the problem is that ActivityNotFoundException is never triggered because it always download the PDF even if there is no PDF Viewer around. How do you suggest I do this?
EDIT:
Here's my old code:
Intent pdfIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(materialPdfUrl));
pdfIntent.setType("application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(pdfIntent);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(materialPdfUrl)));
is starting an Implicit Intent and therefore will not throw ActivityNotFoundException.
If you read this http://developer.android.com/guide/components/intents-filters.html#ccases
Consider, for example, what the browser application does when the user follows a link on a web page. It first tries to display the data (as it could if the link was to an HTML page). If it can't display the data, it puts together an implicit intent with the scheme and data type and tries to start an activity that can do the job. If there are no takers, it asks the download manager to download the data. That puts it under the control of a content provider, so a potentially larger pool of activities (those with filters that just name a data type) can respond.
Therefore if no PDF viewers are found the Android Download Manager will attempt to download the file (rather than throw that exception).
If you want to view the pdf or be told you cannot view it (rather than download) then you will need to query the system manually using the PackageManager to find out if an application will respond to your intent rather than just firing and forgetting.
FYI ActivityNotFoundException will be thrown for Explicit Intent's something like:
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.facebook","NewsFeedActivity.java"));
startActivity(intent);```
I would recommend using the PackageManager to detect if the system will handle a PDF intent for you.
PackageManager packageManager = context.getPackageManager();
Intent intent = new Intent(Intent.ACTION_VIEW)
.setType("application/pdf");
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY;
if (list.size() > 0) {
// Happy days a PDF reader exists
startActivity(intent);
} else {
// No PDF reader, ask the user to download one first
// or just open it in their browser like this
intent = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("http://docs.google.com/gview?embedded=true&url=" + pdfUrl);
startActivity(intent);
}
See this blog post for more info on checking intents. The added benefit of this approach is that you can grey out/remove menu options before the app even tries to execute the Intent. Then you can explain to the user in a slightly more friendly way that they need to grab a PDF viewer app, or indeed apply some logic to fallback to a web based PDF viewer.
Having trialled this approach with Foxit Reader and Adobe Reader they both seem to have different behaviours. Foxit will not download the PDF for you, it will redirect you to the browser and download the file. Adobe will download the file for you then display it.
So to get round this difference once you have detected that a PDF viewer is available then you will probably want to download the PDF to the SD card, for example the downloads folder. This is probably best achieved in an AsyncTask, or you might be able to use the DownloadManager. Then open the local file Uri in the preferred reader. This should get round the difference in behaviour. Maybe open a ticket with Foxit to bring it into line with Adobe? ;)
The function startActivity() you use is not on the condition that the PDF reader is not exist, it only download the PDF from the URL, and if there are PDF Readers then it will offer a selector, just as the function of clicking on a PDF file.
you may try this code. This may helpful to you.
Intent intent = new Intent();
intent.setClassName("com.adobe.reader", "com.adobe.reader.AdobeReader");
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
try {
startActivity(intent);
} catch (Exception e) {
AlertDialog.Builder builder = new AlertDialog.Builder(
getApplicationContext());
builder.setTitle("No Application Found");
builder.setMessage("Download application from Android Market?");
builder.setPositiveButton(
"Yes, Please",
new DialogInterface.OnClickListener() {
#Override
public void onClick(
DialogInterface dialog,
int which) {
Intent marketIntent = new Intent(
Intent.ACTION_VIEW);
marketIntent.setData(Uri
.parse("market://details?id=com.adobe.reader"));
mProgressDialog.dismiss();
startActivity(marketIntent);
}
});
builder.setNegativeButton("No, Thanks",
null);
builder.create().show();
}
That is because your device or emulator does not have an application capable of viewing a local PDF file.
Whenever you start an intent, you should have the native app installed on the emulator to handle that intent. Ex. If you invoke an intent with Maps as the action, you would have to use the Google API's based emulator. By default, android emulator does not have a PDF reader. You could test this on a device with a PDF reader and it should work fine.
use startActivityForResult(..).
See the link here.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
startActivityForResult(intent, REQUEST_CODE);
You will get the result in onActivityResult(..) of your activity.
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();
}
I have to build an Android application which shows a list of pdf files. These pdf files should be secured, in other words - the user of the app should not be able to get a copy of the pdf content by any means (copy/cut/print...etc). My questions now are
How should I ship the content of the pdf file along with .apk file.
If we send the content of the file in a diff format (raw/byte code), how should I convert the file back to pdf and where should I place the file on the installed machine such that it is secure.
FYI, I will be locking down current version of Adobe Redaer as the pdf viewer to view the pdf files as it doesn't allow copy/paste/print.
This is the first Android app that I am developing. So, I'm a kind of Android newbie. Any help would be greatly appreciated.
Thanks,
Navin
Probably you should store it in the res/raw folder
You can copy this(these) pdf file(s) into assets folder, thus your files will be the part of your application and can not be accessible outside, and you can also treat them as normal files (i.e. open with any pdf viewer in your case). Whenever you want to get access to context file you can call context.getAssets() .
Hope this information helps.
Update : Here is the code I am using to open a file in downloads directory, this code can open the pdf file in any compatible pdf reader if installed on phone, Tested with Adobe Reader. You will need a URI of the pdf file in assets folder to run the code.
String fileName = pdfUrl.substring(pdfUrl.lastIndexOf("/"));
Log.i("fileName", "" + fileName);
File file = new File("/sdcard/download" + fileName);
// File file = new
// File("/sdcard/download/airtel_latest000.mp3");
Intent intent;
if (file.exists()) {
Uri path = Uri.fromFile(file);
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
downloaded = true;
} else {
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(pdfUrl));
downloaded = false;
}
try {
startActivityForResult(intent, 5);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}