无法从firebase检索图像

时间:2018-04-19 07:26:01

标签: android firebase firebase-storage

我正在尝试将我手动上传到firebase中的图像检索到imageview但无法检索,我提供了互联网权限,检查规则,既没有在logcat中识别任何内容。代码是:

public class homepage extends AppCompatActivity {

    private StorageReference mStorage;
    private ImageView mImage;
    private static final int IMAGE_CODE=1;

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

        mImage = (ImageView)findViewById(R.id.g_image);

        mStorage = FirebaseStorage.getInstance().getReference();
}
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==IMAGE_CODE && resultCode==RESULT_OK)
        {
            Uri uri = data.getData();
            StorageReference filepath = mStorage.child("Products");
            filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Uri downloadUri = taskSnapshot.getDownloadUrl();
                    Picasso.with(homepage.this).load(downloadUri).fit().centerCrop().into(mImage);
                }
            });
        }
    }
}

1 个答案:

答案 0 :(得分:1)

试试这个:

StorageReference storageref= FirebaseStorage.getInstance().getReference(); 
pathReference=storageref.child("Products/images.png"); 
pathReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() { 
  @Override 
public void onSuccess(Uri uri) { 
        //got the url
     } 
}).addOnFailureListener(new OnFailureListener() {
  @Override
 public void onFailure(@NonNull Exception exception) {
    // Handle any errors
  } 
});

更多信息:

https://firebase.google.com/docs/storage/android/download-files#download_data_via_url