2010-12-29 8 views
1

안녕하세요, 안드로이드에 익숙하지 않은 나는 어떻게 우리가 안드로이드에 설치된 응용 프로그램의 경로를 얻을 수 있는지 알고 싶습니다, 그래서 우리는 우리의 응용 프로그램에서 그 파일을 열 수 있습니다 설치된 응용 프로그램.안드로이드 설치된 응용 프로그램의 경로를 얻는 방법

예를 들어

: 내 응용 프로그램에서 PDF 파일이나 문서 파일을 열려면하지만 난이 파일을 열 수있는 응용 프로그램의 경로를 필요로 ...

사전에 나에게 감사합니다 제발 도와주세요 ..

+0

을하려면 다음 코드를 사용했다. – ykatchou

+0

좋아, 나는 의도가이 모든 것을 만들 수 있다는 것을 몰랐다. – Mak

답변

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

Android에서 작동하는 방식이 아닙니다. 다른 설치된 응용 프로그램을 호출하려면 Intent을 사용해야합니다. 그러면 Android에서 수행하려는 작업에 가장 적합한 응용 프로그램을 선택하고 필요한 파일을 열 때 사용합니다. 당신의 예에서 코드는 다음과 같이 될 것이다 : 그에 대한 자세한 설명을 가지고있는 Intent의 javadoc에서

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

봐합니다 (createChooser 부분은 더 이상 할 수있는 경우에 사용자가 다양한 애플리케이션을 선택할 수 있도록 여기에있다 지정된 파일)

+0

고마워 ... 나에게 도움이된다. – Mak

관련 문제