Firebase存储获取downloadURL问题

时间:2019-03-25 05:11:15

标签: android

当我尝试获取Firebase存储中图像的下载URL时,我得到了一些奇怪的结果

  

com.google.android.gms.tasks.zzu@3a6d712

这是我的代码

public class MainActivity extends AppCompatActivity {

    private StorageReference mStorage;
    private ProgressDialog mProgressDialog;
    private static final int GALLERY_REQUEST_CODE = 1;
    private static final int CAMERA_REQUEST_CODE = 2;
    private Button uploadButton;
    private Button captureButton;
    ImageView imageViewBig, imageViewSmall;

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

        mStorage = FirebaseStorage.getInstance().getReference();
        mProgressDialog = new ProgressDialog(this);
        imageViewBig = findViewById(R.id.imageViewBig);
        imageViewSmall = findViewById(R.id.imageViewSmall);
        captureButton = findViewById(R.id.captureButton);
        uploadButton = findViewById(R.id.uploadButton);
        captureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent, GALLERY_REQUEST_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == GALLERY_REQUEST_CODE && resultCode == RESULT_OK) {
            Uri uri = data.getData();
            StorageReference filePath = mStorage.child("photos").child(uri.getLastPathSegment());
            filePath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(final UploadTask.TaskSnapshot taskSnapshot) {
                    Toast.makeText(MainActivity.this, "DONE", Toast.LENGTH_SHORT).show();
                    imageViewBig.setBackgroundColor(Color.GREEN);
                    imageViewBig.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.i("URL", taskSnapshot.getMetadata().getReference().getDownloadUrl().toString());
                        }
                    });
                }
            });
        }
    }
}

所以如何获得正确的下载URL,以使用毕加索将图像下载到图像视图中

3 个答案:

答案 0 :(得分:0)

使用此代码在Firebase上上传文件并获取该文件的下载URL

Uri file = Uri.fromFile(new File(aadhaarPath));
        final StorageReference riversRef = storageRef.child("documents/" + file.getLastPathSegment());
        uploadTask = riversRef.putFile(file);

        uploadTask.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                progressDialog.dismiss();
            }
        }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                // get the image Url of the file uploaded
                riversRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                        Uri downloadUrl = uri;
                        aadharDownloadUrl = downloadUrl.toString();
                        uploadPanCard(panCardPath);
                    }
                });
            }
        });

aadhaarPath 是您选择上传的文件路径

并将其添加到您的build .gradle

implementation 'com.google.firebase:firebase-storage:16.0.3'

答案 1 :(得分:0)

我将照片保存到Firebase存储中,并将图像名称保存到Firebase实时数据库中,然后我使用Glide从Firebase存储中加载图像:

            FirebaseStorage storage = FirebaseStorage.getInstance();
            StorageReference storageRef = storage.getReference();
            StorageReference imageRef = storageRef.child(PHOTO_NAME);

            GlideApp.with(mContext).load(islandRef).into(viewHolder.image);

答案 2 :(得分:0)

UploadTask

public class UploadTask extends StorageTask<UploadTask.TaskSnapshot>

一个可控制的任务,可以上载并触发事件以获取成功,进度和失败。它还允许暂停和恢复以控制上传操作。

Example

final StorageReference firebaseStorage = FirebaseStorage.getInstance().getReference();

StorageReference postPhotos = firebaseStorage.child("photos/");

final StorageReference upload_Image = postPhotos.child(uri.getLastPathSegment());

//uri = uri of your file which you want to update
UploadTask uploadTask = upload_Image.putFile(uri);
           uploadTask.addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {

                    }
                });
           uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        upload_Image.getDownloadUrl().addOnCompleteListener(new OnCompleteListener<Uri>() {
                            @Override
                            public void onComplete(@NonNull Task<Uri> task) {
                                if (task.isSuccessful()) {
                                    //downloadable uri
                                     Uri path = task.getResult();
                                }
                            }
                        });
                    }
                });