我正在尝试制作一个聊天应用,但我遇到了问题。当我尝试上传个人资料图片并将其存储在Firebase中时,已上传的图片总是被存储在Firebase中,但是在应用程序中却没有显示出来,只有一个空白的白屏。我的项目中每一个涉及图片-Firebase相关的东西都存在这个问题(因为我一直在重复使用相同的代码)。我的错误是什么?我错过了什么或者做错了什么吗?任何形式的帮助都受欢迎。
我附上一些可能与问题相关的代码和截图。
我附上一些可能与问题相关的代码和截图。
- 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());
}
}
});
}
}
}
- FullscreenActivity 全屏活动
public class FullScreenActivity extends AppCompatActivity { @Override 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.");
}
});
}
});
}
3. Firebase规则
4. 应用中前面提到的问题
聊天输出 (图片应该显示)
个人资料活动的输出 (个人资料图片应该是我之前上传的不同图片而不是默认图片)
implementation 'com.github.bumptech.glide:glide:4.11.0'
。 - Anshul Khattar