I want to open pdf in Android
I am currently using code
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + path.get((int) v.getTag()));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
mContext.startActivity(intent);
But the file is not opening currently.
Regards
Related
I have downloaded a PDF file into my application in the path getFilesDir().getPath()
When I try to open the same file using below code :
String path = getBaseContext().getFilesDir().getPath()+"/myfile.pdf";
File file = new File(path);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
I am getting The target file doesnot exist error from pdf reader.
When I browse my device for myfile.pdf path, It's located in "/sdcard/Android/data/com.***.***/files/data/com.***.***/files/myfile.pdf" .
If I hardcode the above path then also I am getting the same error.
Please let me know how to read file from that path.
use: Environment.getExternalStorageDirectory().getAbsolutePath()
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/myfile.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Open PDF in Android
I would like to open a PDF file with Adobe Reader from my android application.
I've a PDF file in /mnt/sdcard called test.pdf, and I'm using the next code:
file = new File ("/mnt/sdcard/test.pdf");
if (file.exists())
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
intent.setType("application/pdf");
intent.setPackage("com.adobe.reader");
startActivity(intent);
}
Adobe Reader is opened when this code is executed, but the file is not opened. The file is valid.
What is the problem?
try this.
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
try this code
File file=new File("/mnt/sdcard/test.pdf");
if(file.exists())
{
Uri path=Uri.fromFile(file);
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
try
{
startActivity(intent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(TestActivity.this, "No software for PDF", Toast.LENGTH_SHORT).show();
}
}
i new to android application developemnt. I am trying to open the pdf file stored in sd card.I used this code snippet but this opens the viewer in the device but not the file given in the path. Any help appreciated.
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("Environment.getExternalStorageDirectory().getPath()+/sample.pdf"));
intent.setType("application/pdf");
intent.setPackage("com.adobe.reader"); selects the adobe reader directly
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(intent,0);
if (activities.size() >= 0)
{
startActivity(intent);
} else {
Toast.makeText(this, "No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
you can read pdf file from sdcard by giving the path and try the following.
File pdfFile = new File(path);
if(pdfFile.exists())
{
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try
{
startActivity(pdfIntent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(uractivity.this, resource.getString(R.string.noapplicationpdf), Toast.LENGTH_LONG).show();
}
}
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);
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();
}
}
I am trying to create program that simply opens file on sdcard. I tried opening mp3, mp4, and apk - the code bellow always crashes unexpectedly.
String _path = "file:///sdcard/1.apk";
Uri outputFileUri = Uri.parse(_path);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Market links also crashes. But when I set _path="http://google.com" - browser opens normally. How can I make this code work?
If you're trying to install the apk, you need to use the following:
String fileName = Environment.getExternalStorageDirectory() + "/1.apk";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
startActivity(intent);
If you're trying to launch the app (after installing), then:
Intent intent = new Intent();
intent.setClassName("com.pkg.addr", "com.pkg.addr.MainActivity");
startActivity(intent);
If you're trying to launch a player to play the mp3 file:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri data = Uri.parse("file:///sdcard/1.mp3");
intent.setDataAndType(data,"audio/mp3");
startActivity(intent);
Hope that helps.