2012-03-29 8 views
15

Come utilizzare l'intento per richiedere all'utente di scegliere "fine azione con" per scegliere l'app per selezionare il file (supponendo che ci sia qualche app per sfogliare il file nel dispositivo)Come utilizzare l'intento per la scelta del file browser per selezionare il file

voglio filtrare il file utilizzando un'estensione .. (es. * .sav, * .props)

Grazie in anticipo

+0

visita questa discussione, http://stackoverflow.com/questions/5537907/view -file-path-in-un-file-manager-come-android-intent Se non sbaglio, questo potrebbe aiutarti. –

risposta

39

si può usare qualcosa di simile:

.... 
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    intent.setType("file/*"); 
    startActivityForResult(intent, YOUR_RESULT_CODE); 
    .... 

B Non ho dubbi sul fatto che puoi impostare un filtro per i browser di file di terze parti. Oppure puoi provare a usare questa finestra di dialogo: http://code.google.com/p/android-file-dialog/

+2

Grazie per il collegamento .. :) è molto semplice e utile filebrowser –

+6

Avevo bisogno di usare 'intent.setType (" */* ")' invece. Nessun tipo MIME che conosca inizia con 'file /'. – Alicia

-5

// controlla qui KITKAT o nuova versione e questo risolverà anche il problema del file Samsung.

boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; 
if (isKitKat) { 
    Intent intent = new Intent(); 
    intent.setType("*/*"); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    startActivityForResult(intent,FILE_SELECT_CODE); 
} else { 
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    intent.setType("*/*"); 
    startActivityForResult(intent,FILE_SELECT_CODE); 
} 
+9

Codice in se/else sta facendo lo stesso – roplacebo

6

questo aprirà il file explorer incorporato, se disponibile, altrimenti ti chiederà di selezionare un file explorer che hai installato.

private void showFileChooser() { 

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    //intent.setType("*/*");  //all files 
    intent.setType("text/xml"); //XML file only 
    intent.addCategory(Intent.CATEGORY_OPENABLE); 

    try { 
     startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"), FILE_SELECT_CODE); 
    } catch (android.content.ActivityNotFoundException ex) { 
     // Potentially direct the user to the Market with a Dialog 
     Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show(); 
    } 
} 
0

Essa può aiutare a scegliere un file doc e u può cambiare l'azione secondo il vostro bisogno

 Intent intent; 
     if (VERSION.SDK_INT >= 19) { 
      intent = new Intent("android.intent.action.OPEN_DOCUMENT"); 
      intent.setType("*/*"); 
     } else { 
      PackageManager packageManager =getActivity().getPackageManager(); 
      intent = new Intent("android.intent.action.GET_CONTENT"); 
      intent.setType("file*//*"); 
      if (packageManager.queryIntentActivities(intent,MEDIA_TYPE_IMAGE).size() == 0) { 
       UserToast.show(getActivity(), getResources().getString(R.string.no_file_manager_present)); 
      } 
     } 
     if (getActivity().getPackageManager().resolveActivity(intent, NativeProtocol.MESSAGE_GET_ACCESS_TOKEN_REQUEST) != null) { 
      startActivityForResult(intent, UPLOAD_FILE); 
     } 
Problemi correlati