Android文件select器

我想做一个file upload。 因此,我需要一个文件select器,但我不想自己写这个。 我发现OI文件pipe理器,我认为它适合我。 但是我怎么能强制用户安装OI文件pipe理器? 如果我不能,是否有更好的方法在我的应用程序中包含文件pipe理器? 谢谢

编辑2012年1月2日 ):

我创build了一个小型的开源Android库项目,简化了这个过程,同时也提供了一个内置的文件浏览器 (如果用户没有一个存在的话)。 这是非常简单的使用,只需要几行代码。

你可以在GitHub: aFileChooserfind它。


原版的

如果您希望用户能够select系统中的任何文件,则需要包含自己的文件pipe理器,或者build议用户下载文件pipe理器。 我相信你所能做的最好的就是像这样在Intent.createChooser()查找“可打开的”内容:

 private static final int FILE_SELECT_CODE = 0; private void showFileChooser() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("*/*"); 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(); } } 

然后你会听到onActivityResult()所选文件的Uri ,如下所示:

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case FILE_SELECT_CODE: if (resultCode == RESULT_OK) { // Get the Uri of the selected file Uri uri = data.getData(); Log.d(TAG, "File Uri: " + uri.toString()); // Get the path String path = FileUtils.getPath(this, uri); Log.d(TAG, "File Path: " + path); // Get the file instance // File file = new File(path); // Initiate the upload } break; } super.onActivityResult(requestCode, resultCode, data); } 

我的FileUtils.javagetPath()方法是:

 public static String getPath(Context context, Uri uri) throws URISyntaxException { if ("content".equalsIgnoreCase(uri.getScheme())) { String[] projection = { "_data" }; Cursor cursor = null; try { cursor = context.getContentResolver().query(uri, projection, null, null, null); int column_index = cursor.getColumnIndexOrThrow("_data"); if (cursor.moveToFirst()) { return cursor.getString(column_index); } } catch (Exception e) { // Eat it } } else if ("file".equalsIgnoreCase(uri.getScheme())) { return uri.getPath(); } return null; } 

Android的档案浏览器帮了我很多。 这很容易和有用。

为了这个目的,我使用了AndExplorer,我的解决scheme是popup一个对话框,然后在市场上redirect以安装misssing应用程序:

我的startCreation正试图调用外部文件/目录select器。 如果缺less调用show installResultMessage函数。

 private void startCreation(){ Intent intent = new Intent(); intent.setAction(Intent.ACTION_PICK); Uri startDir = Uri.fromFile(new File("/sdcard")); intent.setDataAndType(startDir, "vnd.android.cursor.dir/lysesoft.andexplorer.file"); intent.putExtra("browser_filter_extension_whitelist", "*.csv"); intent.putExtra("explorer_title", getText(R.string.andex_file_selection_title)); intent.putExtra("browser_title_background_color", getText(R.string.browser_title_background_color)); intent.putExtra("browser_title_foreground_color", getText(R.string.browser_title_foreground_color)); intent.putExtra("browser_list_background_color", getText(R.string.browser_list_background_color)); intent.putExtra("browser_list_fontscale", "120%"); intent.putExtra("browser_list_layout", "2"); try{ ApplicationInfo info = getPackageManager() .getApplicationInfo("lysesoft.andexplorer", 0 ); startActivityForResult(intent, PICK_REQUEST_CODE); } catch( PackageManager.NameNotFoundException e ){ showInstallResultMessage(R.string.error_install_andexplorer); } catch (Exception e) { Log.w(TAG, e.getMessage()); } } 

这种方法只是拿起一个对话框,如果用户想从市场上安装外部应用程序

 private void showInstallResultMessage(int msg_id) { AlertDialog dialog = new AlertDialog.Builder(this).create(); dialog.setMessage(getText(msg_id)); dialog.setButton(getText(R.string.button_ok), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }); dialog.setButton2(getText(R.string.button_install), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("market://details?id=lysesoft.andexplorer")); startActivity(intent); finish(); } }); dialog.show(); } 

我认为你不应该“强迫”用户做某事。 如果你只需要一个简单的文件select器,你可以自己写。 另一方面,如果你不想这样做(对于你的用户),你就远离了你的用户。

或者您可以使用https://code.google.com/p/android-file-chooser/