无法从Firebase检索图像

时间:2019-03-19 08:18:50

标签: android firebase

我想从Firebase检索图像。我已经可以将图像上传到带有Firestore的Firebase中。但是,当我尝试检索图像时,它不会出现在我的应用程序中。

从firebase检索图像的代码:

mRecycleView = dialogView.findViewById(R.id.recycler_view);
    mRecycleView.setHasFixedSize(true);
    mRecycleView.setLayoutManager(new LinearLayoutManager(this));

    mUpload = new ArrayList<>();

    Mdatabase = FirebaseDatabase.getInstance().getReference("Upload");

    FirebaseUser user = firebaseAuth.getCurrentUser();
    String UserID = user.getUid();

    Mdatabase.child(UserID).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()){
                Upload upload = postSnapshot.getValue(Upload.class);
                mUpload.add(upload);
            }

            mAdapter = new ImageAdapter(DisplayAllActivity.this, mUpload);

            mRecycleView.setAdapter(mAdapter);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Toast.makeText(DisplayAllActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

将图片上传到Firebase的代码:

 if (mImageUri != null){
        StorageReference fileReference = mStoregeRef.child(user.getUid()).child(System.currentTimeMillis()+ "." + getFileExtension(mImageUri));

        mUploadTask = fileReference.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mProgressBar.setProgress(0);
                    }
                }, 500);

                Toast.makeText(UploadImageActivity.this, "Upload successful", Toast.LENGTH_LONG).show();
                Upload upload = new Upload(mEditTextFileName.getText().toString().trim(),
                        taskSnapshot.getUploadSessionUri().toString());

                String uploadId = mDatabaseRef.push().getKey();
                mDatabaseRef.child(user.getUid()).child(uploadId).setValue(upload);

            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(UploadImageActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        })
        .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
                mProgressBar.setProgress((int) progress);
            }
        });


    }else{
        Toast.makeText(this, "No file selected", Toast.LENGTH_SHORT).show();
    }

这是上载的类:

public class Upload {
private String mName;
private String mImageUrl;

public Upload(){

}

public Upload(String name, String imageUrl) {
    if (name.trim().equals("")) {
        name = "No Name";
    }

    mName = name;
    mImageUrl = imageUrl;
}

public String getName() {
    return mName;
}

public void setName(String name) {
    mName = name;
}

public String getImageUrl() {
    return mImageUrl;
}

public void setImageUrl(String imageUrl) {
    mImageUrl = imageUrl;
}

}

数据库的json:

"Upload" : {
"LV4y2BXnNWWkJix6YwTRJV8LdQs2" : {
  "-LaJymb5s11f-wHvdnxh" : {
    "imageUrl" : "https://firebasestorage.googleapis.com/v0/b/smartpusara.appspot.com/o?name=Upload%2FLV4y2BXnNWWkJix6YwTRJV8LdQs2%2F1552982676748.jpg&uploadType=resumable&upload_id=AEnB2UqwhZF9aq1IouAmNIjzLV_FTwE4nCJaH-zpX4jXHDC0eTFJIVjMCsnc4AwnrzUr7G54m9X0FaX8zB9zIVr5VV1Y1qIxutARw0bE_3Amg02l52GkzQ8&upload_protocol=resumable",
    "name" : "wwq"
  }
}

},

我试图找到解决方案,但没有找到解决该问题的方法

0 个答案:

没有答案