简体   繁体   English

为什么我无法在 Android Studio 中使用 ImageButton 上传图像文件?

[英]Why Couldn't I upload Image file using ImageButton in Android Studio?

I'm trying to upload an image using Image Button.我正在尝试使用图像按钮上传图像。 I don't get any error or crash, but after uploading my image, I get an alert: "Failed,".我没有收到任何错误或崩溃,但在上传我的图片后,我收到一条警报:“失败”。 and the message of e.和 e 的消息。

Can you help me please?你能帮我吗?

 private void uploadImage() {
        final ProgressDialog pd = new ProgressDialog(getContext());
        pd.setMessage("Uploading");
        pd.show();

        if(imageUri != null) {
            final StorageReference fileReference = storageReference.child(System.currentTimeMillis() + "." + getFileExtension(imageUri));
            uploadTask = fileReference.getFile(imageUri);
            uploadTask.continueWith(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 fileReference.getDownloadUrl();
                }
            }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                @Override
                public void onComplete(@NonNull Task<Uri> task) {

                    if(task.isSuccessful()) {
                        Uri downloadUri = task.getResult();
                        String mUri = downloadUri.toString();
                        reference = FirebaseDatabase.getInstance().getReference().child("users").child(fuser.getUid());
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("imageURL", mUri);
                        reference.updateChildren(map);
                        pd.dismiss();
                    } else {
                        Toast.makeText(getContext(), "Failed!", Toast.LENGTH_SHORT).show();
                        pd.dismiss();
                    }
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                    pd.dismiss();
                }
            });
        } else {
            Toast.makeText(getContext(), "No Image Selected", Toast.LENGTH_SHORT).show();
        }
    }

If your error message was:如果您的错误消息是:

Object does not exist at location Object 在该位置不存在

Then that means the object you're trying to download simply doesn't exist.那么这意味着您尝试下载的 object 根本不存在。 Here's the code you're using to build that location:这是您用于构建该位置的代码:

final StorageReference fileReference = storageReference
    .child(System.currentTimeMillis() + "." + getFileExtension(imageUri));

So, it seems you are trying to fetch a file that's supposed to exist using a name that matches this the current moment in time.因此,您似乎正在尝试使用与当前时间匹配的名称来获取应该存在的文件。 I suspect that's almost never going to work.我怀疑这几乎永远不会奏效。 Since we can't see the contents of your storage bucket, and we don't know the exact string you're passing here, there's no telling how to fix this.由于我们看不到您的存储桶的内容,而且我们不知道您在此处传递的确切字符串,因此不知道如何解决此问题。 Your code should only fetch objects whose names that exist in the bucket.您的代码应仅获取其名称存在于存储桶中的对象。

You're not calling a fileReference.put method anywhere, which means you're not uploading any data.您没有在任何地方调用fileReference.put方法,这意味着您没有上传任何数据。

So when you call fileReference.getDownloadURL() it throws an error, because there is no file at the location to return a download URL for.因此,当您调用fileReference.getDownloadURL()时,它会引发错误,因为该位置没有文件可以返回下载 URL 。

I recommend reading the documentation on uploading files , and waiting for the upload to complete before trying to get the download URL.我建议阅读有关 上传文件的文档,并等待上传完成,然后再尝试下载 URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM