我正在尝试从数据库参考中将图像加载到Firebase存储中。然后,我正在使用Picasso(实现'com.squareup.picasso:picasso:2.71828')从数据库引用中检索图像。我正在使用的全部代码都可以在https://github.com/kshitiz1007/Lets-Chat处找到,尽管它具有更新的库并稍微更新了ui。但是需要注意的两个文件是SettingActivity,其中用户选择配置文件图像和文本状态放入数据库中,而ProfileActivity,配置文件图像由Picasso调用。
我的理解是,在SettingActivity中,配置文件图像以及配置文件图像的拇指大小版本都保存在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
我的问题是当Picasso调用加载时调用,显示源图像(字符串display_image = dataSnapshot.child(“ image”)。getValue()。toString();)永远不会加载。
但是我不知道机制,也不知道如何使用Firebase数据库参考从存储中检索图像。当毕加索尝试从数据库引用中获取图像时,它会在运行中显示
D /毕加索:主要创作者[R22] 请求{com.google.android.gms.tasks.zzu@5843814} ...
D /毕加索:主要错误[R22] + 246ms无法识别的请求类型: 请求{com.google.android.gms.tasks.zzu@5843814}
如果您查看第二张图片,则该图片似乎指向任务,而毕加索不知道如何使用该信息。
下面是来自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();
}
}
}
}
下面来自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) {
}
});
}