如何使用此uri上传到Firebase存储?

时间:2019-07-03 13:35:34

标签: java android firebase

如果imageUri处于这种格式,则上传成功。 content://com.android.providers.media.documents/document/image%3A12305

但是此表单失败了
/ document / image:10674

我收到了这个Toast消息。

“发生未知错误,请检查HTTP结果代码和服务器响应的内部异常。”

如果您能给我任何提示,我将不胜感激。

我尝试过的

1。从图库中获取图片

Uri imageUri;
StorageTask uploadTask;
StorageReference storageReference;

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    File compressedImageFile; //for compress library : https://github.com/zetbaitsu/Compressor

    if (requestCode == IMAGE_REQUEST && resultCode == RESULT_OK
            && data != null && data.getData() != null) {

        Uri selectedImageUri = data.getData();

        try {
            compressedImageFile = new Compressor(getContext()).compressToFile(new File(selectedImageUri.getPath()));

            imageUri = data.getData(); //this works well but uploaded original size.
//                imageUri =Uri.fromFile(compressedImageFile); //fail
//                imageUri= Uri.fromFile(new File(compressedImageFile.toURI().getPath())); //fail
//                imageUri= Uri.parse(new File(compressedImageFile.getPath()).toString()); //fail

            if (uploadTask != null && uploadTask.isInProgress()) {
                Toast.makeText(getContext(), "Upload in preogress", Toast.LENGTH_SHORT).show();
            } else {
                uploadImage();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2.uploadImage();

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

        uploadTask = fileReference.putFile(imageUri);
        uploadTask.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 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("Users").child(fuser.getUid());
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("imageURL", "" + mUri);
                    reference.updateChildren(map);                        
                } else {                       
                    Toast.makeText(getContext(), "Failed!", Toast.LENGTH_SHORT).show();

                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();

            }
        });
    }

1 个答案:

答案 0 :(得分:0)

将此代码添加到您的文件中:-

StorageReference storageReference;
storageReference = storage.getReference();

final StorageReference ref = storageReference.child("images/" + UUID.randomUUID());
        ref.putFile(your file path)
          .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot> ()  {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        ref.getDownloadUrl().addOnSuccessListener(new 
    OnSuccessListener<Uri>() {
                            @Override
                            public void onSuccess(Uri imageUrl{



                            }
                        });
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(IndivudualChatActivity.this, "Failed " + 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());

                    }
                });