为了从Firebase检索图像,需要在以下代码中进行哪些更改?

时间:2019-05-20 19:21:24

标签: java android firebase firebase-storage

我在线获得了以下源代码,并且我尝试了多种方法使其正常工作,但是我没有成功。以下代码不会将图像和名称从Firebase获取到应用程序中。有哪些可以使其生效的更改?毕加索工作正常。

private void uploadFile() {
if (mImageUri != null) {
    StorageReference fileReference = mStorageRef.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);

                    Upload upload = new Upload(mEditTextFileName.getText().toString().trim(),
                            Objects.requireNonNull(Objects.requireNonNull(taskSnapshot.getMetadata()).getReference()).getDownloadUrl().toString());
                    String uploadId = mDatabaseRef.push().getKey();
                    assert uploadId != null;
                    mDatabaseRef.child(uploadId).setValue(upload);
                }


            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(MainActivity.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();
}

这是在ImageActivity上

mDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");

mDatabaseRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                Upload upload = postSnapshot.getValue(Upload.class);
                mUploads.add(upload);
        }

        mAdapter = new ImageAdapter(ImagesActivity.this, mUploads);

        mRecyclerView.setAdapter(mAdapter);
        mProgressCircle.setVisibility(View.INVISIBLE);
    }

0 个答案:

没有答案