如何从onActivityResult获取任何文件路径,名称,扩展名?

时间:2015-03-03 04:45:13

标签: android onactivityresult

我的代码是:

1

File file = new File(Environment.getExternalStorageDirectory(),"myFolder");

            Log.d("path", file.toString());

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setDataAndType(Uri.fromFile(file), "*/*");
        startActivityForResult(intent,0);

2

 @Override
    protected void onActivityResult(int requestCode, 
    int resultCode, Intent FileReturnedIntent) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, FileReturnedIntent);

如何获取文件路径,名称,扩展名? (假设文件是​​doc,pdf,csv格式)

6 个答案:

答案 0 :(得分:4)

它返回文件扩展名,如pdf,doc .. etc

 public static String getMimeType(Context context, Uri uri) {
    String extension;

    if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
         final MimeTypeMap mime = MimeTypeMap.getSingleton();
        extension = mime.getExtensionFromMimeType(context.getContentResolver().getType(uri));
    } else {
        extension = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(uri.getPath())).toString());

    }

    return extension;
}

它返回获取文件名的真实路径。一位朋友使用这种方式,这非常有用。

Get filename and path from URI from mediastore

public String getRealPathFromURI(Context context, Uri contentUri) {
  Cursor cursor = null;
  try { 
    String[] proj = { MediaStore.Images.Media.DATA };
    cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
  } finally {
    if (cursor != null) {
      cursor.close();
    }
  }
}

您从此

获取文件名
public String getFileName(Uri uri) {
  String result = null;
  if (uri.getScheme().equals("content")) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    try {
      if (cursor != null && cursor.moveToFirst()) {
        result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
      }
    } finally {
      cursor.close();
    }
  }
  if (result == null) {
    result = uri.getPath();
    int cut = result.lastIndexOf('/');
    if (cut != -1) {
      result = result.substring(cut + 1);
    }
  }
  return result;
}

答案 1 :(得分:2)

我迟到了,但这可以帮助其他人解决问题。

hashmap

答案 2 :(得分:1)

你可以像这样使用

@Override
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) 
            {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();
           }
}

答案 3 :(得分:0)

如果 FileReturnedIntent 包含File作为附加内容,请尝试以下操作

Bundle data1=FileReturnedIntent.getExtras();
File f=(File)data1.get(key);
String path=f.getAbsolutePath();
System.out.println("The file path along with extension is : "+path);

希望这会对你有所帮助。

答案 4 :(得分:0)

科特林的新星日:

val myFile = File(data?.data!!.path)

答案 5 :(得分:-4)

您可以使用

将String Extra置于意图中
intent.putExtra("path", your_path);

并通过

获取onActivityResult
FileReturnedIntent.getStringExtra("path");