Android中的firebase图像检索

时间:2018-05-10 09:57:41

标签: android firebase firebase-storage

我面临的问题是,使用Gallery Intent从图库中选择图像时,虽然图像存储在Firebase存储上但未显示在ImageView中。相反,占位符消失,没有任何内容可见。 P.S我是Android和Firebase的新手。

附上相同的屏幕截图。

before uploading

After uploading

以下是我的代码:

public class EditProfile extends AppCompatActivity {

Button selectImage, save_data_editProfile;
EditText editText, editText2;
ImageView imageView;
private StorageReference mStorage;
private Toolbar mToolBar;
private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth mFirebaseAuth;
private ProgressDialog mProgress;
private ImageButton mphotoPickerButton;
private String currentUserEmail;
private static final int GALLERY_INTENT = 2;
private DatabaseReference mCurrentUserDatabaseReference;
private Context mView;

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

    //selectImage = (Button) findViewById(R.id.selectImage);
    save_data_editProfile = (Button) findViewById(R.id.save_data_editProfile);
    //imageView = (ImageView) findViewById(R.id.imageView);
    editText = (EditText) findViewById(R.id.editText);
    editText2 = (EditText) findViewById(R.id.editText2);


    mView = EditProfile.this;
    initializeScreen();
    openImageSelector();
    initializeUserInfo();


}

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

    mStorage = FirebaseStorage.getInstance().getReference(); //make global
    super.onActivityResult(requestCode, requestCode, data);

    if(requestCode ==GALLERY_INTENT && resultCode == RESULT_OK){

        mProgress.setMessage("Uploading...");
        mProgress.show();

        Uri uri = data.getData();
        //Keep all images for a specific chat grouped together
        final String imageLocation = "Photos/profile_picture/" + currentUserEmail;
        final String imageLocationId = imageLocation + "/" + uri.getLastPathSegment();
        final String uniqueId = UUID.randomUUID().toString();
        final StorageReference filepath = mStorage.child(imageLocation).child(uniqueId + "/profile_pic");
        final String downloadURl = filepath.getPath();
        filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                //create a new message containing this image
                addImageToProfile(downloadURl);
                mProgress.dismiss();
            }
        });
    }

}

public void addImageToProfile(final String imageLocation){
    final ImageView imageView = (ImageView) findViewById(R.id.imageView);
    mCurrentUserDatabaseReference
            .child("profilePicLocation").setValue(imageLocation).addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    StorageReference storageRef = FirebaseStorage.getInstance()
                            .getReference().child(imageLocation);

                    //Picasso.get().load(imageLocation).into(imageView);



                    Glide.with(mView)
                            .using(new FirebaseImageLoader())
                            .load(storageRef)
                            .bitmapTransform(new CropSquareTransformation(mView))
                            .into(imageView);
                }
            }
    );

}

public void openImageSelector(){

    mphotoPickerButton = (ImageButton) findViewById(R.id.imageButton);
    mProgress = new ProgressDialog(this);
    mphotoPickerButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType("image/*");
            startActivityForResult(intent, GALLERY_INTENT);
            //mView = view;
        }
    });
}

private void initializeUserInfo(){
    final ImageView imageView = (ImageView) findViewById(R.id.imageView);
    mCurrentUserDatabaseReference
            .addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    User user = dataSnapshot.getValue(User.class);
                    try{
                        if(user.getProfilePicLocation() != null){
                            StorageReference storageRef = FirebaseStorage.getInstance()
                                    .getReference().child(user.getProfilePicLocation());



                            Glide.with(mView)
                                    .using(new FirebaseImageLoader())
                                    .load(storageRef)
                                    .bitmapTransform(new CropSquareTransformation(mView))
                                    .into(imageView);
                        }
                    }catch (Exception e){
                        Log.e("Error", e.getMessage());
                    }

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
}

private void initializeScreen(){
    mFirebaseAuth = FirebaseAuth.getInstance();
    mFirebaseDatabase = FirebaseDatabase.getInstance();
    currentUserEmail = EmailEncoding.commaEncodePeriod(mFirebaseAuth.getCurrentUser().getEmail());
    mCurrentUserDatabaseReference = mFirebaseDatabase
            .getReference().child(Constants.USERS_LOCATION
                    + "/" + currentUserEmail);
    }
}

1 个答案:

答案 0 :(得分:0)

尝试在OnSuccessListener和

上调用addImageToProfile(taskSnapshot.downloadURl.toString());
Glide.with(mView)
       .using(new FirebaseImageLoader())
       .load(imageLocation)
       .bitmapTransform(new CropSquareTransformation(mView))
       .into(imageView);