How to get the path of android installed application
Hi I am new to Android and I want to know that how can we get the path of the installed application in android, So that we can open the file in our application by using that installed application.
for example: I want to open the pdf file or doc fil开发者_JAVA技巧e in my application but i require the path of that application which can open this file...
Please Help me Thanks in advance...
This is not how things work in Android. To call another installed application, you'll have to do it with an Intent. Android will then select the application(s) best suited for what you're trying to do, and use them to open the file you need. In your example, the code would be something like this :
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
startActivity(Intent.createChooser(intent, "Open PDF"));
Look at the Intent javadoc to have more explanations about that (the createChooser
part is here to allow the user to choose between various apps if more than one can open the designated file)
I used the Following Code to do this
private void openFile(File f)
{
// Create an Intent
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
// Category where the App should be searched
// String category = new String("android.intent.category.DEFAULT");
// Setting up the data and the type for the intent
String type = getMIMEType(f);
/*Uri startDir = Uri.fromFile(f);
intent.setAction(Intent.ACTION_PICK);
intent.setDataAndType(startDir, "vnd.android.cursor.dir/*");*/
intent.setDataAndType(Uri.fromFile(f), type);
// will start the activtiy found by android or show a dialog to select one
startActivity(intent);//
/**intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK+Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
String theMIMEcategory = getMIMEcategory(type);
intent.setDataAndType(Uri.fromFile(f),theMIMEcategory);
try {
startActivity(Intent.createChooser(intent,"Choose Applicaton"));
} catch (Exception e) {
//show error
}
*/
}
/**
* Returns the MIME type for the given file.
*
* @param f the file for which you want to determine the MIME type
* @return the detected MIME type
*/
private String getMIMEType(File f)
{
String end = f.getName().substring(f.getName().lastIndexOf(".")+1, f.getName().length()).toLowerCase();
String type = "";
if(end.equals("mp3") || end.equals("aac") || end.equals("aac") || end.equals("amr") || end.equals("mpeg") || end.equals("mp4")) type = "audio/*";
else if(end.equals("jpg") || end.equals("gif") || end.equals("png") || end.equals("jpeg")) type = "image/*";
else if(end.equals("pdf")) type = "application/pdf";
else if(end.equals("xls")) type = "application/vnd.ms-excel";
else if(end.equals("doc")) type = "application/msword";
else if(end.equals("zip")) type="application/zip";
else {type="*/*" ;}
//type += "/*";
return type;
}
public static String getMIMEcategory(String aMIMEtype) {
if (aMIMEtype!=null) {
aMIMEtype = aMIMEtype.substring(0,aMIMEtype.lastIndexOf("/",aMIMEtype.length()-1))+"/*";
} else {
aMIMEtype = "*/*";
}
return aMIMEtype;
}'
精彩评论