llevo dias intentando recuperar la foto de perfil desde firebase y no es posible. De la unica manera que me funciona es que cargando la foto con Glide pero me recupera la de la cuenta de google, y yo quisiera recuperar la foto que sube el usuario a firebase. Subire ambos codigos, el que me funciona con google y el que intento recuperar de firebase pero me da error.
Adapter (Firebase) :
public class MessagesAdapter extends ArrayAdapter<Message> {
private Context ctx;
Message message;
private final String TAG = "demoMessageAdapter";
private ChatActivity dataUpdateAfterMessageDelete;
private FirebaseAuth mAuth;
private ArrayList<Message> messageObjects;
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.US);
private DatabaseReference UsersRef;
private Date convertedDate;
private String date;
private StorageReference UserProfileImageRef;
private FirebaseUser firebaseUser;
private GoogleSignInClient googleSignInClient;
private String currentUserId;
int num = 1;
MessagesAdapter(@NonNull Context context, int resource, @NonNull List<Message> objects, ChatActivity chatActivity) {
super(context, resource, objects);
this.ctx = context;
this.messageObjects = (ArrayList<Message>) objects;
this.dataUpdateAfterMessageDelete = chatActivity;
mAuth = FirebaseAuth.getInstance();
}
@SuppressLint("UseCompatLoadingForDrawables")
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
firebaseUser = mAuth.getCurrentUser();
UsersRef = FirebaseDatabase.getInstance().getReference().child("Users");
UserProfileImageRef = FirebaseStorage.getInstance().getReference().child("profileimage");
message = getItem(position);
MessagesAdapter.ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.messages_listview, parent, false);
viewHolder = new MessagesAdapter.ViewHolder();
viewHolder.messageTV = convertView.findViewById(R.id.messageTV);
viewHolder.messageSenderTV = convertView.findViewById(R.id.messageSenderTV);
viewHolder.messageTimeTV = convertView.findViewById(R.id.messageTimeTV);
viewHolder.fotoforos = convertView.findViewById(R.id.fotoforos);
viewHolder.deleteMessageButton = convertView.findViewById(R.id.deleteMessageButton);
convertView.setTag(viewHolder);
} else {
viewHolder = (MessagesAdapter.ViewHolder) convertView.getTag();
}
if (!message.user_id.equals(Objects.requireNonNull(mAuth.getCurrentUser()).getUid())) {
viewHolder.deleteMessageButton.setVisibility(View.INVISIBLE);
} else {
viewHolder.deleteMessageButton.setVisibility(View.VISIBLE);
}
viewHolder.messageSenderTV.setText(message.user_name);
viewHolder.messageTV.setText(message.message);
if (message != null) {
String profileimage = UsersRef.child(currentUserId).child("profileimage").getRef().toString();
Picasso.with(ctx).load(profileimage).placeholder(R.drawable.profilefoto).into(viewHolder.fotoforos);
} else {
viewHolder.fotoforos.setImageDrawable(ctx.getResources().getDrawable(R.drawable.profilefoto));
}
ChatActivity (firebase)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
setTitle("Chatroom");
threadNameTV = findViewById(R.id.threadNameTV);
newMessageET = findViewById(R.id.newMessageET);
homeButton = findViewById(R.id.homeButton);
sendButton = findViewById(R.id.sendButton);
messagesLV = findViewById(R.id.messagesLV);
final View messageview = getLayoutInflater().inflate(R.layout.messages_listview, null);
messageTV = messageview.findViewById(R.id.messageTV);
fotoforos = messageview.findViewById(R.id.fotoforos);
mAuth = FirebaseAuth.getInstance();
currentUserId = mAuth.getCurrentUser().getUid();
user = mAuth.getCurrentUser();
mDatabase = FirebaseDatabase.getInstance().getReference();
firebaseUser = mAuth.getCurrentUser();
UsersRef = FirebaseDatabase.getInstance().getReference().child("Users");
UserProfileImageRef = FirebaseStorage.getInstance().getReference().child("profileimage");
googleSignInClient = GoogleSignIn.getClient(ChatActivity.this, GoogleSignInOptions.DEFAULT_SIGN_IN);
threadNameTV.setTextColor(Color.parseColor("#000000"));
if (getIntent() != null && getIntent().getExtras() != null) {
if (getIntent().getExtras().containsKey("messageThreadDetails")) {
messageThread = (MessageThread) getIntent().getSerializableExtra("messageThreadDetails");
threadNameTV.setText(messageThread.title);
getMessages(messageThread.thread_id);
incrementar();
}
} else {
Toast.makeText(this, "No data received", Toast.LENGTH_SHORT).show();
}
homeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (user != null) {
user = null;
mDatabase = null;
mAuth = null;
Intent intent = new Intent(ChatActivity.this, ThreadsActivity.class);
startActivity(intent);
finish();
} else {
user = null;
mDatabase = null;
mAuth = null;
Toast.makeText(ChatActivity.this, "You need to login", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(ChatActivity.this, IniciarSesion.class);
startActivity(intent);
finish();
}
}
});
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message = newMessageET.getText().toString();
String user_name = user.getDisplayName();
String profileimage = UsersRef.child(currentUserId).child("profileimage").getRef().toString();
Picasso.with(ChatActivity.this).load(profileimage).placeholder(R.drawable.profilefoto).into(fotoforos);
if (message.isEmpty()) {
Toast.makeText(ChatActivity.this, "Enter Message", Toast.LENGTH_SHORT).show();
} else {
addMessage(message, user_name, profileimage, messageThread.thread_id);
}
}
});
}
Log:
java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
at com.google.firebase.database.DatabaseReference.child(DatabaseReference.java:96)
at com.example.ayurveda2.normas.MessagesAdapter.getView(MessagesAdapter.java:120)
Adapter (funciona y carga foto de google)
if (message != null) {
Glide.with(ctx).load(message.getProfileimage()).apply(RequestOptions.circleCropTransform()).into(viewHolder.fotoforos);
} else {
viewHolder.fotoforos.setImageDrawable(ctx.getResources().getDrawable(R.drawable.profilefoto));
}
Chat Activity (funciona y carga foto de google)
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message = newMessageET.getText().toString();
String user_name = user.getDisplayName();
String profileimage = UsersRef.child(currentUserId).child("profileimage").getRef().toString();
Glide.with(ChatActivity.this).load(firebaseUser.getPhotoUrl()).apply(RequestOptions.circleCropTransform()).into(fotoforos);
if (message.isEmpty()) {
Toast.makeText(ChatActivity.this, "Enter Message", Toast.LENGTH_SHORT).show();
} else {
addMessage(message, user_name, profileimage, messageThread.thread_id);
}
}
});
}
RecyclerView
es tomada de una instancia de la claseUsuario
. Dado que es facilísimo mapear un resultado de Firebase a una clase, podrías llenar una sola vez la instancia de la clase (desde dentro del listener) y luego usarla donde la necesites, en este caso en el Adapter.