字符串downloadThumbUri = uploadTask.getResult()。getStorage()。getDownloadUrl()。toString();不适用于UploadTasks

时间:2019-09-12 11:09:20

标签: java android firebase google-cloud-firestore

如何获取下载URI?

String downloadThumbUri = uploadTask.getResult().getStorage().getDownloadUrl().toString();

不起作用。

我正在使用位图压缩图像以将其用作缩略图。这是代码:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
compressedImageFile.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] thumbData = baos.toByteArray();

final UploadTask uploadTask = storageReference.child("post_images/thumbs").child(randomName + ".jpg").putBytes(thumbData);

uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

        //Toast.makeText(NewPostActivity.this,"Thumbnail was uploaded ",Toast.LENGTH_LONG).show();

        String downloadThumbUri = uploadTask.getResult().getStorage().getDownloadUrl().toString();

        Map<String,Object> postMap = new HashMap<>();

        postMap.put("image url",downloadUri);
        postMap.put("thumb",downloadThumbUri);
        postMap.put("desc",desc);
        postMap.put("user_id",current_user_id);
        postMap.put("timestamp",FieldValue.serverTimestamp());

        firebaseFirestore.collection("Posts").add(postMap).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
            @Override
            public void onComplete(@NonNull Task<DocumentReference> task) {

                if(task.isSuccessful()){

                    newPostProgress.setVisibility(View.INVISIBLE);
                    Toast.makeText(NewPostActivity.this,"Post was added ",Toast.LENGTH_LONG).show();
                    Intent mainIntent = new Intent(NewPostActivity.this,MainActivity.class);
                    startActivity(mainIntent);
                    finish();

                }
            }
        })
    }
}

1 个答案:

答案 0 :(得分:2)

使用以下代码获取下载uri并将其存储在Firestore中。放入onSucess

uploadTask.getDownloadUrl().addOnCompleteListener(new OnCompleteListener<Uri>() {
                @Override
                public void onComplete(@NonNull Task<Uri> task) {
                    String downloadThumbUri = task.getResult().toString();
                    Map<String,Object> postMap = new HashMap<>();
                    postMap.put("image url",downloadUri);
                    postMap.put("thumb",downloadThumbUri);
                    postMap.put("desc",desc);
                    postMap.put("user_id",current_user_id);
                    postMap.put("timestamp",FieldValue.serverTimestamp());

                    firebaseFirestore.collection("Posts").add(postMap).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
                        @Override
                        public void onComplete(@NonNull Task<DocumentReference> task) {

                            if(task.isSuccessful()){

                                newPostProgress.setVisibility(View.INVISIBLE);
                                Toast.makeText(NewPostActivity.this,"Post was added ",Toast.LENGTH_LONG).show();
                                Intent mainIntent = new Intent(NewPostActivity.this,MainActivity.class);
                                startActivity(mainIntent);
                                finish();
                            }
            });
        }
    })
    .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            progressBar.setVisibility(View.GONE);
            Toast.makeText(ProfileActivity.this, "aaa "+e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });