Android 毕加索未将图像从Firebase存储加载到RecyclerView
我正试图通过毕加索图书馆从Firebase存储中获取图像,并将其显示在回收视图上 我也尝试过使用其他库(如Glide和Fresco)加载图像,但仍然没有显示图像 请在下面查找此应用程序的代码: BlogRecyclerAdapter.java:Android 毕加索未将图像从Firebase存储加载到RecyclerView,android,firebase-storage,picasso,Android,Firebase Storage,Picasso,我正试图通过毕加索图书馆从Firebase存储中获取图像,并将其显示在回收视图上 我也尝试过使用其他库(如Glide和Fresco)加载图像,但仍然没有显示图像 请在下面查找此应用程序的代码: BlogRecyclerAdapter.java: public BlogRecyclerAdapter(Context context, List<Blog> blogList) { this.context = context; this.blogList
public BlogRecyclerAdapter(Context context, List<Blog> blogList) {
this.context = context;
this.blogList = blogList;
this.firebaseStorage = firebaseStorage;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.post_row, parent, false);
return new ViewHolder(view, context);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Blog blog = blogList.get(position);
//String imageUrl = null;
holder.title.setText(blog.getTitle());
holder.desc.setText(blog.getDesc());
DateFormat dateFormat = DateFormat.getDateInstance();
String formattedDate = dateFormat.format(new Date(Long.valueOf(blog.getTimestamp())).getTime());
holder.timestamp.setText(formattedDate);
String imageUrl = blog.getImage();
//TODO: Use Picasso library to load image
Picasso.get()
.load(imageUrl)
.into(holder.image);
}
@Override
public int getItemCount() {
return blogList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public TextView desc;
public TextView timestamp;
public ImageView image;
String userid;
public ViewHolder(View view, Context ctx) {
super(view);
title = (TextView) view.findViewById(R.id.postTitleList);
desc = (TextView) view.findViewById(R.id.postTextList);
image = (ImageView) view.findViewById(R.id.postImageList);
timestamp = (TextView) view.findViewById(R.id.timestampList);
userid = null;
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// we can go to the next activity...
}
});
}
}
}
post_row.xml:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:layout_margin="5dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/postImageList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:scaleType="centerCrop"
android:src="@mipmap/add_btn"/>
<TextView
android:id="@+id/postTitleList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Post Title"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/postTextList"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="15dp"
android:padding="10dp"
android:text="Post Description Here"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/timestampList"
android:text="Date Created"
android:padding="5dp"
android:textStyle="italic"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
在运行代码时,我还遇到以下错误消息:
E/RecyclerView:未连接适配器;跳过布局
如果您发现我的代码有问题,请告诉我,我将期待您的回复。您的childlistner似乎不起作用。尝试ValueEventListener而不是ChildEventListener。通过firebase了解更多关于听众的信息
问题在于事件的执行顺序-需要在onCreate中将适配器设置为recycler视图。毕竟,这可能不是图像库的问题 我希望您能理解,重新创建问题很困难,因为我需要创建一个测试Firebase数据库等,并100%确定答案。我强烈建议重新安排适配器代码 1将这两行移到PostListActivity中onCreate的底部:
blogRecyclerAdapter = new BlogRecyclerAdapter(PostListActivity.this, blogList);
recyclerView.setAdapter(blogRecyclerAdapter);
2向BlogRecyclerAdapter类添加一个方法,该方法允许更新设置到适配器的项:
public void updateBlogList(ArrayList<Blog> newBlogs){
this.blogList = newBlogs;
notifyDataSetChanged(); }
正如我所说,很难重新创建问题,代码中可能还有其他问题,但希望这能解决E/RecyclerView:没有连接适配器;跳过布局错误
此外,Glide似乎是从外部源加载图像的强大库。在我的一个项目中,我能够使用以下代码在onBind In适配器中加载图像:
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRef = mFirebaseDatabase.getReference(Constants.USERS_PATH).child(offeredRoute.getUserID());
mFirebaseStorage = FirebaseStorage.getInstance();
mStorageReference = mFirebaseStorage.getReference().child(Constants.IMAGES_PATH).child(offeredRoute.getUserID());
//download all other values
mRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mUserProfile = dataSnapshot.getValue(UserProfile.class);
//set data to views
if (mUserProfile != null) {
holder.resultUsernameTv.setText(mUserProfile.getUser());
if (mUserProfile.getPhotoUrl() == null || mUserProfile.getPhotoUrl().isEmpty()) {
Timber.v("No photo saved yet");
} else {
StorageReference downloadRef = mStorageReference.child(mUserProfile.getPhotoUrl());
Glide.with(mContext)
.using(new FirebaseImageLoader())
.load(downloadRef)
.into(holder.ImageViewRv);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
谢谢给我图像url,我会检查加载问题,首先设置适配器,然后获取列表,然后通过适配器中的getter和setter设置新列表,并调用notifydatasetchangedchange android:layout\u height=match\u parent to android:layout\u height=wrap\u内容在您的recyclerview中。我正在尝试从firebase存储中获取许多图像,而不仅仅是一个,那么我应该给你什么url呢?谢谢你的回答,但是当我运行你的代码时,应用程序崩溃了;跳过布局会消失,但图像仍然不可见。日志现在怎么说?另外,你在build.gradle中有哪些依赖项?我已经在问题中添加了build.gradle文件;保存在您的Firebase存储规则中?您的build.gradle中有实现com.firebaseui:firebaseui存储:4.3.x的副本
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.myblogapp"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.firebase:firebase-core:16.0.8'
implementation 'com.google.firebase:firebase-core:16.0.8'
implementation 'com.google.firebase:firebase-database:16.1.0'
implementation 'com.firebaseui:firebase-ui-storage:4.3.1'
implementation 'com.google.firebase:firebase-auth:16.2.0'
implementation 'com.google.firebase:firebase-storage:16.0.4'
implementation 'com.github.bumptech.glide:glide:3.7.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.theartofdev.edmodo:android-image-cropper:2.4.+'
implementation 'com.facebook.fresco:fresco:1.10.0'
implementation 'com.firebaseui:firebase-ui-storage:4.3.2'
}
apply plugin: 'com.google.gms.google-services'
}
mDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()){
Blog blog = dataSnapshot.getValue(Blog.class);
blogList.add(blog);
}
Collections.reverse(blogList);
blogRecyclerAdapter = new BlogRecyclerAdapter(PostListActivity.this, blogList);
recyclerView.setAdapter(blogRecyclerAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
blogRecyclerAdapter = new BlogRecyclerAdapter(PostListActivity.this, blogList);
recyclerView.setAdapter(blogRecyclerAdapter);
public void updateBlogList(ArrayList<Blog> newBlogs){
this.blogList = newBlogs;
notifyDataSetChanged(); }
Blog blog = dataSnapshot.getValue(Blog.class);
blogList.add(blog);
Collections.reverse(blogList);
blogRecyclerAdapter.updateBlogList(blogList);
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRef = mFirebaseDatabase.getReference(Constants.USERS_PATH).child(offeredRoute.getUserID());
mFirebaseStorage = FirebaseStorage.getInstance();
mStorageReference = mFirebaseStorage.getReference().child(Constants.IMAGES_PATH).child(offeredRoute.getUserID());
//download all other values
mRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mUserProfile = dataSnapshot.getValue(UserProfile.class);
//set data to views
if (mUserProfile != null) {
holder.resultUsernameTv.setText(mUserProfile.getUser());
if (mUserProfile.getPhotoUrl() == null || mUserProfile.getPhotoUrl().isEmpty()) {
Timber.v("No photo saved yet");
} else {
StorageReference downloadRef = mStorageReference.child(mUserProfile.getPhotoUrl());
Glide.with(mContext)
.using(new FirebaseImageLoader())
.load(downloadRef)
.into(holder.ImageViewRv);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});