【发布时间】:2017-01-12 22:47:24
【问题描述】:
我知道解决方案存在于人们说使用 Glide 或 Picasso 等图像缓存工具可以显示图像的地方。但是我也试过了,还是不行。
这是我将文件上传到 Firebase 存储的实现
if (uri != null) {
//upload to storage
StorageReference riversRef = imagesRef.child(uri.getLastPathSegment());
UploadTask uploadTask = riversRef.putFile(uri);
// Register observers to listen for when the download is done or if it fails
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
Snackbar.make(coordinatorLayout, "Unable to process your request. Please try again.", Snackbar.LENGTH_SHORT).show();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
Uri downloadUrl = taskSnapshot.getMetadata().getDownloadUrl();
if (downloadUrl != null) {
Date date = new Date();
Locale locale = new Locale("en", "IN");
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy /h:mm a", locale);
String formattedDate = sdf.format(date);
HashMap<String, String> map = new HashMap<>();
map.put("isXRead", String.valueOf(false));
map.put("isYRead", String.valueOf(true));
map.put("imgUrl", String.valueOf(downloadUrl));
map.put("timestamp", formattedDate);
map.put("user", "X");
ref.push().setValue(map);
}
}
});
}
图片上传成功,返回url,类似
https://firebasestorage.googleapis.com/v0/b/private.appspot.com/o/Messenger_images%2F14076?alt=media&token=2164ec58-55c2-4b03-b97a-ebfd36e66593
这是我尝试使用 Volley 显示图像的方式:
ImageRequest ir = new ImageRequest(message.getImgUrl(), new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
// callback
// new GlideOperations(context, img).glideMessenger(response);
img.setImageBitmap(response);
}
}, 100, 100, null, null);
//ir.setShouldCache(false);
AppController.getInstance().addToRequestQueue(ir);
这是我尝试使用 Glide 显示图像的方式:
try {
Glide.with(mContext)
.load(object)
.thumbnail(0.5f)
.override(900, 400)
.crossFade(100)
.fitCenter()
.error(R.drawable.wallpaper)
.into(new SimpleTarget<GlideDrawable>() {
@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
try {
mImageView.setImageDrawable(resource);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
以上方法似乎都不起作用。感谢任何帮助。
解决方案
好像我使用的ImageView 有问题。感谢您的帮助。
【问题讨论】:
-
我在使用 Picasso 和 Firebase 存储方面有相当多的经验,如果您愿意再试一次 Picasso,我可以提供帮助,请告诉我。
-
很抱歉,滑翔似乎更有效率
-
要使用 glide 将存储在 Firebase Storage 中的图像加载到 imageView 中,我使用来自 this thread's answer by Samuel 的
FirebaseImageLoader类,它可以完美运行,您不需要获取它的直接公共 URL图片 -
@Wilik 请在这里帮助我stackoverflow.com/q/42480626/3671509
标签: android firebase android-volley android-glide firebase-storage