如何获取android安装应用程序的路径

时间:2010-12-29 07:55:48

标签: android android-intent

您好我是Android新手,我想知道如何在Android中获取已安装应用程序的路径,以便我们可以使用已安装的应用程序在我们的应用程序中打开该文件。

例如: 我想在我的应用程序中打开pdf文件或doc文件,但我需要该应用程序的路径才能打开此文件......

请帮帮我 提前谢谢......

2 个答案:

答案 0 :(得分:2)

这不是Android中的工作方式。要调用另一个已安装的应用程序,您必须使用Intent来执行此操作。然后,Android将选择最适合您尝试执行的应用程序,并使用它们打开您需要的文件。在您的示例中,代码将是这样的:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
startActivity(Intent.createChooser(intent, "Open PDF"));

查看Intent javadoc以获得更多解释(createChooser部分允许用户在各种应用程序之间进行选择,如果多个应用程序可以打开指定文件)

答案 1 :(得分:2)

我使用以下代码执行此操作

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;
    }'
相关问题