如何使用intent从文件选择器获取实际文件路径

时间:2013-11-13 12:00:06

标签: android file-upload

如何使用intent获取文件路径。我正在使用此代码来选择文件

 Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
 intent.setType("*/*"); 
// intent.setData(uri);
   intent.addCategory(Intent.CATEGORY_OPENABLE);
   try {
        startActivityForResult(
         Intent.createChooser(intent, "Select a File to Upload"),PICKFILE_RESULT_CODE);
       } catch (android.content.ActivityNotFoundException ex) 
       {// Potentially direct the user to the Market with a Dialog
   Toast.makeText(getApplicationContext(), "Please install a File Manager.", Toast.LENGTH_LONG).show();
                            }

现在我想在

中获得实际的文件路径
@Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (requestCode == PICKFILE_RESULT_CODE && resultCode == RESULT_OK
                        && data != null) {
    Uri selectedFile = data.getData();


                String[] filePathColum = {  };

                Cursor cursor = getContentResolver().query(selectedFile,
                        filePathColum, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColum[0]);

                String FilePath = cursor.getString(columnIndex);

但是当我尝试获取文件路径时,它崩溃了 我如何获得文件路径

0 个答案:

没有答案