This few lines do the magic:
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("Url to pdf or other")));
} catch (ActivityNotFoundException e) {
//do something here
}
質問
I tried to open PDF files that have external links, but after Adobe reader is opening, it gives me that "The document path is not valid"
This is the code I wrote:
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
File file = new File("http://www.kb.nl/sites/default/files/docs/pdf_guidelines.pdf");
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(MainActivity.this,
getString(R.string.app_name),
Toast.LENGTH_SHORT).show();
}
}
});
How can I open external PDF files in my emulator, my aim is to open pdf files which are inside my google drive.
Thanks in advance.
解決 2
This few lines do the magic:
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("Url to pdf or other")));
} catch (ActivityNotFoundException e) {
//do something here
}
他のヒント
File file = new File("http://www.kb.nl/sites/default/files/docs/pdf_guidelines.pdf");
http://www.kb.nl/sites/default/files/docs/pdf_guidelines.pdf is not a path to a File
. It is a URL to a Web site. You cannot wrap a Web URL in a File
object and expect it to work.
You can try using Uri.parse("http://www.kb.nl/sites/default/files/docs/pdf_guidelines.pdf")
to get your Uri
and seeing if Adobe Reader will support that, but I doubt it.
More likely, you need to download the PDF yourself, then use that File
in the Intent
.