currently, I'm trying to open my pdf files with a chooser. The problem I'm currently having is that the file tries to open before the chooser is visible. Only when I go back from the opened file I can see the chooser and choose my favorite application. I tried to modify my code according to several StackOverflow suggestions on how to use the chooser but even tho I tried them they won't work.
Here is my code to open the pdf:
case "PDF":
Intent pdfIntent = new Intent();
pdfIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
pdfIntent.setAction(android.content.Intent.ACTION_VIEW);
Uri contentPDFUri = FileProvider.getUriForFile(context,
"com.ndlp.socialstudy.provider",
my_clicked_file);
pdfIntent.setDataAndType(contentPDFUri,"application/pdf");
Intent intentpdfChooser = pdfIntent.createChooser(pdfIntent, "Open With");
try {
context.startActivity(intentpdfChooser);
} catch (ActivityNotFoundException e) {
Toast.makeText(context, "Please install a PDF app to view your file!", Toast.LENGTH_LONG).show();
// Instruct the user to install a PDF reader here, or something
}
Intent myIntent = new Intent(Intent.ACTION_VIEW);
myIntent.setData(Uri.fromFile(file));
Intent j = Intent.createChooser(myIntent, "Choose an application to open with:");
startActivity(j);
Instead of calling
Intent intentpdfChooser = pdfIntent.createChooser(pdfIntent, "Open With");
try
Intent intentpdfChooser = Intent.createChooser(pdfIntent, "Open With");
and let the Android system figure out what are the applicaitons which can handle your pdfIntent
In one of my apps I'm using:
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(uri, "application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, "Open PDF using");
try {
mContext.startActivity(intent);
} catch (ActivityNotFoundException e) {
// Instruct the user to install a PDF reader here, or something
Toast.makeText(mContext, "No Applications found to open pdf", Toast.LENGTH_SHORT).show();
}
and it's working for me.
Related
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();
}
I searched a lot to find a solution , but still can't find any
I am trying to open a PDF file in my android application
Here is my code to do this :
try
{
File file = new File(Environment.getExternalStorageDirectory()+"/pdf/Read.pdf");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
Log.d("CheckingURI", uri.toString());
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
}
catch (Exception e)
{
Log.d("OpenPDFError", e.getMessage());
}
The Tomcat Log implies that there is no activity to handle the intent
09-19 19:55:02.938: D/CheckingURI(30483): file:///mnt/sdcard/pdf/Read.pdf
09-19 19:55:02.948: D/OpenPDFError(30483): No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/pdf/Read.pdf typ=application/pdf }
Is there any other way to do this or can i open the PDF file with another default PDF viewer? If yes, how?
Update :
the main problem was that i did not install a PDF viewer application on my emulator ...
Try this instead:
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/pdf/Read.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
CAVEAT: The above method only work if you have a pre-installed PDF Viewer. If not, you need to install a PDF Viewer.
You just have to popup some sort of error msg to the user if no activity was found to launch the PDF (In the error dialog you can let the user know he needs some PDF app and send him to the Play Store), though i'd suggest using this piece of code instead of the genral exception catcher you're using:
try {
/* your code */
...
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
or you can use some PDF library, like this one.
I want to pick a file (other than image, video or audio), like pdf, ppt, docx, txt etc.,,
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select File"),Constant.SELECT_FILE);
Above method is not working.
When I do the action Dialog box appears With message "No Application can perform this action"
Because there is no any application installed on device which is handle your intent with action Intent.ACTION_GET_CONTENT. So if your device running on Android 4.4 then try with Intent.ACTION_OPEN_DOCUMENT or Implement your own File Browser Activity.
Look at https://github.com/vaal12/AndroidFileBrowser and aFileChooser
Try this:
The problem is that there is no app installed to handle opening the PDF. You should use the Intent Chooser, like so:
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, "Open File");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
// Instruct the user to install a PDF reader here, or something
}
I have written a simple app that lists a number of pdf files and when the user clicks on one of them they open up in a pdf viewer (using adobe here).
Heres the code for opening the pdf file :
Uri path = Uri.fromFile(open[filePosition]);
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
} catch (ActivityNotFoundException e) {
Toast.makeText(getApplicationContext(), "PDF Reader application is not installed in your device", Toast.LENGTH_LONG).show();
}
Now after viewing the file in the pdf viewer , when the user clicks back it opens up the main menu of the device.
How can i make it go back to my app so the user can open another file?
Remove
finish();
from your code and you should be good.
Remove finish(), and then it should be :
Uri path = Uri.fromFile(open[filePosition]);
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(getApplicationContext(), "PDF Reader application is not installed in your device", Toast.LENGTH_LONG).show();
}
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();
}
}