Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用Glide&;存储和显示图像;FireBase安卓_Android_Database_Firebase_Firebase Storage_Android Glide - Fatal编程技术网

Android 使用Glide&;存储和显示图像;FireBase安卓

Android 使用Glide&;存储和显示图像;FireBase安卓,android,database,firebase,firebase-storage,android-glide,Android,Database,Firebase,Firebase Storage,Android Glide,我正在尝试使用Glide&Firebase上传和显示个人资料图片。上载部分正在成功工作。但是,如果我试图从数据库加载该图像,其显示为空白。我的动机是在用户输入活动一次时加载profile_图像,用户可以点击现有图像并根据自己的意愿更改该图像 我的代码 public class User extends AppCompatActivity { private ImageView imageView; private Uri filePath; private final

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

我的代码

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();
            }
        });
    }

}
公共类用户扩展AppCompative活动{
私人影像视图;
私有Uri文件路径;
私有最终整数选择图像请求=71;
StorageReference-StorageReference;
私有void loadImage(){
Bundle Bundle=getIntent().getExtras();
断言包!=null;
最终字符串retrievedName=bundle.getString(“名称”);
//对云存储中图像文件的引用
StorageReference-StorageReference=FirebaseStorage.getInstance().getReference().child(retrievedName).child(“图像/配置文件\图像”);
imageView=findViewById(R.id.profile\u图像);
//使用Glide加载图像
Glide.with(User.this.getApplicationContext())
.load(存储参考)
.进入(图像视图);
}
私有void uploadImage(){
if(filePath!=null)
{
Bundle Bundle=getIntent().getExtras();
断言包!=null;
最终字符串retrievedName=bundle.getString(“名称”);
storageReference=FirebaseStorage.getInstance().getReference();
StorageReference ref=StorageReference.child(retrievedName).child(“图像/配置文件\图像”);
ref.putFile(文件路径)
.addOnSuccessListener(新的OnSuccessListener(){
@凌驾
成功时公共无效(UploadTask.TaskSnapshot TaskSnapshot){
Toast.makeText(User.this,“上载”,Toast.LENGTH_SHORT.show();
}
})
.addOnFailureListener(新的OnFailureListener(){
@凌驾
public void onFailure(@NonNull异常e){
Toast.makeText(User.this,“Failed”+e.getMessage(),Toast.LENGTH_SHORT.show();
}
});
}
}
私有void chooseImage(){
意图=新意图();
intent.setType(“image/*”);
intent.setAction(intent.ACTION\u GET\u CONTENT);
startActivityForResult(Intent.createChooser(Intent,“选择图片”),PICK\u IMAGE\u请求);
}
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
如果(requestCode==PICK\u IMAGE\u REQUEST&&resultCode==RESULT\u OK
&&数据!=null&&data.getData()!=null)
{
filePath=data.getData();
试一试{
位图Bitmap=MediaStore.Images.Media.getBitmap(getContentResolver(),filePath);
设置图像位图(位图);
上传图像();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u用户);
imageView=findViewById(R.id.profile\u图像);
loadImage();
imageView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
选择图像();
}
});
}
}

正如firebase官方文档中所述,您应该
获取照片的下载URL()
,以便将其传递到glide库,这样glide将显示您的图像

在这一行中添加
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'
}
另外,请关注GitHub链接,了解如何从
StorageReference

另一个更简单的方法是获取
下载URL
,并在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();
                    }
                });

我终于找到了解决办法

在上传部分,我在实时数据库中添加了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();
                    }
                });

感谢大家的支持。

StorageReference ref=StorageReference.child(retrievedName.child(“图像/配置文件\图像”).getDownloadUrl()在这一行中,我得到错误。。。。还有
。使用
无法解决问题,但我需要活动开始时的加载值。。。。上传后没有。在上传下一张图片之前先加载图片。因此,您应该首先使用下载url将您的图片存储到您的数据库中以访问它。如果您的第一张图片已存储,我提供的链接一定会对您有所帮助。我已经找了好几个小时了。非常感谢。
Glide.with(this /* your_context */)
    .load(uploadImageUrl)
    .centerCrop()
    .into(imageView)
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();
                    }
                });
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) {

        }
    });