无法通过Firebase存储更新ImageView

时间:2018-08-31 22:59:50

标签: android firebase firebase-storage updating

我无法更新我的imageView,代码运行并且没有显示错误,但是图像没有上传到Firebase存储中

private ImageView profilepic_edit, contactinfo_edit, detailedinfo_edit;
private FirebaseAuth firebaseAuth;
private FirebaseDatabase firebaseDatabase;
private FirebaseStorage firebaseStorage;
Uri imagePath;
private static int PICK_IMAGE = 123;
private StorageReference storageReference;
Boolean imgupload = false;

我有

  

boolean imgupload = false;

设置为false,当用户点击imageview时它将变为true,因为我希望将图像上传到后台的Firebase存储中 这是我的活动结果

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

    if (requestCode == PICK_IMAGE && resultCode == RESULT_OK && data.getData() != null) {
        imagePath = data.getData();
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), imagePath);
            profilepic_edit.setImageBitmap(bitmap);
            imgupload = true;

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    super.onActivityResult(requestCode, resultCode, data);
}

这里是侦听器,当用户点击imageview时会发生

StorageReference storageReference = firebaseStorage.getReference();
profilepic_edit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Image"), PICK_IMAGE);
        }
    });
    if (imgupload.equals(true)) {
        updateUserData();
    }

这是我的更新用户数据方法

private void updateUserData() {
    StorageReference imageReference = storageReference.child(firebaseAuth.getUid()).child("Images").child("profile pic");
    UploadTask uploadTask = imageReference.putFile(imagePath);
    uploadTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(getContext(), "Upload failed!", Toast.LENGTH_SHORT).show();
        }
    }).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
            Toast.makeText(getContext(), "Upload successful!", Toast.LENGTH_SHORT).show();
        }
    }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
            //displaying the upload progress
            double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
            Toast.makeText(getContext(), "Uploaded " + ((int) progress) + "%...", Toast.LENGTH_LONG);

        }
    });

烤面包都没有显示我专门添加的内容,以检查图片是否太大且上传缓慢,也没有图片上传到Firebase存储。我不知道我哪里错了

0 个答案:

没有答案