如何将content:// uri转换为文件路径?

时间:2018-06-21 05:33:43

标签: android uri google-drive-android-api

我正在尝试从驱动器中选择一个pdf文件,并将其上传到Firebase存储。当我从外部存储中选择一个pdf文件时,它工作正常,但当我尝试从驱动器中选择文件时,它不起作用。这是由于不支持从驱动器获取的content:// uri。有没有办法将此uri转换为文件路径。如果没有,还有什么其他相关的解决方案?

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("pdf/*");

intent.putExtra(Intent.ACTION_OPEN_DOCUMENT, true);startActivityForResult(intent, PICKFILE_REQUEST_CODE);@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICKFILE_REQUEST_CODE && resultCode == RESULT_OK && data != null && data.getData() != null) {uri = data.getData();

String stringUri = uri.toString();
String fileFormat = "pdf";
Log.d("Pdf Uri",stringUri);final DatabaseReference dataRef = FirebaseDatabase.getInstance().getReference();

final StorageReference filepath = storageRefr.child(root_child).child(uri.getLastPathSegment());
UploadTask uploadTask = filepath.putFile(uri);


uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}

// Continue with the task to get the download URL
return filepath.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
//do something
}

这是我从驱动器content://com.google.android.apps.docs.storage.9HYx5b%0A

中获得的uri。

所需的uri是:

file:///storage/emulated/0/Download/file.pdf

0 个答案:

没有答案