I created a social.network app using Java language with the ability to insert a picture as a profile photo. If I try to upload a photo of mine it works but if other users try to do it using another device they don't see my photo and I don't see theirs. Basically everyone sees their own photo using their own device.
However, despite uploading my profile photo properly, it tends to disappear after a period of time.
How to fix it?
Screenshot:
https://i.stack.imgur.com/SXo99.jpg
Code for try to setting profile photo:
firebaseFirestore.collection("Users").document(user_id).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
if(task.getResult().exists()){
String name = task.getResult().getString("name");
String image = task.getResult().getString("image");
String about = task.getResult().getString("about");
String skills = task.getResult().getString("skills");
mainImageUri = Uri.parse(image);
setupName.setText(name);
setupAbout.setText(skills);
setupSkills.setText(about);
RequestOptions placeholderRequest = new RequestOptions();
placeholderRequest = placeholderRequest.placeholder(R.drawable.defaultuser);
Glide.with(getApplicationContext())
.setDefaultRequestOptions(placeholderRequest)
.load(image)
.into(setupImage);
}
} else {
String error = task.getException().getMessage();
Toast.makeText(SetupActivity.this, "C'è stato un problema, riprova per favore.", Toast.LENGTH_LONG).show();
}
progressBar.setVisibility(View.INVISIBLE);
setupButton.setEnabled(true);
}
});
setupButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String user_name = setupName.getText().toString();
final String about = setupAbout.getText().toString();
final String userSkills = setupSkills.getText().toString();
if (!TextUtils.isEmpty(user_name) && mainImageUri != null) {
progressBar.setVisibility(View.VISIBLE);
if (isChanged) {
user_id = firebaseAuth.getCurrentUser().getUid();
final StorageReference image_path = storageReference.child("profile_images").child(user_id + ".jpg");
image_path.putFile(mainImageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
storeFirestore(task, user_name, about, userSkills, image_path);
} else {
Toast.makeText(SetupActivity.this, "C'è stato un problema, riprova per favore.", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.INVISIBLE);
}
}
});
} else {
storeFirestore(null, user_name, about, userSkills, null);
}
} else {
if(TextUtils.isEmpty(user_name))
{
Toast.makeText(SetupActivity.this, "Per favore, inserisci un nickname!", Toast.LENGTH_LONG).show();
} else
{
Toast.makeText(SetupActivity.this, "Per favore, inserisci una foto profilo!", Toast.LENGTH_LONG).show();
}
}
}
});
setupImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(SetupActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_DENIED) {
Snackbar snackbar = Snackbar.make(view, "Hai autorizzato l'app ad accedere al tuo archivio esterno?", Snackbar.LENGTH_SHORT);
snackbar.show();
ActivityCompat.requestPermissions(SetupActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
} else {
BringImagePicker();
}
} else {
BringImagePicker();
}
}
});
}
private void storeFirestore(Task<UploadTask.TaskSnapshot> task, String user_name,
String userSkills, String about, StorageReference image_path){
Uri download_uri = null;
if(task != null) {
task.getResult().getStorage().getDownloadUrl();
} else {
download_uri = mainImageUri;
}
Map<String, String> userMap = new HashMap<>();
userMap.put("skills", userSkills);
userMap.put("name", user_name);
userMap.put("about", about);
userMap.put("image", download_uri.toString());
userMap.put("user_id", user_id);
firebaseFirestore.collection("Users").document(user_id).set(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
Toast.makeText(SetupActivity.this, "Le impostazioni sono state aggiornate!", Toast.LENGTH_LONG).show();
Intent mainIntent = new Intent(SetupActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();
} else {
Toast.makeText(SetupActivity.this, "C'è stato un problema, riprova per favore.", Toast.LENGTH_LONG).show();
}
progressBar.setVisibility(View.INVISIBLE);
}
});
}
In the first line "user_id" is "firebaseAuth.getCurrentUser().getUid();"
Code for onActivityResult:
This looks wrong:
Uri download_uri = null;
if(task != null) {
task.getResult().getStorage().getDownloadUrl();
} else {
download_uri = mainImageUri;
}
I recommend studying the documentation on getting a download URL (or here ), as that really is the one and only way to get a download URL.
In your case, that'd be something like this:
private void storeFirestore(Task<UploadTask.TaskSnapshot> task, String user_name,
String userSkills, String about, StorageReference image_path){
image_path.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri download_uri) {
Map<String, String> userMap = new HashMap<>();
userMap.put("skills", userSkills);
userMap.put("name", user_name);
userMap.put("about", about);
userMap.put("image", download_uri.toString());
userMap.put("user_id", user_id);
firebaseFirestore.collection("Users").document(user_id).set(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
Toast.makeText(SetupActivity.this, "Le impostazioni sono state aggiornate!", Toast.LENGTH_LONG).show();
Intent mainIntent = new Intent(SetupActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();
} else {
Toast.makeText(SetupActivity.this, "C'è stato un problema, riprova per favore.", Toast.LENGTH_LONG).show();
}
progressBar.setVisibility(View.INVISIBLE);
}
});
}
});
}
As per the documentation you'll need to use the onSuccessListener
to ensure that the download URL is actually filled and ready to be used.
I see that you call the storeFirestore
function two times, with the one on line 79 with the image_path
parameter with null value. That's probably the cause of the crash you mentioned in the comments above.
You can try with something like this:
private void storeFirestore(Task<UploadTask.TaskSnapshot> task, String user_name,
String userSkills, String about, StorageReference image_path){
Uri download_uri = null;
if (image_path != null) {
download_uri = image_path.getDownloadUrl();
}
else if(task != null) {
download_uri = task.getResult().getStorage().getDownloadUrl();
} else {
download_uri = mainImageUri;
}
download_uri.addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri downloaded_uri) {
Map<String, String> userMap = new HashMap<>();
userMap.put("skills", userSkills);
userMap.put("name", user_name);
userMap.put("about", about);
userMap.put("image", downloaded_uri.toString());
userMap.put("user_id", user_id);
firebaseFirestore.collection("Users").document(user_id).set(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
Toast.makeText(SetupActivity.this, "Le impostazioni sono state aggiornate!", Toast.LENGTH_LONG).show();
Intent mainIntent = new Intent(SetupActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();
} else {
Toast.makeText(SetupActivity.this, "C'è stato un problema, riprova per favore.", Toast.LENGTH_LONG).show();
}
progressBar.setVisibility(View.INVISIBLE);
}
});
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.