- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
当在运行时 onStart()
方法上尝试 Picasso 时,它通常使用来自控制台的下载链接从 Firebase 加载图像。
但是当我尝试在 StorageReference onSuccess
方法中加载图像时,它就无法工作。
这个类正在扩展 fragment :
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_IN && resultCode == Activity.RESULT_OK) {
isGranted = true;
String path = FireStorage.retUID() + "/";
showIndProgress();
Uri uri = data.getData();
StorageReference childPathRef = FireStorage.storageRef.child(path + uri.getLastPathSegment());
childPathRef.putFile(uri)
.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if(task.isSuccessful()) {
Picasso.with(mContext).load(task.getResult().getDownloadUrl())
.into(profView);
Picasso.Builder build = new Picasso.Builder(mContext);
build.listener(new Picasso.Listener() {
@Override
public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
Toast.makeText(mContext, "exception\n" + exception.toString(), Toast.LENGTH_SHORT).show();
}
});
progressDialog.dismiss();
} else {
//failed
}
}
});
}
}
这是我在另一个类上的 FirebaseStorage 实例:
public static FirebaseStorage storage = FirebaseStorage.getInstance();
public static StorageReference storageRef =
storage.getReferenceFromUrl("gs://myurl.appspot.com/");
list :
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
我的 XML 布局:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@drawable/gcover"
android:id="@+id/relativeLayout">
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profPhoto"
android:clickable="true"
android:layout_width="120dp"
android:layout_height="120dp"
android:padding="20dp"
android:src="@drawable/default_prof"
android:scaleType="centerCrop"
app:civ_border_color="@color/trans"
app:civ_border_width="2dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
我的 View 初始化:
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_prof, container, false);
profView = (CircleImageView) view.findViewById(R.id.profPhoto);
btn1 = (Button) view.findViewById(R.id.btn_1);
btn2 = (Button) view.findViewById(R.id.btn_2);
btn3 = (Button) view.findViewById(R.id.btn_3);
return view;
}
最佳答案
更新 2:
虽然我发布的代码对我有效,但您表示它对您无效。我的测试不使用 fragment ,我不知道你使用的是什么版本的库,现在知道 fragment 是如何添加到布局中的,等等。不同行为的可能原因太多,无法有效调试它在 SO 上。
作为实验,您可以尝试使用 Glide 和 FirebaseUI 代替 Picasso 执行下载。代码如下所示,对我有用。因为下载是从存储引用而不是 URL,所以这种方法需要对存储的读取访问权限。您还需要将这些行添加到您的构建依赖项中:
// FirebaseUI 2.0.1 is the version to use for Firebase SDK version 11.0.1
// SDK/Firebase version compatibility is important.
// See the FirebaseUI docs for a table of compatible versions.
compile 'com.firebaseui:firebase-ui-storage:2.0.1'
compile 'com.github.bumptech.glide:glide:3.8.0'
.
childPathRef.putFile(Uri.fromFile(file))
.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
Log.d(TAG, "Upload: SUCCESS");
Glide.with(mContext)
.using(new FirebaseImageLoader())
.load(childPathRef)
.dontAnimate()
.listener(new RequestListener<StorageReference, GlideDrawable>() {
@Override
public boolean onException(Exception e, StorageReference model,
Target<GlideDrawable> target, boolean isFirstResource) {
Log.e(TAG, "Download: FAILED: ", e);
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource,
StorageReference model, Target<GlideDrawable> target,
boolean isFromMemoryCache, boolean isFirstResource) {
Log.d(TAG, "Download: SUCCESS");
return false;
}
})
.into(mCircleImageView);
} else {
Log.e(TAG, "Upload: FAILED: " + task.getException().getMessage());
}
}
});
更新:
额外的调试显示上传和下载都成功完成。问题出在下载图像的渲染上。 documentation for CircleImageView表示从 Picasso 下载时必须使用 noFade()
:
If you use an image loading library like Picasso or Glide, you need to disable their fade animations to avoid messed up images. For Picasso use the
noFade()
option, for Glide usedontAnimate()
.
还发现渲染小图但大图显示为黑色。添加了 fit()
以使 Picasso 调整图像大小。
要同时检测上传成功和失败,请使用 completion listener而不仅仅是一个成功的监听器,并测试 task.isSuccessful()
。上传可能因安全规则或无效的 StorageReference
而失败:
StorageReference childPathRef = Class.storageRef.child(path + uri.getLastPathSegment());
childPathRef.putFile(uri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
Log.d(TAG, "Upload: SUCCESS");
Picasso.with(mContext)
.load(task.getResult().getDownloadUrl())
.noFade()
.fit()
.into(profView, new Callback() {
@Override
public void onSuccess() {
Log.d(TAG, "Download: SUCCESS");
Toast.makeText(mContext, "download done" , Toast.LENGTH_SHORT).show(); }
@Override
public void onError() {
Log.d(TAG, "Download: FAILED");
}
});
Toast.makeText(mContext, "upload done" , Toast.LENGTH_SHORT).show();
} else {
Log.e(TAG, "Upload: FAILED: " + task.getException().getMessage());
}
progressDialog.dismiss();
}
});
关于java - picasso 无法在 StorageReference 的 onSuccess 方法中加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44604650/
Picasso.with(context).load("url").into(imageView); 在这里,我想要位图而不是 url,我该如何实现这一点。 像下面- Picasso.with(con
我在运行低于 API 版本 21 的手机上遇到 noClassDefFoundError。首先它与其他类一起出现,在删除所有可能的代码后,它开始与 Picasso 库一起出现。我附上了我的 logca
在 Picasso.with(context) .. public static Picasso with(Context context) { if (singleton == null) {
我第一次尝试使用Picasso 如官方网站示例: private void setItemBgImageUsingPicasso(View convertView) { String imag
我添加了 picasso 依赖项,但似乎没有用。我尝试更改版本。但还是没用。 这是我的 build.gradle(模块) apply plugin: 'com.android.application'
我想用 com.squareup.picasso:picasso:2.5.3-SNAPSHOT 编译。但是我得到了这个错误: Error:Could not find com.squareup.pic
我只在 Lollipop 版本上遇到过这个问题。我可以在 Lollipop 以上的版本上轻松运行该应用程序。当我在我的应用程序文件中运行该应用程序时出现错误: java.lang.NoClass
它在 Gradle build 和 Compile time 都没有给出任何错误消息,但是当我运行应用程序时它崩溃并给我以下错误消息。 Caused by: java.lang.ClassNotFou
在 android studio 中,Gradle 项目无法同步并导致此错误。 Error:(29, 13) Failed to resolve: com.squareup.picasso:picas
首先我知道这个错误是asked已经,但我的情况有点不同: 我正在分发 SDK(Android 库项目)。我的 SDK 需要其他东西(播放服务、支持 v4、gson jar 等)Picasso,所以我在
我认为这是一个库兼容性问题,但值得一试。 我正在尝试在我的应用程序中实现图像 slider ,但 Picasso 库出现了异常,它给了我 noSuchMethodError。 好吧,我连接两个字符串,
我对此非常绝望。问题是 picasso 只显示占位符图标。 我的 FirebaseStorageRefernce 代码看起来像这样引用 Firebase Blog post : StorageRef
我们正在使用Picasso加载应用程序中的所有图像,从小型化身到大型全屏图像,每10个每日事件用户中就会出现1个此类错误。 picasso 缓存已满,但据我们了解,它应该自行维护。 我们的日志表明,在
在我的函数中: public void getPointMarkerFromUrl(final String url, final OnBitmapDescriptorRetrievedListene
我正在尝试使用 Picasso 库加载矢量绘图 Picasso.get().load(R.drawable.project).into(image3a, new Callback() {
出于某种原因,每当我尝试将从 GET 请求获取的 URL 加载到服务器时,它都不会加载,但如果我尝试直接加载字符串,它就会工作。这是我的代码: new Thread(new Runnable() {
我在使用 picasso 库时遇到问题。当我使用此库(implementation 'com.squareup.picasso:picasso:2.71828')时,我的图像不会显示在 ImageVi
我使用 Picasso 库从 URL 加载图像。我想获得真实的图像大小,但我只能获得内存中的图像大小: Picasso.with(this) .load(imageUrl) .erro
我正在尝试在我的 gradle-android 项目中使用 Picasso 库。我正在使用 Eclipse IDE。当我对我的项目进行 gradle 构建时,构建正确,但在使用 Picasso 的 *
我遇到了一个问题,即在不同版本的 Android(KitKat 和 Marshmellow)上的某些设备(非 Nexus 设备)上,当我尝试使用 picasso 将图像加载到 ImageView 中时
我是一名优秀的程序员,十分优秀!