I am trying to load image in my Firebase storage from my database reference. I am then using Picasso (implementation 'com.squareup.picasso:picasso:2.71828') to retrieve the image from the database reference. The entirety of the code I am using can be found here https://github.com/kshitiz1007/Lets-Chat though with updated libraries and slightly update ui. But the two files of note are SettingActivity, where user chooses profile image and text status to put into database and ProfileActivity, where the the profile image is called for use by Picasso.
My understanding is that in SettingActivity, the profile image as well as the thumb size version of the profile image, are being saved to a hashmap in SettingActivity:
update_HashMap.put("image",downloadUrl);
update_HashMap.put("thumb_image",thumb_download_url);
//--------ADDING URL INTO DATABASE REFERENCE-------
mDatabaseReference.updateChildren(update_HashMap).addOnCompleteListener(new
My problem is that calling when Picasso calls load, display image of source (String display_image = dataSnapshot.child("image").getValue().toString();) never loads.
But I do not know the mechanism nor how the image is retrieved from Storage using the Firebase database reference. When Picasso tries to get the image from the database reference it says in Run
D/Picasso: Main created [R22] Request{com.google.android.gms.tasks.zzu@5843814} ...
D/Picasso: Main errored [R22]+246ms Unrecognized type of request: Request{com.google.android.gms.tasks.zzu@5843814}
If you view the second picture, the image seems to have a pointer to a task and Picasso doesn't know how to use that information.
Below is from SettingActivity.java
public class SettingActivity extends AppCompatActivity {
...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//-----STARTING GALLERY----
if(requestCode == GALLERY_PICK && resultCode == RESULT_OK){
Uri sourceUri = data.getData();
//-------CROPPING IMAGE AND SETTING MINIMUM SIZE TO 500 , 500------
CropImage.activity(sourceUri).
setAspectRatio(1,1).
setMinCropWindowSize(500,500).
start(SettingActivity.this);
}
//------START CROP IMAGE ACTIVITY------
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE ) {
//------CROP IMAGE RESULT------
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
mProgressDialog.setTitle("Uploading Image");
mProgressDialog.setMessage("Please wait while we process and upload the image...");
mProgressDialog.setCancelable(false);
mProgressDialog.setProgress(ProgressDialog.STYLE_SPINNER);
mProgressDialog.show();
Uri resultUri = result.getUri();
File thumb_filepath = new File(resultUri.getPath());
try {
//--------COMPRESSING IMAGE--------
Bitmap thumb_bitmap = new Compressor(this).
setMaxWidth(200).
setMaxHeight(200).
setQuality(75).
compressToBitmap(thumb_filepath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
thumb_bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
thumb_bytes= baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
final StorageReference filepath = mStorageReference.child("profile_image").child(uid+".jpg");
final StorageReference thumb_file_path = mStorageReference.child("profile_image").child("thumbs").child(uid+".jpg");
//------STORING IMAGE IN FIREBASE STORAGE--------
filepath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if(task.isSuccessful()){
@SuppressWarnings("VisibleForTests")
final String downloadUrl= filepath.getDownloadUrl().toString();
final UploadTask uploadTask = thumb_file_path.putBytes(thumb_bytes);
//---------- STORING THUMB IMAGE INTO STORAGE REFERENCE --------
uploadTask.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> thumb_task) {
@SuppressWarnings("VisibleForTests")
String thumb_download_url = uploadTask.getSnapshot().getMetadata().getReference().getDownloadUrl().toString();
// String thumb_download_url = thumb_task.getResult().getDownloadUrl().toString();
if(thumb_task.isSuccessful()){
Map update_HashMap=new HashMap();
// Download Url stored to HashMap but where is this HashMap and how does it reference storage from database
update_HashMap.put("image",downloadUrl);
update_HashMap.put("thumb_image",thumb_download_url);
//--------ADDING URL INTO DATABASE REFERENCE--------
mDatabaseReference.updateChildren(update_HashMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
mProgressDialog.dismiss();
Toast.makeText(SettingActivity.this, "Uploaded Successfuly...", Toast.LENGTH_SHORT).show();
}
else{
mProgressDialog.dismiss();
Toast.makeText(getApplicationContext(), " Image is not uploading...", Toast.LENGTH_SHORT).show();
}
}
});
}
else{
mProgressDialog.dismiss();
Toast.makeText(getApplicationContext(), " Error in uploading Thumbnail..", Toast.LENGTH_SHORT).show();
}
}
});
}
else{
mProgressDialog.dismiss();
Toast.makeText(getApplicationContext(), " Image is not uploading...", Toast.LENGTH_SHORT).show();
}
}
});
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
}
Below is from ProfileActivity.java
mfriendReqReference = FirebaseDatabase.getInstance().getReference().child("friend_request");
mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("users").child(user_id);
mFriendDatabase = FirebaseDatabase.getInstance().getReference().child("friends");
mNotificationReference = FirebaseDatabase.getInstance().getReference().child("notifications");
mRootReference = FirebaseDatabase.getInstance().getReference();
mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
//----fOR SETTING ONLINE---
getmDatabaseReference = FirebaseDatabase.getInstance().getReference().child("users").child(mFirebaseUser.getUid());
mProgressDialog = new ProgressDialog(ProfileActivity.this);
mProgressDialog.setTitle("Fetching Details");
mProgressDialog.setMessage("Please wait...");
mProgressDialog.setProgress(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
mCurrent_state = "not_friends"; // 4 types--- "not_friends" , "req_sent" , "req_received" & "friends"
//----ADDING NAME , STATUS AND IMAGE OF USER----
mDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String display_name = dataSnapshot.child("name").getValue().toString();
String display_status = dataSnapshot.child("status").getValue().toString();
String display_image = dataSnapshot.child("image").getValue().toString();
mProfileName.setText(display_name);
mProfileStatus.setText(display_status);
Picasso.get()
.load(display_image)
.placeholder(R.drawable.user_img)
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.into(mProfileImage);
// ---------------------------------------------------------------------------------
//https://stackoverflow.com/questions/46071230/use-glide-load-into-imageview-but-delay?rq=1
/*RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.user_img);
Glide.with(ProfileActivity.this)
.setDefaultRequestOptions(requestOptions)
.load(Uri.parse(display_image))
// .placeholder(R.drawable.user_img)
.into(mProfileImage);*/
//----ADDING TOTAL NO OF FRIENDS---
mFriendDatabase.child(user_id).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
long len = dataSnapshot.getChildrenCount();
mprofileFriendCount.setText("TOTAL FRIENDS : "+len);
//----SEEING THE FRIEND STATE OF THE USER---
//----ADDING THE TWO BUTTON-----
mfriendReqReference.child(mFirebaseUser.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//----CHECKING IF FRIEND REQUEST IS SEND OR RECEIVED----
if(dataSnapshot.hasChild(user_id)){
String request_type = dataSnapshot.child(user_id).child("request_type").getValue().toString();
if(request_type.equals("sent")){
mCurrent_state="req_sent";
mProfileSendReqButton.setText("Cancel Friend Request");
mProfileDeclineReqButton.setVisibility(View.INVISIBLE);
mProfileDeclineReqButton.setEnabled(false);
}
else if(request_type.equals("received")){
mCurrent_state="req_received";
mProfileSendReqButton.setText("Accept Friend Request");
mProfileDeclineReqButton.setVisibility(View.VISIBLE);
mProfileDeclineReqButton.setEnabled(true);
}
mProgressDialog.dismiss();
}
//---USER IS FRIEND----
else{
mFriendDatabase.child(mFirebaseUser.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mProfileDeclineReqButton.setVisibility(View.INVISIBLE);
mProfileDeclineReqButton.setEnabled(false);
if(dataSnapshot.hasChild(user_id)){
mCurrent_state="friends";
mProfileSendReqButton.setText("Unfriend This Person");
}
mProgressDialog.dismiss();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
mProgressDialog.dismiss();
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(ProfileActivity.this, "Error fetching Friend request data", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}