Clicking on setupBtn
saves the following com.google.android.gms.tasks.zzu@9138f83
instead of image links.
I think the whole problem is in this line
Uri download_uri = Uri.parse(task.getResult().getMetadata().getReference().getDownloadUrl().toString());
Tell me what the problem is and how to fix it.
Complete code for Activity
public class SetupActivity extends AppCompatActivity {
private CircleImageView setupImage;
private Uri mainImageURI = null;
private String user_id;
private boolean isChanged = false;
private EditText setupName;
private Button setupBtn;
private ProgressBar setupProgress;
private StorageReference storageReference;
private FirebaseAuth firebaseAuth;
private FirebaseFirestore firebaseFirestore;
@SuppressLint("CheckResult")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup);
Toolbar setupToolbar = findViewById(R.id.setupToolbar);
setSupportActionBar(setupToolbar);
Objects.requireNonNull(getSupportActionBar()).setTitle("Account Setup");
firebaseAuth = FirebaseAuth.getInstance();
firebaseFirestore = FirebaseFirestore.getInstance();
user_id = Objects.requireNonNull(firebaseAuth.getCurrentUser()).getUid();
storageReference = FirebaseStorage.getInstance().getReference();
setupImage = findViewById(R.id.setup_image);
setupName = findViewById(R.id.setup_name);
setupBtn = findViewById(R.id.setup_btn);
setupProgress = findViewById(R.id.setup_progress);
setupProgress.setVisibility(View.VISIBLE);
setupBtn.setOnClickListener(v -> {
String user_name = setupName.getText().toString();
if (!TextUtils.isEmpty(user_name) && mainImageURI != null) {
String user_id = firebaseAuth.getCurrentUser().getUid();
setupProgress.setVisibility(View.VISIBLE);
StorageReference image_path = storageReference.child("profile_image").child(user_id + ".jpg");
image_path.putFile(mainImageURI).addOnCompleteListener(task -> {
if (task.isSuccessful()){
Uri download_uri = Uri.parse(task.getResult().getMetadata().getReference().getDownloadUrl().toString());
Map<String, String> userMap = new HashMap<>();
userMap.put("name", user_name);
userMap.put("image", download_uri.toString());
firebaseFirestore.collection("Users").document(user_id).set(userMap).addOnCompleteListener(task1 -> {
if(task1.isSuccessful()){
Toast.makeText(SetupActivity.this, "Update", Toast.LENGTH_LONG).show();
Intent mainIntent = new Intent(SetupActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();
}else {
String error = task1.getException().getMessage();
Toast.makeText(SetupActivity.this, "Firestore Error" + error, Toast.LENGTH_LONG).show();
}
setupProgress.setVisibility(View.INVISIBLE);
});
}else {
String error = task.getException().getMessage();
Toast.makeText(SetupActivity.this, "Image Error" + error, Toast.LENGTH_LONG).show();
setupProgress.setVisibility(View.INVISIBLE);
}
});
}
});
setupImage.setOnClickListener(v -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(SetupActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(SetupActivity.this, "Pre D", Toast.LENGTH_LONG).show();
ActivityCompat.requestPermissions(SetupActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
} else {BringImagePicker();}
}else {BringImagePicker();}
});
}
private void BringImagePicker() {
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1, 1)
.start(SetupActivity.this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
mainImageURI = result.getUri();
setupImage.setImageURI(mainImageURI);
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
}