我一直试图将图像从我的android应用程序上传到firebase,但是当调用该方法时,我的应用程序崩溃。
我可以从手机文件夹中选择图像,并在应用程序中显示/显示它,没有问题,但是当我尝试存储图像时,应用程序崩溃,并且图像未存储在Firebase存储器中。
我的应用程序已经连接到Firebase存储,并且已经添加到build:gradle implementation 'com.google.firebase:firebase-storage:16.0.4'
存储规则设置为
允许读写:如果为true;
这是我的代码:
public void openGallery() {
Intent galleryIntent = new Intent();
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, GalleryPick); //opening gallery
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GalleryPick && resultCode == RESULT_OK) {
imageUri = data.getData();
uploadedImage.setImageURI(imageUri); //displaying the picture selected
}
}
和
private String getFileExtension(Uri uri) {
ContentResolver contentResolver = getContentResolver();
MimeTypeMap mime = MimeTypeMap.getSingleton();
return mime.getExtensionFromMimeType(contentResolver.getType(uri));
}
private void uploadingImages() {
StorageReference storageReference = myStorageRef.child(System.currentTimeMillis() + "." + getFileExtension(imageUri));
storageReference.putFile(imageUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Get a URL to the uploaded content
//Uri downloadUrl = taskSnapshot.getDownloadUrl();
//Uri downloadUrl = taskSnapshot.getMetadata().getReference().getDownloadUrl();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
// ...
}
});
}
这是显示的错误:
W / StorageTask:无法将内部状态更改为:INTERNAL_STATE_CANCELED isUser:状态为:false:INTERNAL_STATE_IN_PROGRESS
E / StorageException:发生StorageException。
发生未知错误,请检查HTTP结果代码和服务器响应的内部异常。 代码:-13000 Http结果:0E / AndroidRuntime:严重例外:FirebaseStorage-Upload-1
流程:com.example.jiowhere,PID:11196 java.lang.NoSuchMethodError:没有虚拟方法getToken(Z)Lcom / google / android / gms / tasks / Task;在Lcom / google / firebase / FirebaseApp类中;或其超级类(“ com.google.firebase.FirebaseApp”的声明出现在/data/app/com.example.jiowhere-QrgbO5CmvplqJBc3zVin7w==/split_lib_dependencies_apk.apk中)com.google.firebase.storage.internal.Util.getCurrentAuthToken(com.google.firebase:firebase-storage @@ 16.0.4:148)
com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(com.google.firebase:firebase-storage @@ 16.0.4:65)
在com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(com.google.firebase:firebase-storage @@ 16.0.4:57)
位于com.google.firebase.storage.UploadTask.sendWithRetry(com.google.firebase:firebase-storage @@ 16.0.4:457)
com.google.firebase.storage.UploadTask.beginResumableUpload(com.google.firebase:firebase-storage @@ 16.0.4:257)
com.google.firebase.storage.UploadTask.run(com.google.firebase:firebase-storage @@ 16.0.4:198)
com.google.firebase.storage.StorageTask.lambda $ getRunnable $ 7(com.google.firebase:firebase-storage @@ 16.0.4:1106) com.google.firebase.storage.StorageTask $$ Lambda $ 12.run(未知来源:2)
在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:641)
在java.lang.Thread.run(Thread.java:764)
E / StorageException:发生StorageException。 发生未知错误,请检查HTTP结果代码和服务器响应的内部异常。 代码:-13000 Http结果:0