Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setDataAndType(uri, "text/html");
File file = new File(uri.getPath());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
context.startActivity(intent);
the URI is
file:///data/user/0/com.carmate.crashreporter/files/crashResult.html
the browser is opened correctly also with the correct URI, however, it shows nothing
Sorry, but do you really need to do it? I think you better try to use a WebView or create an activity and show the content in a TextView using Html.fromHtml().
You can do something like
TextView view = findViewById(R.id.your_text_view);
view.setText(Html.fromHtml("<h1>Your html content </h1>"))
Related
The code below can open pdf file by intent and works well:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(url)), "application/pdf");
if(intent.resolveActivity(getPackageManager()) != null){
startActivity(intent);
}
but if I change Uri.fromFile(new File(url)) to Uri.parse(url), just like below, and it will fail to open pdf file. Why?
intent.setDataAndType(Uri.parse(url), "application/pdf");
I know the class type of instance that the two method return is different, but is this the key that the code above works?
By the way, the url is right and the file exists.
Uri is Type used to provide content to the ContentProvider. The uri is the path to a pdf file right? How would Uri.parse know that what you're passing it is a pdf and not just a url? This is why fromFile is needed.
I would like to open a PDF file from my android application. I've searched how to do it in internet, and it seems very easy, but it doesn't work, at least in my mobile (Sony XPeria P).
File file = ....
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.fromFile (file));
intent.setType("application/pdf");
startActivity(intent);
When this code is executed, a window is opened asking to choose an application to show the PDF. When I choose the Adobe Reader, it's opened by no document is shown.
What I'm doing wrong?
Try this, Its working for me
//Method to open the pdf file in the PDF Viewer
public void OpenPDFFile() {
File pdfFile = new File(Environment.getExternalStorageDirectory(),"PdfFile.pdf");//File path
if (pdfFile.exists()) //Checking for the file is exist or not
{
Uri path = Uri.fromFile(pdfFile);
Intent objIntent = new Intent(Intent.ACTION_VIEW);
objIntent.setDataAndType(path, "application/pdf");
objIntent.setFlags(Intent. FLAG_ACTIVITY_CLEAR_TOP);
startActivity(objIntent);//Staring the pdf viewer
}
}
The setType("application/pdf") function removes all the previous data.
If the targetSdkVersion >= 24 we need to use file provider to get the Uri. See this post for details: https://stackoverflow.com/a/38858040/8192914
So, the final code would look something like this:
Uri pathUri = FileProvider.getUriForFile(getBaseContext(), context.getApplicationContext().getPackageName() + ".provider", finalFile);
Intent pdfViewerIntent = new Intent(Intent.ACTION_VIEW);
pdfViewerIntent.setDataAndType(pathUri, "application/pdf");
pdfViewerIntent.setFlags(Intent. FLAG_ACTIVITY_CLEAR_TOP);
pdfViewerIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(pdfViewerIntent);
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'm trying to open a local html file using the default browser using the following code:
Uri uri = Uri.fromFile(file);
Intent browserIntent = new Intent(Intent.ACTION_VIEW).setData(uri);
startActivity(browserIntent);
but i'm getting the following exception:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///sdcard/SolveDroid/solution.html }
i'm confused - should i create an activity to hande the web beowser? isn't it supposed to just call its activity?
please advise :)
UPDATE:
the same code works if i pass a URL like so:
Uri uri = Uri.parse("http://www.metalist.co.il");
Uri uri = Uri.fromFile(file);
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
browserIntent.setData(uri);
startActivity(browserIntent);
I found an answer for this problem... just needed to add
browserIntent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
i used it with the "file://" uri by using Uri.fromfile(file) and it works (Android v.2.2.1)
try this
Intent in = new Intent(Intent.ACTION_VIEW);
File f=new File("/sdcard/html.html");
in.setDataAndType(Uri.fromFile(f), "text/html");
startActivity(in);
I have a png and a jpg image on disk. I'd like to use any built-in intent (if any are available) to view the image (just a single one at a time). Something like this:
Intent intent = new Intent();
intent.setImagePath("/blah/myimage.jpg");
startActivity(intent);
is there a built-in intent in android to do this, or do I have to write my own image viewing-activity? It would be cool if there was one that had panning/zooming as found in WebView,
Thanks
This should work.
Uri uri = Uri.fromFile("/blah/myimage.jpg");
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/jpg");
startActivity(intent);