I'm new in android development so I know how to open pdf from assest folder file by using some of the available API .
But how can I make for any pdf means like adobe reader.
If you would like to open Adobe Reader (with a version) specifically from your app, you need to query it using PackageManager.getPackageInfo(String, int)!
You can also try the following code,
private void loadDocInAdobeReader(String someDoc)
throws ActivityNotFoundException, Exception {
try {
Intent intent = new Intent();
intent.setPackage("com.adobe.reader");
intent.setDataAndType(Uri.parse(someDoc), "application/pdf");
startActivity(intent);
} catch (ActivityNotFoundException ae) {
ae.printStackTrace();
throw ae;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
Related
I have a word document(.doc or .docx) in the server. I need to open in my android application by clicking one button .doc file have to open. How to do this? anyone, please help me.
I tried this code :
File file = new File(Environment.getExternalStorageDirectory(),
"Document.docx");
try {
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent objIntent = new Intent(Intent.ACTION_VIEW);
// replace "application/msword" with
// "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
// for docx files
// objIntent.setDataAndType(path,"application/msword");
objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(objIntent);
} else {
Toast.makeText(PMComplitionProjectActivity.this, "File NotFound",
Toast.LENGTH_SHORT).show();
}
} catch (ActivityNotFoundException e) {
Toast.makeText(PMComplitionProjectActivity.this,
"No Viewer Application Found", Toast.LENGTH_SHORT)
.show();
} catch (Exception e) {
e.printStackTrace();
}
I have this link that shows how to open any file in Android : http://www.androidsnippets.com/open-any-type-of-file-with-default-intent.html
Also, there is a similar case, which is done by PDF file : Android : how to open pdf file from server in android
I hope these links will work for you.
Good Luck,
G.
I am having an issue, I have never had problem opening files via ACTION_VIEW the next way:
File file = new File(getActivity().getFilesDir(), TEMP_FILE_NAME);
String dataType = "image/*";
if (file.exists()) {
Intent fileIntent = new Intent(Intent.ACTION_VIEW);
fileIntent.setDataAndType(Uri.fromFile(file), dataType);
fileIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(fileIntent, "Open file");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
Log.e(TAG, "There is a problem when opening the file");
}
} else {
Toast.makeText(getContext(), "Invalido", Toast.LENGTH_LONG).show();
}
The problem I am having right now is that even though the file exists when I choose the app to open the file it immediately closes and tells me Not found. I have put the image I am loading in an image view and there is no problem, so the file is valid but for some reason it has conflicts when I am opening it via intent.
I am aware that it may have something to do with the way I am creating the file, I am retrieving it from Google drive so I am writing the file using the Apache Commons library the next way:
DriveContents contents = result.getDriveContents();
InputStream inputStream = contents.getInputStream();
File file = new File(getActivity().getFilesDir(), TEMP_FILE_NAME);
try {
OutputStream outputStream = new FileOutputStream(file);
IOUtils.copy(inputStream, outputStream);
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
What is it I am doing wrong? I am not totally sure if the problem has to do with the copy method executing asynchronously or something like that.
Thanks in advance.
I have never had problem opening files via ACTION_VIEW the next way
That code will never work, as third-party apps have no rights to work with files on getFilesDir() of your app.
What is it I am doing wrong?
You are attempting to serve an inaccessible file to third-party programs. Use FileProvider to serve the file, using FileProvider.getUriForFile() to get the Uri to use in your ACTION_VIEW Intent.
I have a case that opens a PDF by going to the sd card and looking for a file, and then opening it. The only things I can think of that would force it throw an exception would be if it couldn't find the file, or the user does not has adobe reader. If they file doesn't exist, adobe reader already displays a dialog and returns to the app, but I would like to catch it before that happens.
case R.id.ImageButton2:
Intent intent2 = new Intent();
intent2.setPackage("com.adobe.reader");
intent2.setDataAndType(Uri.fromFile(new File("/storage/emulated/0/afipresents/brochures/MultiLineBrochure.pdf")), "application/pdf");
startActivity(intent2);
break;
try{
// ur code
} catch (Exception e) { // or u can have specific exceptions
showDialog(title_error,R.drawable.error_icon,msg_error_,label_ok);
e.printStackTrace();
}
Below is my code
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File(SDCardRoot,filename);
intent.setDataAndType(path, "video/quicktime");
try {
startActivity(intent);
}
catch (ActivityNotFoundException ex) {
}
Any idea how to catch the msg without displaying sorry, this video cannot be played when I trying to play mov file format and replace to my own sentences for error opening?
You're currently catching an 'ActivityNotFoundException', so other exceptions won't be catched. You either have to change the ActivityNotFoundException to something more generic (e.g. 'Exception') or add another catch-clause
Solution1:
try {
startActivity(intent);
}
catch (Exception ex) {
}
Solution2:
try {
startActivity(intent);
}
catch (ActivityNotFoundException ex) {
}
catch (...<insert your particular exception type here>...) {
}
I am writing an Android application to display pdf files on the device. And I need to use the current versioncode (35498) of the Adobe Reader to display the pdf files.I have with code to display list of files on the screen. Now I need to invoke the Adobe reader (not any other pdf reader installed on the device) onclick of each document. I am not sure how I code that. I am an Android newbie. Any help will be greatly appreciated.
Thanks in Advance,
Navin
I see that you want to open Adobe specifically, but you may want to consider doing it the more Android-like way of opening a general intent and allowing the user to choose how it opens. For your reference, you'd do that with the following code:
private void openFile(File f, String mimeType)
{
Intent viewIntent = new Intent();
viewIntent.setAction(Intent.ACTION_VIEW);
viewIntent.setDataAndType(Uri.fromFile(file), mimeType);
// using the packagemanager to query is faster than trying startActivity
// and catching the activity not found exception, which causes a stack unwind.
List<ResolveInfo> resolved = getPackageManager().queryIntentActivities(viewIntent, 0);
if(resolved != null && resolved.size() > 0)
{
startActivity(viewIntent);
}
else
{
// notify the user they can't open it.
}
}
If you really need to use both Abode Reader specifically, and a specific version, you would need to query for it using PackageManager.getPackageInfo(String, int)
Try the following code
private void loadDocInReader(String doc)
throws ActivityNotFoundException, Exception {
try {
Intent intent = new Intent();
intent.setPackage("com.adobe.reader");
intent.setDataAndType(Uri.parse(doc), "application/pdf");
startActivity(intent);
} catch (ActivityNotFoundException activityNotFoundException) {
activityNotFoundException.printStackTrace();
throw activityNotFoundException;
} catch (Exception otherException) {
otherException.printStackTrace();
throw otherException;
}
}
If you are in "online mode", here is an interesting alternate solution using Google docs.
String myPDFURL = "http://{link of your pdf file}";
String link;
try {
link = "http://docs.google.com/viewer?url="
+ URLEncoder.encode(myPDFURL, "UTF-8")
+ "&embedded=true";
} catch (Exception e) {
e.printStackTrace();
}
Uri uri = Uri.parse(link);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
This works, setDataAndType method cannot seem to correctly recognize the PDF type if used via URL.
private static Intent newPDFLinkIntent(String url) {
Uri pdfURL = Uri.parse(url);
Intent pdfDownloadIntent = new Intent(Intent.ACTION_VIEW, pdfURL);
pdfDownloadIntent.setType("application/pdf");
pdfDownloadIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
return pdfDownloadIntent ;
}
Unfortunately, the PDF applications I'm using don't anticipate downloading and caching the online content (some will have memory leak error, some will reject link downloading), so you'll eventually end up invoking an intent that downloads the PDF first, before opening the downloaded content via the notification link. I eventually used the solution below:
private static Intent newPDFLinkIntent(String url) {
Intent pdfDownloadIntent = null;
try {
pdfDownloadIntent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
pdfDownloadIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
} catch (URISyntaxException e) {
Log.e("PDF Link Tag", e.getMessage());
}
return pdfDownloadIntent;
}