无法从Firebase存储下载图像

时间:2019-07-04 08:37:00

标签: java android firebase firebase-storage

我正在尝试从名称设置为用户的用户名的Firebase下载个人资料图片。我正在使用glide库下载图像,但出现StorageException: StorageException has occurred. Object does not exist at location.错误。

这是我的代码

String uid = user.getUid();

            storageReference.child("ProfilePictures").child(uid).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                    // Got the download URL for 'users/me/profile.png'
                    Log.d("TAG" , "URI = "+uri);

                    GlideApp.with(context).load(uri).into(profilepic);
                    //profilepic.setImageURI(uri);
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    // Handle any errors
                    Toast.makeText(getApplicationContext(), "Error getting Profile Picture", Toast.LENGTH_LONG).show();
                }
            });
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}

    });

我的数据库 enter image description here

enter image description here

storageReference的声明

StorageReference storageReference;

storageReference = FirebaseStorage.getInstance().getReference();

3 个答案:

答案 0 :(得分:2)

更改此:

storageReference.child("ProfilePictures").child(uid).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override

对此:

storageReference.child("ProfilePictures").child(uid + ".jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override

答案 1 :(得分:2)

确保是否允许用户使用此规则访问存储:

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if true;
    }
  }
}

将其放入您的依赖项中:

dependencies {
    // FirebaseUI Storage only
    implementation 'com.firebaseui:firebase-ui-storage:4.3.1'
}

现在从存储中获取图像:

StorageReference storageReference= FirebaseStorage.getInstance().getReference().child("ProfilePictures/"+uid+".jpg"); // if you know how to use this you can get image directly without doing that big query

//Following line will be useful when you try to get image from storage
GlideApp.with(this /* context */)
        .load(storageReference)
        .into(imageView);

有关更多信息,您可以阅读docs或在发生任何问题时发表评论

答案 2 :(得分:1)

 public StorageReference mStorageRef;
 StorageReference particular_image;

 private FirebaseDatabase firebasedatabase;
 private DatabaseReference databasereference;

 oncreate()
   {
     mStorageRef = FirebaseStorage.getInstance().getReference().child("give");
     firebasedatabase = FirebaseDatabase.getInstance();  //1st time is imp.
     databasereference = firebasedatabase.getReference().child("giver_data"); 

      particular_image.putFile(photoURI).addOnSuccessListener
                (this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        final Uri download_uri = taskSnapshot.getDownloadUrl();
                            Glide.with(getApplicationContext())
                .load(p_l.getPhotoUrl()).asBitmap().override(view.getMaxWidth(),view.getMaxHeight()).error(R.drawable.ic_selfie_point_icon)   //asbitmap after load always.
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        Bitmap d = new BitmapDrawable(resource).getBitmap();
                        int nh = (int) ( d.getHeight() * (512.0 / d.getWidth()) );
                        Bitmap scaled = Bitmap.createScaledBitmap(d, 512, nh, true);
                        //holder.food_img.setImageBitmap(scaled);
                        view.setImageBitmap(scaled);
                    }
                });
                   //getting uri of the image stored
                      //Photo_link p_link =newPhoto_link(download_uri.toString());
                     //  databasereference.push().setValue(p_link);
                        //String uri_string = download_uri.toString();

                        pb.clearAnimation();
                        pb.clearFocus();
                       // animation.end();
                        Intent i = new Intent(getApplicationContext(),Giver_Edit.class);
                        i.setData(download_uri);
                        startActivity(i);

                        Toast.makeText(getApplicationContext(),"Food Image Uploaded successfully",Toast.LENGTH_SHORT).show();

                        Log.d("giver_image_success","no eroooooor_on_success");
                        //Toast.makeText(getApplicationContext(),"Image added",Toast.LENGTH_LONG).show();
                    }
                });


        //  final Uri selectedImgUri = getIntent().getData();


        particular_image.putFile(uri).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.d("giver_image_failure","eroooooor_on_ffailure");
                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

现在这应该可以解决您的问题!如果有帮助,请确保投票,如有疑问请发表评论!
注意:而不是我的UploadTask代码功能,而是使用代码中提到的onSuccess()。