我自学了FireBase存储,我决定制作一个简单的应用程序,让用户选择一个图像,并将其上传到Firebase。 但是,只要我选择要上传的图片,我的应用就会崩溃并说我的URI为空,即使我从我选择的图片中获取
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
return;
}
if (requestCode == 1) {
final Bundle extras = data.getExtras();
if (extras != null) {
//Get image
path = extras.getParcelable("data");
}
StorageReference storageRef = FirebaseStorage.getInstance().getReference();
storageRef.putFile(path)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Get a URL to the uploaded content
@SuppressWarnings("VisibleForTests") Uri downloadUrl = taskSnapshot.getDownloadUrl();
Log.e("myTag", "done, " +downloadUrl);
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Log.e("myTag", exception.toString());
}
});
}
}
我一直试图解决这个错误超过五个小时,尽管经历了十亿次的文档,我仍然感到困惑。任何帮助表示赞赏!
答案 0 :(得分:0)
选定的Uri
将作为Intent
的{{3}}返回,而不是额外的@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
return;
}
if (requestCode == 1) {
path = data.getData(); // assumes path is declared as a Uri
StorageReference storageRef = FirebaseStorage.getInstance().getReference("images");
storageRef.putFile(path)...
。试试这个改变:
zips/2