猿问

String downloadThumbUri = uploadTask.getResult()

如何获取下载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();


                }

            }

        })

    }

}


子衿沉夜
浏览 93回答 1
1回答

倚天杖

使用以下代码获取下载 uri 并存储在 Firestore 中。把它放在onSucess里面uploadTask.getDownloadUrl().addOnCompleteListener(new OnCompleteListener<Uri>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onComplete(@NonNull Task<Uri> task) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String downloadThumbUri = task.getResult().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<String,Object> postMap = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; postMap.put("image url",downloadUri);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; postMap.put("thumb",downloadThumbUri);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; postMap.put("desc",desc);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; postMap.put("user_id",current_user_id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; postMap.put("timestamp",FieldValue.serverTimestamp());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; firebaseFirestore.collection("Posts").add(postMap).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onComplete(@NonNull Task<DocumentReference> task) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(task.isSuccessful()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newPostProgress.setVisibility(View.INVISIBLE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(NewPostActivity.this,"Post was added ",Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent mainIntent = new Intent(NewPostActivity.this,MainActivity.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(mainIntent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finish();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; })&nbsp; &nbsp; .addOnFailureListener(new OnFailureListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onFailure(@NonNull Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; progressBar.setVisibility(View.GONE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(ProfileActivity.this, "aaa "+e.getMessage(), Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });
随时随地看视频慕课网APP

相关分类

Java
我要回答