opening local html file with Android Browser - android

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

Related

why does Uri.fromFile(new File(url)) work and Uri.parse(url) not in open pdf by intent?

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.

Open PDF in Android

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

Facebook Link doesnt work android

Hello I just want to call Facebook app with the link below (on my android project):
String url_facebook_prixo = "https://www.facebook.com/pages/Prixo/468580313168290";
I tried this :
Uri uri = Uri.parse("facebook://facebook.com/page{468580313168290}");
Intent viewIntent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(viewIntent);
I tried with others link but its only displaying my wall..
Someone?
You should use the id to open the page.
Intent facebookIntent = new Intent(
Intent.ACTION_VIEW,
Uri.parse("fb://profile/468580313168290"));
startActivity(facebookIntent);
I would advice you to encapsulate this in a try catch. Inside the catch open a normal browser intent

Mediaplayer exception

Intent intent1 = new Intent(
android.provider.MediaStore.INTENT_ACTION_MUSIC_PLAYER)
.setData(selectedImageUri);
i want to play a media file using android default media player but its not working in devices showing
ActivityNotFoundException
.can any one help me to correct it.i am stuck here
String extension = MimeTypeMap
.getFileExtensionFromUrl(selectedImagePath);
String mimeType = MimeTypeMap.getSingleton()
.getMimeTypeFromExtension(extension);
Intent mediaIntent = new Intent(Intent.ACTION_VIEW);
mediaIntent.setDataAndType(Uri.parse(selectedImagePath),
mimeType);
startActivity(mediaIntent);
I used this code and i got my output.
Maybe this will help you. The following is the piece which I use and it works fine. Pass the url to your default media Player and from there it will take care of it.
Uri myUri = Uri.parse( //your url);
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(myUri, "audio/*");
startActivity(intent);

startActivity on "file://" links crashes

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.

Categories

Resources