我正在尝试使用Firebase存储服务创建一个应用程序。我有一个功能,可以从设备本地存储中保存图像,使用图片的URI将其保存到Firebase。尝试将图像上传到Firebase服务器时,出现“对象在该位置不存在”的问题,可能是我的URI出现问题。
我正在添加整个活动,以便您可以查看发生的情况-
UserControl
我错误地实现了URI吗?
编辑-
尝试通过注释解决此问题,但仍然遇到相同的错误-
Frame.Source
答案 0 :(得分:1)
对我来说,此代码可以上传。我是在评论部分的https://www.youtube.com/watch?v=lPfQN-Sfnjw&list=PLrnPJCHvNZuBf5KH4XXOthtgo6E4Epjl8&index=4处找到的。还将Firebase存储中的身份验证设置为true
private void uploadFile(){
if(mImageUri!=null){
storageReference.putFile(mImageUri).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();
}
return storageReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>()
{
@Override
public void onComplete(@NonNull Task<Uri> task)
{
if (task.isSuccessful())
{
Uri downloadUri = task.getResult();
Log.e("TAG", "then: " + downloadUri.toString());
Upload upload = new Upload(mEditTextFileName.getText().toString().trim(),
downloadUri.toString());
databaseReference.push().setValue(upload);
Toast.makeText(MainActivity.this, "Uploaded", Toast.LENGTH_SHORT).show();
} else
{
Toast.makeText(MainActivity.this, "upload failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
else{
Toast.makeText(this, "Please upload image!", Toast.LENGTH_SHORT).show();
}
}
答案 1 :(得分:0)
尝试使用此代码
StorageReference imageReference = storageReference.child("uploads");
UploadTask uploadTask = imageReference.putFile(imageuri);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(verify.this, "Upload failed!", Toast.LENGTH_SHORT).show();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
if (taskSnapshot.getMetadata() != null) {
if (taskSnapshot.getMetadata().getReference() != null) {
Task<Uri> result = taskSnapshot.getStorage().getDownloadUrl();
result.addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
答案 2 :(得分:0)
替换以下代码:
StorageReference fileRefrence = storageReference.child(System.currentTimeMillis()
+ "." + getFileExtension(imageUri));
与此:
StorageReference fileRefrence = storageReference.child(imageUri.getLastPathSegment());
答案 3 :(得分:0)
这是我用于项目及其工作的代码... 还要确保
StorageReference storageReference;
storageReference = FirebaseStorage.getInstance()。getReference();
StorageReference refStorage = storageReference.child(“ images /” + uri.getLastPathSegment()); UploadTask uploadTask = refStorage.putFile(uri);
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw new Exception("task is not succeeded");
} return refStorage.getDownloadUrl();
}//end of throws Exception method
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
downUri = task.getResult();
} else {
// Handle failures
// ...
Log.d("<<<<<<<<", "<<<<<<<<<<<<<<<<<<<< not uplooaded ");
}
}//end of onComplete()for getting download uri
});