【问题标题】:Receiving imgs from firebase to RecyclewView从 Firebase 接收图像到 RecyclerView
【发布时间】:2018-06-25 11:45:21
【问题描述】:

我的 RecyclerView 没有在 Firebase 上显示存储的图像。 为了更方便用户管理我的数据,我使用 FirebaseUI,为了加载我的 imgs,我使用 GLIDE(GlideApp 方法)。

所以:

清单:

    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />

梯度:

    implementation  'com.google.firebase:firebase-core:16.0.1'
implementation  'com.google.firebase:firebase-database:16.0.1'
implementation  'com.google.firebase:firebase-crash:16.0.1'
implementation  'com.google.firebase:firebase-auth:16.0.1'
implementation  'com.google.firebase:firebase-messaging:17.0.0'
implementation  'com.google.firebase:firebase-storage:16.0.1'

implementation 'com.firebaseui:firebase-ui-database:4.0.1'
implementation 'com.firebaseui:firebase-ui-storage:4.0.1'

kapt 'com.github.bumptech.glide:compiler:4.7.1'
implementation 'com.github.bumptech.glide:glide:4.7.1'
implementation 'com.github.bumptech.glide:annotations:4.7.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'

我的数据库:

我的 FOTOMODEL.CLASS:

public class PhotoModel {

@Exclude private String desc;
@Exclude private String image;
@Exclude private String  title;

public PhotoModel(String desc, String image, String title) {
    this.desc = desc;
    this.image = image;
    this.title = title;
}
//firebase
public PhotoModel (){

}

public String getDesc() {
    return desc;
}

public void setDesc(String desc) {
    this.desc = desc;
}

@Keep
public String getImage() {
    return image;
}
@Keep
public void setImage(String image) {
    this.image = image;
}


public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

滑翔模块:

public void registerComponents(Context context, Glide glide, Registry registry) {
// Register FirebaseImageLoader to handle StorageReference
registry.append(StorageReference.class, InputStream.class,
        new FirebaseImageLoader.Factory());

}

最后是我的代码:

    private RecyclerView recyclerList;

DatabaseReference mDatabaseStorage ;
FirebaseStorage mFirebaseStorage;
FirebaseRecyclerAdapter  firebaseRecyclerAdapter;
Query query;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    setContentView(R.layout.activity_photo);
    super.onCreate(savedInstanceState);

    mDatabaseStorage = FirebaseDatabase.getInstance().getReference().child("Blog");

    Toolbar toolbar = (Toolbar) findViewById(R.id.myToolbar);
    setSupportActionBar(toolbar);
    toolbar.setNavigationOnClickListener(view -> onBackPressed());

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    query = FirebaseDatabase.getInstance().getReference("Blog");

    ChildEventListener childEventListener = new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
            // ...
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
            // ...
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
            // ...
        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
            // ...
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            // ...
        }
    };
    query.addChildEventListener(childEventListener);

    recyclerList = (RecyclerView)findViewById(R.id.recycler_view);
    recyclerList.setHasFixedSize(true);
    recyclerList.setLayoutManager(new LinearLayoutManager(this));
}

@Override
protected void onStart() {
    super.onStart();

    FirebaseRecyclerOptions<PhotoModel> options = new FirebaseRecyclerOptions.Builder<PhotoModel>()
                    .setQuery(query, PhotoModel.class)
                    .build();

    mFirebaseStorage = FirebaseStorage.getInstance();

    firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<PhotoModel, PhotoViewHolder>(options) {

        @Override
        public void onError(DatabaseError e) {
            Log.e("FirebaseRecyclerAdapter", e.toString());
        }
        @NonNull
        @Override
        public PhotoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.recycler_single_photo_row, parent, false);
            return new PhotoViewHolder (view) ;
        }
        @Override
        protected void onBindViewHolder(@NonNull PhotoViewHolder holder, int position, @NonNull PhotoModel model) {

            Uri myUri = Uri.parse(model.getImage().toString());
            Log.i("ImageFromModel", model.getImage().toString());

           GlideApp.with(getApplicationContext())
                    .load(myUri)
                    .centerCrop()
                    .error(R.drawable.ic_error_black_24dp)
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                            Log.e("TAG_FROM_GLIDE", "Error loading image", e);
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                            return false;
                        }
                    })
                    .fitCenter()
                    .into(holder.firebaseImages);

        }

    };

    recyclerList.setAdapter(firebaseRecyclerAdapter);
    firebaseRecyclerAdapter.startListening();
}



public static class PhotoViewHolder extends RecyclerView.ViewHolder {

    public  ImageView firebaseImages;


    View globalView;

    public PhotoViewHolder(View itemView) {
        super(itemView);

        this.firebaseImages = (ImageView) itemView.findViewById(R.id.imageFirebase);
        globalView = itemView;
    }

    public void setTitle (String title){
        TextView post_name = (TextView) globalView.findViewById(R.id.photo_name);
        post_name.setText(title);
    }
    public void setDesc (String desc){
        TextView post_desc = (TextView) globalView.findViewById(R.id.photo_desc);
        post_desc.setText(desc);
    }



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu,menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if ( item.getItemId() == R.id.addPhotoActivity) {
        Intent intent = new Intent(PhotoActivity.this, AddNewPhotoActivity.class);
        startActivity(intent);
    }
    return true;
}

我来自 Glide 的 logcat(监听器):

06-25 11:09:20.853 3460-3460/com.example.htw.mytestapp E/TAG_FROM_GLIDE: Error loading image
class com.bumptech.glide.load.engine.GlideException: Failed to load resource
There were 3 causes:
java.io.FileNotFoundException(No such file or directory)
java.io.FileNotFoundException(No such file or directory)
java.io.FileNotFoundException(No such file or directory)
 call GlideException#logRootCauses(String) for more detail
  Cause (1 of 4): class com.bumptech.glide.load.engine.GlideException: Fetching data failed, class java.io.InputStream, LOCAL
There was 1 cause:
java.io.FileNotFoundException(No such file or directory)
 call GlideException#logRootCauses(String) for more detail
    Cause (1 of 1): class java.io.FileNotFoundException: No such file or directory
  Cause (2 of 4): class com.bumptech.glide.load.engine.GlideException: Fetching data failed, class android.os.ParcelFileDescriptor, LOCAL
There was 1 cause:
java.io.FileNotFoundException(No such file or directory)
 call GlideException#logRootCauses(String) for more detail
    Cause (1 of 1): class java.io.FileNotFoundException: No such file or directory
  Cause (3 of 4): class com.bumptech.glide.load.engine.GlideException: Fetching data failed, class android.content.res.AssetFileDescriptor, LOCAL
There was 1 cause:
java.io.FileNotFoundException(No such file or directory)
 call GlideException#logRootCauses(String) for more detail
    Cause (1 of 1): class java.io.FileNotFoundException: No such file or directory
  Cause (4 of 4): class com.bumptech.glide.load.engine.GlideException: Failed LoadPath{StringUri->Object->Drawable}, LOCAL
    Cause (1 of 2): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{StringUri->Drawable->Drawable}
    Cause (2 of 2): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{StringUri->Bitmap->Drawable}
  • 来自 Log.i("ImageFromModel", model.getImage().toString());

    content://com.example.htw.mytestapp.provider/my_images/Android/data/com.example.htw.mytestapp/files/Pictures/JPEG_20180613_104007_-1022454302.jpg content://com.example.htw.mytestapp.provider/my_images/Android/data/com.example.htw.mytestapp/files/Pictures/JPEG_20180613_104052_-450216218.jpg content://com.example.htw.mytestapp.provider/my_images/Android/data/com.example.htw.mytestapp/files/Pictures/JPEG_20180613_104118_1956285222.jpg content://com.example.htw.mytestapp.provider/my_images/Android/data/com.example.htw.mytestapp/files/Pictures/JPEG_20180619_114556_5693763765908649752.jpg content://com.example.htw.mytestapp.provider/my_images/Android/data/com.example.htw.mytestapp/files/Pictures/JPEG_20180621_123728_-880882730.jpg

我也试试:

StorageReference 存储 = mFirebaseStorage.getReference().child(model.getImage()); + GlideApp.with(getApplicationContext()) .load(存储)

但不是工作日志:

06-25 11:42:49.454 17886-17886/com.example.htw.mytestapp E/TAG_FROM_GLIDE: Error loading image
class com.bumptech.glide.load.engine.GlideException: Failed to load resource
There was 1 cause:
com.google.firebase.storage.StorageException(Object does not exist at location.)
 call GlideException#logRootCauses(String) for more detail
  Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Fetching data failed, class java.io.InputStream, REMOTE
There was 1 cause:
com.google.firebase.storage.StorageException(Object does not exist at location.)
 call GlideException#logRootCauses(String) for more detail
    Cause (1 of 1): class com.google.firebase.storage.StorageException: Object does not exist at location.

【问题讨论】:

  • 我之前没有使用过 Glide,但是如果你不介意的话,我有另一种从 url 获取图像位图的方法,我可以在这里发布代码
  • 当然,我会尝试一切。

标签: android firebase android-recyclerview firebaseui android-glide


【解决方案1】:

您的 firebase 数据库中的下载 url 链接不是您所需要的,而不是 content: example ...您只需要照片的纯链接

你需要这个网址

【讨论】:

  • 我不需要单个文件的 URL,而是整个存储文件夹的 URL 来加载到 GlideApp 方法加载中。
  • 您应该像现在一样存储每个图像 url 并检索每个 url,您无法从存储中的文件夹中加载一堆照片
  • 我应该得到 content://com.example.htw.mytestapp.provider/my_images/Android/data/com.example.htw.mytestapp/files/Pictures/JPEG_20180613_104007_-1022454302.jpg 等将它们放入某个对象( List ?),然后将此列表放入 GlideApp.load ?你能提供一些例子吗?
  • 对不起,但这不是 firebase 与 glide 的工作方式,glide 只是从 URL 打开图像或只是从您的应用程序中打开一个可绘制的图像,如果您想从 firebase 加载图像,您应该获取下载 url这张照片,看看我制作的这个视频,是西班牙语,但它会帮助你了解它是如何工作的,第 1 部分 youtube.com/watch?v=pNleQQhVfd0&t 和第 2 部分 youtube.com/watch?v=rYfNRh0HjeI
  • 我看过你的视频,但没有答案,放单个文件很简单,我要求更复杂的东西
【解决方案2】:

从 url 加载位图

try {
                    String imageUrl = "your/url/image.jpg";
                    InputStream in = (InputStream) new URL(imageUrl).getContent();
                    Bitmap bitmap = BitmapFactory.decodeStream(in);
//Store your bitmap for reuse
                    container.bitmap = bitmap;
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }

那么你可以使用ImageView.setImageBitmap(container.bitmap);

【讨论】:

    【解决方案3】:

    解决方案!

    全球验证:

    私有字符串uploadImageUrl;

    添加我的 addPhotoButton 方法:

    newPostDatabase.child("url_link").setValue(uploadImageUrl);

    添加:

    filepatch.putFile(photoURI).addOnSuccessListener

    添加这个:

              filepatch.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            Uri downloadUrl = uri;
                            uploadImageUrl = downloadUrl.toString();
                            Log.i("photoURI",uploadImageUrl ) ;
                        }
                    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-06
      • 1970-01-01
      • 2015-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-11
      相关资源
      最近更新 更多