Android:打开fileBrowser以获取文件路径

我一直试图找到一种方法让我的应用程序打开一个文件浏览器,用户可以选择一个video文件并让我的应用程序将该路径存储到一个字符串中以便以后使用。 有没有我可以使用或运作的意图? 有什么建议么?

你可以通过意图选择器来做到这一点。 试试这个代码我认为这个解决方案可以解决你的目

Intent intent = new Intent(); intent.addCategory(Intent.CATEGORY_OPENABLE); // Set your required file type intent.setType("*/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "DEMO"),1001); 

然后是onActivityResult方法。

 public void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); // super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1001) { Uri currFileURI = data.getData(); String path=currFileURI.getPath(); }} 

这是我使用过以前的一个项目的代码。

 Intent intent = new Intent(); intent.setType("video/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent,"Select Video "), 33); public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { if (requestCode == 33) { Uri selectedMediaUri = data.getData(); filemanagerstring = selectedMediaUri.getPath(); selectedMediaPath = getPath(selectedMediaUri); if (!selectedMediaPath.equals("")) { filePath = selectedMediaPath; } else if (!filemanagerstring.equals("")) { filePath = filemanagerstring; } int lastIndex = filePath.lastIndexOf("/"); fileName = filePath.substring(lastIndex+1); //filepath is your file's path } } } public String getPath(Uri uri) { String[] projection = { MediaStore.MediaColumns.DATA }; Cursor cursor = getContentResolver().query(uri, projection, null, null, null); if (cursor != null) { // HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL // THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA int column_index = cursor .getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } else return ""; } 

使用下面的代码。

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

并在活动结果上获得文件路径。

  @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub switch(requestCode){ case PICKFILE_RESULT_CODE: if(resultCode==RESULT_OK){ String FilePath = data.getData().getPath(); textFile.setText(FilePath); } break; } }