簡體   English   中英

如何獲取android安裝應用程序的路徑

[英]How to get the path of android installed application

嗨,我是Android新手,我想知道如何在android中獲取已安裝應用程序的路徑,以便我們可以使用已安裝的應用程序在我們的應用程序中打開該文件。

例如:我想在我的應用程序中打開pdf文件或doc文件,但我需要該應用程序的路徑才能打開此文件...

請幫助我提前謝謝...

這不是Android中的工作方式。 要調用另一個已安裝的應用程序,您必須使用Intent 然后,Android將選擇最適合您嘗試執行的應用程序,並使用它們打開您需要的文件。 在您的示例中,代碼將是這樣的:

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

查看Intent javadoc以獲得更多解釋( createChooser部分允許用戶在多個應用程序之間進行選擇,如果多個應用程序可以打開指定文件)

我使用以下代碼來執行此操作

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;
    }'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM