我在获取Firebase图片的网址[重复]时遇到问题

问题描述 投票:0回答:2

对于我在聊天室com.google.android.gms.tasks.zzu@d55ef5中发送的每张图像,这都会返回我>

以下是我的代码:

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

        if (requestCode == 1 && resultCode == RESULT_OK){
            Uri uriImagen = data.getData();
            storageReference = storage.getReference("chat_images");//El nombre de la carpeta en la que voy a guardar las imagenes
            final StorageReference photoReference = storageReference.child(uriImagen.getLastPathSegment()); //Obtenemos como un id de la imagen que la diferencia de todas las demás
            photoReference.putFile(uriImagen).addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    String urlPhoto = taskSnapshot.getMetadata().getReference().getDownloadUrl().toString();

                    Log.v("Url de la foto", urlPhoto);

                    Message m = new Message(tvName.getText().toString() + " te ha enviado una foto", urlPhoto, tvName.getText().toString(), "", "2", "00:00");
                    databaseReference.push().setValue(m);
                }
            });
        }
    }

需要帮助..提前感谢

<< >

您可以改用Task<Uri>,请尝试一下。通过不建议使用此getMetadata().getReference().getDownloadUrl(),建议您阅读此Upload-files firebase

photoReference.putFile(uriImagen).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { @Override public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { Task<Uri> downloadUri = taskSnapshot.getStorage().getDownloadUrl(); if(downloadUri.isSuccessful()){ String urlPhoto = downloadUri.getResult().toString(); Log.v("Url de la foto", urlPhoto); Message m = new Message(tvName.getText().toString() + " te ha enviado una foto", urlPhoto, tvName.getText().toString(), "", "2", "00:00"); databaseReference.push().setValue(m); }} }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { //Do whatever you want } });

尝试此代码:

public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { Task<Uri> downloadUrl = taskSnapshot.getStorage().getDownloadUrl(); downloadUrl.addOnCompleteListener(new OnCompleteListener<Uri>() { @Override public void onComplete(@NonNull Task<Uri> task) { Log.v(TAG, "Media is uploaded"); String downloadURL = "https://" + task.getResult().getEncodedAuthority() + task.getResult().getEncodedPath() + "?alt=media&token=" + task.getResult().getQueryParameters("token").get(0); Log.v(TAG, "downloadURL: " + downloadURL); //save your downloadURL } }); }

java android firebase firebase-storage
2个回答
0
投票
您可以改用Task<Uri>,请尝试一下。通过不建议使用此getMetadata().getReference().getDownloadUrl(),建议您阅读此Upload-files firebase

0
投票
尝试此代码:

public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { Task<Uri> downloadUrl = taskSnapshot.getStorage().getDownloadUrl(); downloadUrl.addOnCompleteListener(new OnCompleteListener<Uri>() { @Override public void onComplete(@NonNull Task<Uri> task) { Log.v(TAG, "Media is uploaded"); String downloadURL = "https://" + task.getResult().getEncodedAuthority() + task.getResult().getEncodedPath() + "?alt=media&token=" + task.getResult().getQueryParameters("token").get(0); Log.v(TAG, "downloadURL: " + downloadURL); //save your downloadURL } }); }

© www.soinside.com 2019 - 2024. All rights reserved.