How to launch the stock installer app in android - android

I used to have this problem but I found a solution, so just decided to post it here just in case someone else needs it.
How to launch the native installer app to install an apk?
Many posts have the solution as below:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(path), "application/vnd.android.package-archive");
context.startActivity(intent);
This is fine except a tiny but crutial detail:
the "path" string must start with file:// otherwise you'll get an exception such as
Unable to find an activity to handle the intent .....
So make sure the path starts with file://
Cheers.

Actually, instead of using the parse(...) method, you can simply use the fromFile(...) method of the Uri class (the Uri will automatically have the form "file://").
Thus:
final File file = new File(path);
final Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
context.startActivity(intent);

Related

How can I ask the user to select the app he wants to use to open a file?

I'm trying to make my own file manager for android.
I am wondering how I could asks the user to choose the application he wants to use open a file. like when you are opening a .doc file and you have a bunch of .doc file readers. How would I be able to retrieve the list of apps that the user can use to read/open a file?
Try this code
Uri uri = Uri.parse("Your file name: //"+file.getAbsolutePath());
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
String type = "application/msword";
intent.setDataAndType(Uri.fromFile(file), type);
startActivity(intent);
You should use implicit intent for your purpose. Below is the sample code for giving options to play a video file
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");
startActivity(Intent.createChooser(intent, "Complete action using"));
I hope it will help you out. For further details you may refer android official documentation as mentioned in below link
https://developer.android.com/training/basics/intents/sending.html#AppChooser
I think you can try this
Intent in= new Intent(Intent.ACTION_MAIN);
in.setComponent(new ComponentName("com.package.address","com.package.address.MainActivity"));
startActivity(in);

How open a .torrent file in miutorrent programmatically

In my android app i have successfully downloaded .torrent files and now i want to open them in miutorrent (or another torrent downloading app) how to implement this ?? any help would be highly appreciated....
Try starting an intent with mime type as application/x-bittorrent
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(TorrentUri, "application/x-bittorrent");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);

How to create an intent to open .doc file using Polaris Viewer on Android?

I am trying to open a .doc file from my SD card using Polaris Viewer.
I keep on getting a message saying "This document cannot be opened".
weird thing is that I CAN open it from elsewhere.
I have ES File Explorer on my phone and I can open through there. It does it via Polaris Viewer so the file is obviously okay.
The only thing I can think of is that I have a problem with my intent.
Is there any way to see exactly what intent ES File Explorer sent?
This is my code (textOpenUri is a full path name of the file to be opened):
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(textOpenUri, "application/msword");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
I did a small test (just to make sure it is not defaulting to some other app) using:
List<ResolveInfo> list = getActivity().getPackageManager().queryIntentActivities(intent,0);
and I get back Polaris as the only app that can deal with the intent.
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/msword");
this.startActivity(intent);
startActivity(intent);

How to open the My Files folder in Android Programatically (using Intent)?

I am using the below code which opens up the Gallery, Music Player, Dropbox and Contacts, i want the My Files folder to get open programatically, please let me know if there are any specific intent parameters i need to pass to get the File Manager open.
if it is not possible through intent then please give me a snippet or an hint to open the My Files folder programatically.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
Intent i = Intent.createChooser(intent, "View Default File Manager");
startActivityForResult(i, CHOOSE_FILE_REQUESTCODE);
Thanks.
You can use this code to file the files.
int PICKFILE_RESULT_CODE=1;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent,PICKFILE_RESULT_CODE);
this will help you to browse the files from your storage.
Its best that you include a library in your project which handles this scenario.
This worked for me:
This library shows the list of third-party apps. It also has its own file browser for selecting files.
Bad thing is, most Android distributions may or may not ship with a file manager, and even so, may be not with the one which handles CHOOSE_FILE_REQUESTCODE.
So, you are left to create your own file picker activity. Luckily there are many ready made ones available:
http://code.google.com/p/android-filechooser/
https://developers.inkfilepicker.com/docs/android/
If you want to open samsung My Files application try this below code.
Intent intent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
intent.putExtra("CONTENT_TYPE", "*/*");
startActivityForResult(intent, CHOOSE_FILE_REQUESTCODE);
You have to specifically mention the package name of the explorer application. Please find the example below to open a specific folder in ES Explorer.
public void openfolderInexplorer(String path){
Intent intent = this.getPackageManager().getLaunchIntentForPackage("com.estrongs.android.pop");
if (intent != null) {
// If the application is avilable
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.parse(path);
intent.setDataAndType(uri, "resource/folder");
this.startActivity(intent);
} else {
// Play store to install app
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("market://details?id=" +
"com.estrongs.android.pop"));
this.startActivity(intent);
}
try this below code. if any file manager available , then it will pop up in a form of menu to choose appropriate for the user.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, CHOOSE_FILE_REQUESTCODE);

How can I send a kml file to Google Earth, like MyTracks (open source) do?

I don't know if you have seen the amazing Mytrack update, but it allow to send a kml file to Google Earth app and display it inside the Google app (if installed, of course).
The source code is there: http://code.google.com/p/mytracks/source/browse/
but I cannot find the way to achieve such a thing.
I think I found something here: http://code.google.com/r/jshih-mytracks3/source/browse/MyTracks/src/com/google/android/apps/mytracks/io/file/SaveActivity.java?spec=svn5178eb75934b7f0c4c23ec26b7d79a0787de18b8&r=5178eb75934b7f0c4c23ec26b7d79a0787de18b8
else if (playTrack) {
Intent intent = new Intent()
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra(GOOGLE_EARTH_TOUR_FEATURE_ID, KmlTrackWriter.TOUR_FEATURE_ID)
.setClassName(GOOGLE_EARTH_PACKAGE, GOOGLE_EARTH_CLASS)
.setDataAndType(Uri.fromFile(new File(savedPath)), GOOGLE_EARTH_KML_MIME_TYPE);
startActivity(intent);
The hardcoded way gives this code:
Intent intent = new Intent()
.addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra("com.google.earth.EXTRA.tour_feature_id","tour")
.setClassName("com.google.earth", "com.google.earth.EarthActivity")
.setDataAndType(Uri.fromFile(new File("/sdcard/test.kml")),
"application/vnd.google-earth.kml+xml");
startActivity(intent);
But the above code simply displays the path with the same result than this code:
Intent mapIntent = new Intent(Intent.ACTION_VIEW);
Uri uri1 = Uri.parse("file:///sdcard/test.kml");
mapIntent.setData(uri1);
startActivity(Intent.createChooser(mapIntent, "Sample"));
My objective is to get the same result, with a "play" button.
You need to specify the URI to your KML file AND the KML MIME type, as follows.
File file = new File(Environment.getExternalStorageDirectory(), "sample_tour.kml");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.google-earth.kml+xml");
intent.putExtra("com.google.earth.EXTRA.tour_feature_id", "my_track");
startActivity(intent);
This is presently undocumented, but we're looking to fix this.
Be sure to use Intent::setDataAndType and not Intent::setData and Intent::setType separately (they each override the other).
"my_track" is a reference to your placemark id. The intent extra automatically starts the tour.
<Placemark id="my_track">
<gx:Track>
...
</gx:Track>
</Placemark>
Is it possible to use a link instead of a kml from disk?
Something like this:
intent.setDataAndType(Uri.parse("http://sites.cyclingthealps.com/other/downloads/doc.kml"), "application/vnd.google-earth.kml+xml");
Thanks
If you are targeting Android N, you are not allowed to send a file:// intent anymore. Use the instructions at android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData() , and add the Intent.FLAG_GRANT_READ_URI_PERMISSION flag.
File KML = new File("/sdcard/doc.kml");
Intent i = getPackageManager().getLaunchIntentForPackage("com.google.earth");
i.setDataAndType(Uri.fromFile(KML), "xml");
startActivity(i);
source: http://enladodelbien.blogspot.com/2015/06/kmlkmz-to-google-earth.html

Categories

Resources