我正在尝试从Firebase存储到我的应用程序打开pdf文件。 这是我获取文件网址的代码。
StorageReference fileref=mStorage.getReferenceFromUrl(selectedItem.getFileUrl());
String fileref_string=fileref.toString();
Uri file_uri=Uri.parse(fileref_string);
但它显示此错误:
java.io.FileNotFoundException:没有内容提供者:gs://funn-30a97.appspot.com/uploads/1531599733740.pdf
如何处理?
用于上传文件的代码: mFileUri是设备中文件的网址。
if (mFileUri != null) {
StorageReference fileReference = mStorageRef.child(System.currentTimeMillis() + "." + getFileExtension(mFileUri));
fileReference.putFile(mFileUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mProgressBar.setProgress(0);
}
}, 5000);
Toast.makeText(upload.this, "Upload Successful", Toast.LENGTH_LONG).show();
Uploading uploading = new Uploading(mEditTextFileName.getText().toString().trim(), taskSnapshot.getDownloadUrl().toString());
String uploadId = mDatabaseRef.push().getKey();
mDatabaseRef.child(uploadId).setValue(uploading);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(upload.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
mProgressBar.setProgress((int) progress);
}
});
上传课程:
public class Uploading {
private String mname;
private String mfileUrl;
private String mKey;
public Uploading() {
}
public Uploading(String name, String fileUrl) {
if (name.trim().equals("")) {
name = "no name";
}
mname = name;
mfileUrl = fileUrl;
}
public String getName() {
return mname;
}
public void setName(String Name) {
mname = Name;
}
public String getFileUrl() {
return mfileUrl;
}
public void setfileUrl(String url) {
mfileUrl = url;
}
@Exclude
public String getKey() {
return mKey;
}
@Exclude
public void setKey(String key) {
mKey = key;
}
}
答案 0 :(得分:0)
这不是正确的方法。您不应该在toString()
对象上调用StorageReference
。
尝试一下:
if (fileref.getPath().length() != 0) {
fileref.getDownloadUrl()
.addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
// Now you have the Uri of your file
}
});
}
祝你好运!