我正在从要上传到Firebase的图像中获取下载URL。下面是代码片段。
private void uploadFile() {
if (mImageUri != null) {
StorageReference fileReference =
mStorageRef.child(System.currentTimeMillis()
+ "." + getFileExtension(mImageUri));
uploadProgressBar.setVisibility(View.VISIBLE);
uploadProgressBar.setIndeterminate(true);
mUploadTask = fileReference.putFile(mImageUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot> () {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
uploadProgressBar.setVisibility(View.VISIBLE);
uploadProgressBar.setIndeterminate(false);
uploadProgressBar.setProgress(0);
}
}, 500);
Toast.makeText(PlayerImageUpload.this, "Player Upload successful", Toast.LENGTH_LONG).show();
Player upload = new Player(nameEditText.getText().toString().trim(),
taskSnapshot.getMetadata().getReference().getDownloadUrl().toString(),
descriptionEditText.getText ().toString ());
String uploadId = mDatabaseRef.push().getKey();
mDatabaseRef.child(uploadId).setValue(upload);
uploadProgressBar.setVisibility(View.INVISIBLE);
openImagesActivity ();
}
})
.addOnFailureListener(new OnFailureListener () {
@Override
public void onFailure(@NonNull Exception e) {
uploadProgressBar.setVisibility(View.INVISIBLE);
Toast.makeText(PlayerImageUpload.this, 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());
uploadProgressBar.setProgress((int) progress);
}
});
} else {
Toast.makeText(this, "You haven't Selected Any file selected", Toast.LENGTH_SHORT).show();
}`
目前,图片网址的格式为“ com.google.android.gms.tasks.zzu@******”,我认为这不是在其他活动中将图片从Firebase中拉回的正确网址。