本文介绍了使用Glide& amp;存储和显示图像FireBase安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Glide&上载并显示个人资料图片火力基地.上载部分已成功运行.但是,如果我尝试从数据库中加载显示为空白的图像.我的动机是在用户一次进入活动时加载profile_image,他可以点击现有图像并根据自己的意愿进行更改.
I am trying to upload and display the profile picture using Glide & Firebase. Upload part is successfully working. But if i try to load that image from Database its showing blank.My motive is to load the profile_image while the user entered into the activity once and he can tap on the existing image and change that for his wish.
我的代码
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();
}
});
}
}
推荐答案
最后,我得到了解决方案.
Finally I got the solution.
在上传部分,我将网址添加到了实时数据库中
At upload part, I added the url in Realtime database
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();
}
});
在加载时我从数据库中使用了该网址并在Glide中使用了
While loading that I used this Url from Database and used in 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) {
}
});
感谢支持人员.
这篇关于使用Glide& amp;存储和显示图像FireBase安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!