[英]How to set image view from Firebase storage?
我从文档和在线帮助中都尝试了所有示例,但无法获得存储中要显示在图像视图上的简单图像。
假设我在名为Pictures的文件夹中的存储器中有照片,那么photo1.jpg将存储在引用中:
StorageReference storageReference = FirebaseStorage.getInstance().getReference();
StorageReference photoReference= storageReference.child("pictures/photo1.jpg");
但是,如何设置它并用ID查找到的现有图像视图替换内容:
ImageView photoView= (ImageView) findViewById(R.id.photo_view);
我一直在听众上失败以获取Uri:
storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
profileImage.setImageURI(uri);
Log.d("Test"," Success!");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Log.d("Test"," Failed!");
}
});
尝试获取已上传到数据库中的图像的下载URL,然后使用Picasso或GLIDE库将其加载到所需的imageview
。
从Firebase存储中检索到的uri
可能有问题,请尝试将其记录在日志中,如下所示:
Log.d("uri:",uri.toString());
这可能会提示您,检索值出了什么问题。 如果您选择使用Glide或Picasso ,请从上面的链接中引用它们的GitHub页面,然后查看您所需要的。
利用Glide和毕加索都非常简单,这里有一个如何使用滑翔到的图像存储在您的例子imageView
。
// Reference to an image file in Cloud Storage
StorageReference storageReference = = FirebaseStorage.getInstance().getReference().child("yourImageReferencePath");
ImageView image = (ImageView)findViewById(R.id.imageView);
// Load the image using Glide
Glide.with(this /* context */)
.using(new FirebaseImageLoader())
.load(storageReference)
.into(image );
如果您不想使用任何外部库并想要检索图像,请参考此答案 ,这可能会有所帮助。
使用以下代码,您可以在ImageView上设置图像:
StorageReference storageReference = FirebaseStorage.getInstance().getReference();
StorageReference photoReference= storageReference.child("pictures/photo1.jpg");
final long ONE_MEGABYTE = 1024 * 1024;
photoReference.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
imageView.setImageBitmap(bmp);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Toast.makeText(getApplicationContext(), "No Such file or Path found!!", Toast.LENGTH_LONG).show();
}
});
您也可以使用Uri,为此您需要将字节数组保存到文件中。 使用下面的代码相同:
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "test.jpg");
f.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
// remember close de FileOutput
fo.close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.