【问题标题】:Storing and Displaying Image using Glide & FireBase android使用 Glide 和 FireBase 存储和显示图像 android
【发布时间】:2018-11-21 19:34:09
【问题描述】:

我正在尝试使用 Glide 和 Firebase 上传和显示个人资料图片。上传部分已成功运行。但是,如果我尝试从数据库中加载该图像,它显示为空白。我的动机是在用户进入活动一次时加载 profile_image,他可以点击现有图像并根据自己的意愿进行更改。

我的代码

public class User extends AppCompatActivity {

    private ImageView imageView;
    private Uri filePath;
    private final int PICK_IMAGE_REQUEST = 71;
    StorageReference storageReference;

    private void loadImage(){

        Bundle bundle = getIntent().getExtras();
        assert bundle != null;
        final String retrievedName = bundle.getString("Name");

        // Reference to an image file in Cloud Storage
        StorageReference storageReference  = FirebaseStorage.getInstance().getReference().child(retrievedName).child("images/profile_image");
        imageView = findViewById(R.id.profile_image);

    // Load the image using Glide
        Glide.with(User.this.getApplicationContext())
                .load(storageReference)
                .into(imageView );
    }


    private void uploadImage() {

        if(filePath != null)
        {

            Bundle bundle = getIntent().getExtras();
            assert bundle != null;
            final String retrievedName = bundle.getString("Name");

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

            StorageReference ref = storageReference.child(retrievedName).child("images/profile_image");
            ref.putFile(filePath)
                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                            Toast.makeText(User.this, "Uploaded", Toast.LENGTH_SHORT).show();
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast.makeText(User.this, "Failed "+e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    });
        }
    }

    private void chooseImage() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
                && data != null && data.getData() != null )
        {
            filePath = data.getData();
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                imageView.setImageBitmap(bitmap);

                uploadImage();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        imageView = findViewById(R.id.profile_image);

        loadImage();

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    chooseImage();
            }
        });
    }

}

【问题讨论】:

    标签: android database firebase firebase-storage android-glide


    【解决方案1】:

    我终于找到了解决办法。

    在上传部分,我在实时数据库中添加了 url

    url_db = FirebaseDatabase.getInstance().getReference().child(retrievedName).child("Url");
            StorageReference ref = FirebaseStorage.getInstance().getReference().child(retrievedName).child("images/profile_image");
            ref.putFile(filePath)
                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                            @SuppressWarnings("VisibleForTests") Uri downloadUrl =
                                    taskSnapshot.getDownloadUrl();
                            uploadImageUrl = downloadUrl.toString();
    
                            url_db.setValue(uploadImageUrl);
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast.makeText(User.this, "Failed " + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    });
    

    在加载时,我从数据库中使用了这个 Url 并在 Glide 中使用

    imageView = findViewById(R.id.profile_image);
    
        url_db = FirebaseDatabase.getInstance().getReference().child(retrievedName).child("Url");
    
        url_db.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
    
                String url = dataSnapshot.getValue(String.class);
    
                if(url!= null){
    
                    imageLoad.setVisibility(View.VISIBLE);
    
    
                    Glide.with( User.this)
                            .load(url)
                            .into(imageView);
    
                    imageLoad.setVisibility(View.INVISIBLE);
    
                }
    
            }
    
            @Override
            public void onCancelled(DatabaseError databaseError) {
    
            }
        });
    

    感谢大家的支持。

    【讨论】:

    • 我一直在寻找这个答案几个小时。谢谢!
    【解决方案2】:

    正如官方 firebase 文档 HERE 中所说,您应该 getDownloadURL() 您的照片以便将其传递到您的滑翔库,这样滑翔就会显示您的图像

    在这一行添加getDownloadUrl();

        StorageReference ref = storageReference.child(retrievedName).child("images/profile_image").getDownloadUrl();
    

    然后用 glide 调用你的图像

    // Load the image using Glide
    Glide.with(this /* context */)
            .using(new FirebaseImageLoader())
            .load(ref)
            .into(imageView);
    

    要使用 FirebaseImageLoader() 只需记住将其添加到您的 gradle 中

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

    还关注THIS GitHub 链接,了解如何从StorageReference 加载图像

    另一种更简单的方法是获取 DownloadUrl 并在 glide 中使用它

    ref.putFile(filePath)
                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                             @SuppressWarnings("VisibleForTests") Uri downloadUrl = 
                         taskSnapshot.getDownloadUrl();
                        uploadImageUrl = downloadUrl.toString();
                    Toast.makeText(getApplicationContext(), "url to your file..."+uploadImageUrl, Toast.LENGTH_SHORT).show();
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast.makeText(User.this, "Failed "+e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    });
    

    uploadImageUrl 是一个全局变量private String uploadImageUrl;

    然后只需将该图像 url 加载到您的 imageView 中

    Glide.with(this /* your_context */)
        .load(uploadImageUrl)
        .centerCrop()
        .into(imageView)
    

    【讨论】:

    • StorageReference ref = storageReference.child(retrievedName).child("images/profile_image").getDownloadUrl(); 在这一行中我遇到了错误......而且.using 也无法解决
    • 但我需要在活动开始时加载值.... 上传后不需要。在上传下一张之前加载图片。
    • 所以,您应该首先将您的图片存储到您的数据库中,并使用下载网址访问它,如果您的第一张图片已存储,我提供的链接必须对您有所帮助
    猜你喜欢
    • 2017-10-11
    • 2017-01-12
    • 1970-01-01
    • 2018-08-13
    • 2019-03-09
    • 2017-02-06
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    相关资源
    最近更新 更多