how to switch back to original activity? - android

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();
}

Related

Open the chooser for PDF before the actual PDF opens

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.

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();
}

Android - No Activity found to handle Intent { act=android.intent.action.VIEW - Trying to open a PDF File

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.

How to pick a file from gallery?

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
}

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