【发布时间】:2021-08-23 16:37:43
【问题描述】:
我不能让应用程序在我的 android 应用程序上显示个人资料图片,我可以成功上传它,但在应用程序中显示它的代码不起作用。 问题是我的应用程序无法转换图像中的 firebase 链接,我真的不知道为什么,代码与 youtube 教程中的完全相同。
ProfileActivity.java
package com.example.scrapbase11;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import com.squareup.picasso.Picasso;
import com.theartofdev.edmodo.cropper.CropImage;
import com.theartofdev.edmodo.cropper.CropImageView;
import de.hdodenhof.circleimageview.CircleImageView;
public class ProfileActivity extends AppCompatActivity {
private CircleImageView Profileimage;
private TextView usernickname, userfirstlastname, usercomment, registrationdate;
private FirebaseAuth mAuth;
private DatabaseReference UsersRef;
private StorageReference UserProfileImageRef;
private ProgressDialog loadingBar;
private String currentuserid;
final static int Gallery_Pick = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
loadingBar = new ProgressDialog(this);
mAuth = FirebaseAuth.getInstance();
currentuserid = mAuth.getCurrentUser().getUid();
UserProfileImageRef = FirebaseStorage.getInstance().getReference().child("Profile Images");
usernickname = (TextView) findViewById(R.id.profile_nickname);
UsersRef = FirebaseDatabase.getInstance().getReference().child("Users").child(currentuserid);
userfirstlastname = (TextView) findViewById(R.id.profile_firstlastname);
usercomment = (TextView) findViewById(R.id.profile_comment);
Profileimage = (CircleImageView) findViewById(R.id.my_profile_pic);
registrationdate= (TextView) findViewById(R.id.terzarow);
UsersRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()){
String myusernickname = snapshot.child("nickname").getValue().toString();
String myusercomment = snapshot.child("status").getValue().toString();
String myfamilyname = snapshot.child("familyname").getValue().toString();
String mygivenname = snapshot.child("givenname").getValue().toString();
String myregistrationdate = snapshot.child("registrationdate").getValue().toString();
usernickname.setText("@" + myusernickname);
usercomment.setText(myusercomment);
userfirstlastname.setText(mygivenname + " " + myfamilyname);
registrationdate.setText(myregistrationdate);
}
else {
Toast.makeText(ProfileActivity.this, "Errore del database", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
Profileimage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent galleryIntent = new Intent();
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, Gallery_Pick);
}
});
UsersRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot)
{
if(snapshot.exists())
{
if (snapshot.hasChild("profileimage"))
{
String image = snapshot.child("profileimage").getValue().toString();
Picasso.get().load(image).into(Profileimage);
}
else
{
Toast.makeText(ProfileActivity.this, "Please select profile image first.", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==Gallery_Pick && resultCode==RESULT_OK && data!=null)
{
Uri ImageUri = data.getData();
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1, 1)
.start(this);
}
if(requestCode==CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE){
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if(resultCode == RESULT_OK){
loadingBar.setTitle("Profile image updating");
loadingBar.setMessage("Please wait...");
loadingBar.show();
loadingBar.setCanceledOnTouchOutside(true);
Uri resultUri = result.getUri();
StorageReference filePath = UserProfileImageRef.child(currentuserid + ".jpg");
filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
{
if (task.isSuccessful()){
final String downloadUrl = task.getResult().getStorage().getDownloadUrl().toString();
UsersRef.child("profileimage").setValue(currentuserid + ".jpg")
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
Intent setupIntent = new Intent(ProfileActivity.this, ProfileActivity.class);
startActivity(setupIntent);
Toast.makeText(ProfileActivity.this, "Profile Image stored succesfully", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
else {
String message = task.getException().getMessage();
loadingBar.dismiss();
Toast.makeText(ProfileActivity.this, "Error Occured", Toast.LENGTH_SHORT).show();
}
}
});
}
}
}
});
}
else {
Toast.makeText(this, "Error occured: Image can't be cropped. Try again", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
}
}
}
这是代码的主要部分,其中包括显示图像的部分:
if(snapshot.exists())
{
if (snapshot.hasChild("profileimage"))
{
String image = snapshot.child("profileimage").getValue().toString();
Picasso.get().load(image).into(Profileimage);
}
编辑:我也试过把 UsersRef.child("profileimage").setValue(downloadUrl) 但是结果是一样的,只是数据库中profileimage的代码不同,这是result
【问题讨论】:
-
你有没有试过检查如果任务不成功会发生什么?
标签: android firebase firebase-realtime-database firebase-storage