Te recomendamos que revises esta resolución en un entorno controlado antes de enviarlo a producción, un saludo.
Solución:
Consulte la documentación para obtener una URL de descarga.
Cuando usted llama getDownloadUrl()
la llamada es asíncrona y debe suscribirse en una devolución de llamada exitosa para obtener los resultados:
// Calls the server to securely obtain an unguessable download Url
private void getUrlAsync (String date)
// Points to the root reference
StorageReference storageRef = FirebaseStorage.getInstance().getReference();
StorageReference dateRef = storageRef.child("/" + date+ ".csv");
dateRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener()
@Override
public void onSuccess(Uri downloadUrl)
//do something with downloadurl
);
Esto devolverá una URL de descarga pública indescifrable. Si acaba de cargar un archivo, esta URL pública estará en la devolución de llamada exitosa de la carga (no necesita llamar a otro método asíncrono después de haberlo cargado).
Sin embargo, si todo lo que quieres es un String
representación de la referencia, puede simplemente llamar .toString()
// Returns a Uri of the form gs://bucket/path that can be used
// in future calls to getReferenceFromUrl to perform additional
// actions
private String niceRefLink (String date)
// Points to the root reference
StorageReference storageRef = FirebaseStorage.getInstance().getReference();
StorageReference dateRef = storageRef.child("/" + date+ ".csv");
return dateRef.toString();
//Firebase Storage – Fácil de trabajar con cargas y descargas.
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable final Intent data)
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RC_SIGN_IN)
if(resultCode == RESULT_OK)
Toast.makeText(this,"Signed in!", LENGTH_SHORT).show();
else if(resultCode == RESULT_CANCELED)
Toast.makeText(this,"Signed in canceled!", LENGTH_SHORT).show();
finish();
else if(requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK)
// HERE I CALLED THAT METHOD
uploadPhotoInFirebase(data);
private void uploadPhotoInFirebase(@Nullable Intent data)
Uri selectedImageUri = data.getData();
// Get a reference to store file at chat_photos/
final StorageReference photoRef = mChatPhotoStorageReference
.child(selectedImageUri
.getLastPathSegment());
// Upload file to Firebase Storage
photoRef.putFile(selectedImageUri)
.addOnSuccessListener(new OnSuccessListener()
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot)
// Download file From Firebase Storage
photoRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener()
@Override
public void onSuccess(Uri downloadPhotoUrl)
//Now play with downloadPhotoUrl
//Store data into Firebase Realtime Database
FriendlyMessage friendlyMessage = new FriendlyMessage
(null, mUsername, downloadPhotoUrl.toString());
mDatabaseReference.push().setValue(friendlyMessage);
);
);
aquí estoy cargando y obteniendo la URL de la imagen al mismo tiempo…
final StorageReference profileImageRef = FirebaseStorage.getInstance().getReference("profilepics/" + System.currentTimeMillis() + ".jpg");
if (uriProfileImage != null)
profileImageRef.putFile(uriProfileImage)
.addOnSuccessListener(new OnSuccessListener()
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot)
// profileImageUrl taskSnapshot.getDownloadUrl().toString(); //this is depreciated
//this is the new way to do it
profileImageRef.getDownloadUrl().addOnCompleteListener(new OnCompleteListener()
@Override
public void onComplete(@NonNull Task task)
String profileImageUrl=task.getResult().toString();
Log.i("URL",profileImageUrl);
);
)
.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();
);