gpt4 book ai didi

java - 保存/发送到 Firebase 数据库后图像不显示

转载 作者:行者123 更新时间:2023-12-01 14:31:53 25 4
gpt4 key购买 nike

我正在尝试制作一个聊天应用程序,但我遇到了一个问题。当我尝试上传个人资料图片然后将其存储在 firebase 中时。我上传的上述图像始终存储在 firebase 中,但该图像未显示在应用程序中,只有空白屏幕。这个问题存在于我项目中所有与 image-firebase 相关的东西中(因为我一遍又一遍地重复使用相同的代码)。我的错误是什么?有什么我想念或做错了什么吗?欢迎任何形式的帮助。

我附上了一些可能与我的问题相关的代码和屏幕截图

  1. 个人资料 Activity

private final String TAG = "CA/ProfileActivity";

// // Will handle all changes happening in database

private DatabaseReference userDatabase, requestsDatabase, friendsDatabase;
private ValueEventListener userListener, requestsListener, friendsListerner;

// Users data

private String currentUserId, otherUserId;

// activity_profile views

private TextView name, status;
private CircleImageView image;
private KenBurnsView cover;
private FABsMenu menu;
private TitleFAB button1, button2, button3;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);

name = findViewById(R.id.profile_name);
status = findViewById(R.id.profile_status);
image = findViewById(R.id.profile_image);
menu = findViewById(R.id.profile_fabs_menu);
cover = findViewById(R.id.profile_cover);

otherUserId = getIntent().getStringExtra("userid");
currentUserId = FirebaseAuth.getInstance().getCurrentUser().getUid();
}

@Override
protected void onStart()
{
super.onStart();

if(userDatabase != null && userListener != null)
{
userDatabase.removeEventListener(userListener);
}

// Initialize/Update realtime user data such as name, email, status, image

userDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(otherUserId);
userDatabase.keepSynced(true); // For offline use
userListener = new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
try
{
String layoutName = dataSnapshot.child("name").getValue().toString();
String layoutStatus = dataSnapshot.child("status").getValue().toString();
final String layoutImage = dataSnapshot.child("image").getValue().toString();
final String layoutCover = dataSnapshot.child("cover").getValue().toString();

name.setText(layoutName);
status.setText("\"" + layoutStatus + "\"");

if(!layoutImage.equals("default"))
{
Picasso.get()
.load(layoutImage)
.resize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 180, getResources().getDisplayMetrics()), (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 180, getResources().getDisplayMetrics()))
.centerCrop()
.networkPolicy(NetworkPolicy.OFFLINE)
.placeholder(R.drawable.user)
.error(R.drawable.user)
.into(image, new Callback()
{
@Override
public void onSuccess()
{

}

@Override
public void onError(Exception e)
{
Picasso.get()
.load(layoutImage)
.resize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 180, getResources().getDisplayMetrics()), (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 180, getResources().getDisplayMetrics()))
.centerCrop()
.placeholder(R.drawable.user)
.error(R.drawable.user)
.into(image);
}
});

image.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Intent intent = new Intent(ProfileActivity.this, FullScreenActivity.class);
intent.putExtra("imageUrl", layoutImage);
startActivity(intent);
}
});
}
else
{
image.setImageResource(R.drawable.user);
}

if(!layoutCover.equals("default"))
{
Picasso.get()
.load(layoutCover)
.resize(getResources().getDisplayMetrics().widthPixels, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 260, getResources().getDisplayMetrics()))
.centerCrop()
.networkPolicy(NetworkPolicy.OFFLINE)
.placeholder(R.drawable.logo_cover)
.error(R.drawable.logo_cover)
.into(cover, new Callback()
{
@Override
public void onSuccess()
{

}

@Override
public void onError(Exception e)
{
Picasso.get()
.load(layoutCover)
.resize(getResources().getDisplayMetrics().widthPixels, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 260, getResources().getDisplayMetrics()))
.centerCrop()
.placeholder(R.drawable.logo_cover)
.error(R.drawable.logo_cover)
.into(cover);
}
});
}
else
{
cover.setImageResource(R.drawable.logo_cover);
}
}
catch(Exception e)
{
Log.d(TAG, "userDatabase listener exception: " + e.getMessage());
e.printStackTrace();
}
}

@Override
public void onCancelled(DatabaseError databaseError)
{
Log.d(TAG, "userDatabase listener failed: " + databaseError.getMessage());
}
};
userDatabase.addValueEventListener(userListener);

if(otherUserId.equals(currentUserId))
{
initMyProfile();
}
else
{
initOtherProfile();
}
}

@Override
protected void onResume()
{
super.onResume();

FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserId).child("online").setValue("true");
}

@Override
protected void onPause()
{
super.onPause();

FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserId).child("online").setValue(ServerValue.TIMESTAMP);
}

@Override
protected void onStop()
{
super.onStop();

removeListeners();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data)
{
super.onActivityResult(requestCode, resultCode, data);

if(requestCode == 1 && resultCode == RESULT_OK)
{
Uri url = data.getData();

//Uploading selected picture

StorageReference file = FirebaseStorage.getInstance().getReference().child("profile_images").child(currentUserId + ".jpg");
file.putFile(url).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>()
{
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task)
{
if(task.isSuccessful())
{
String imageUrl = task.getResult().toString();

// Updating image on user data

userDatabase.child("image").setValue(imageUrl).addOnCompleteListener(new OnCompleteListener<Void>()
{
@Override
public void onComplete(@NonNull Task<Void> task)
{
if(task.isSuccessful())
{
Toast.makeText(ProfileActivity.this, "Picture updated", Toast.LENGTH_SHORT).show();
}
else
{
Log.d(TAG, "updateImage listener failed: " + task.getException().getMessage());
}
}
});
}
else
{
Log.d(TAG, "uploadImage listener failed: " + task.getException().getMessage());
}
}
});
}
else if(requestCode == 2 && resultCode == RESULT_OK)
{
Uri url = data.getData();

//Uploading selected cover picture

StorageReference file = FirebaseStorage.getInstance().getReference().child("profile_covers").child(currentUserId + ".jpg");
file.putFile(url).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>()
{
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task)
{
if(task.isSuccessful())
{
String imageUrl = task.getResult().toString();

// Updating image on user data

userDatabase.child("cover").setValue(imageUrl).addOnCompleteListener(new OnCompleteListener<Void>()
{
@Override
public void onComplete(@NonNull Task<Void> task)
{
if(task.isSuccessful())
{
Toast.makeText(ProfileActivity.this, "Cover updated", Toast.LENGTH_SHORT).show();
}
else
{
Log.d(TAG, "updateUserCover listener failed: " + task.getException().getMessage());
}
}
});
}
else
{
Log.d(TAG, "uploadCover listener failed: " + task.getException().getMessage());
}
}
});
}
}

  1. 全屏 Activity

公共(public)类 FullScreenActivity 扩展了 AppCompatActivity{ @覆盖 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_full_screen);

    final String url = getIntent().getStringExtra("imageUrl");

final ImageView image= findViewById(R.id.a_fullscreen_image);
final TextView message = findViewById(R.id.a_fullscreen_message);

message.setText("Loading Picture...");
message.setVisibility(View.VISIBLE);

Picasso.get()
.load(url)
.networkPolicy(NetworkPolicy.OFFLINE)
.into(image, new Callback()
{
@Override
public void onSuccess()
{
message.setVisibility(View.GONE);
}

@Override
public void onError(Exception e)
{
Picasso.get()
.load(url)
.into(image, new Callback()
{
@Override
public void onSuccess()
{
message.setVisibility(View.GONE);
}

@Override
public void onError(Exception e)
{
message.setVisibility(View.VISIBLE);
message.setText("Error: Could not load picture.");
}
});
}
});

}

  1. Firebase 规则

firebase rules

  1. 应用程序中的上述问题

the output for the fullscreen activity

the output for the chat(应该显示的图像)

the output for the profile activity(个人资料图片应该是我之前上传的不同图片,而不是默认图片)

  1. Stored images in firebase

最佳答案

您正在通过 Intent 传递数据。在logcat中打印layoutImage,检查链接是否存在。

最常用的方法是您可以直接在全屏 Activity 中获取个人资料图片链接,并将其添加到您的 picasso 代码中。它会起作用。

关于java - 保存/发送到 Firebase 数据库后图像不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62433540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com