本文介绍了从Firebase存储API检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了一个应用程序,它从Firebase存储中检索图像。图像被检索并显示在图像视图中,但是要花2分钟以上才能检索图像。我已经按照以下链接从Firebase存储中检索图像使用滑翔库也。





这是我的代码



MainActivity.java

  package com.example.vikas.where_i_am; 

导入android.media.Image;
导入android.support.v7.app.AppCompatActivity;
导入android.os.Bundle;
导入android.widget.ImageView;
导入android.widget.TextView;

import com.bumptech.glide.Glide;
导入com.google.firebase.storage.FirebaseStorage;
导入com.google.firebase.storage.StorageReference;

公共类MainActivity扩展AppCompatActivity
{

私人ImageView imageView;
私人TextView mtext;
私有StorageReference mstorage,filepath;
@Override
protected void onCreate(Bundled savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView =(ImageView)findViewById(R.id.action_image);
mtext =(TextView)findViewById(R.id.text);
$ b mstorage = FirebaseStorage.getInstance()。getReference()。child(Screenshot(129).png);
Glide.with(MainActivity.this)
.using(new FirebaseImageLoader())
.load(mstorage)
.fitCenter()
.centerCrop()
.into(imageView);




$ b

FirebaseImageLoader.java

  package com.example.vikas.where_i_am; 

/ **
*由vikas于5/4/17创建。
* /


导入android.util.Log;

import com.bumptech.glide.Priority;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.stream.StreamModelLoader;
导入com.google.android.gms.tasks.Tasks;
导入com.google.firebase.storage.StorageReference;
导入com.google.firebase.storage.StreamDownloadTask;

import java.io.IOException;
import java.io.InputStream;
$ b $ ** / **
* ModelLoader实现从FirebaseStorage下载带有Glide的图片。
*
*样本用法:
*< pre>
* StorageReference ref = FirebaseStorage.getInstance()。getReference()。child(myimage);
* ImageView iv =(ImageView)findViewById(R.id.my_image_view); ($)
* Glide.with(this)
* .using(new FirebaseImageLoader())
* .load(ref)
* .into(iv)
*< / pre>
* /
public class FirebaseImageLoader实现StreamModelLoader< StorageReference>
{

private static final String TAG =FirebaseImageLoader;

@Override
public DataFetcher< InputStream> getResourceFetcher(StorageReference模型,int宽度,int高度){
返回新的FirebaseStorageFetcher(模型);
}

private class FirebaseStorageFetcher implements DataFetcher< InputStream> {

私有StorageReference mRef;
private StreamDownloadTask mStreamTask;
private InputStream mInputStream;

FirebaseStorageFetcher(StorageReference ref){
mRef = ref;

$ b @Override
public InputStream loadData(Priority priority)throws Exception {
mStreamTask = mRef.getStream();
mInputStream = Tasks.await(mStreamTask).getStream();

返回mInputStream;

$ b @Override
public void cleanup(){
//如果可能,关闭流
if(mInputStream!= null){
试试{
mInputStream.close();
mInputStream = null;
} catch(IOException e){
Log.w(TAG,Could not close stream,e);



$ @覆盖
public String getId(){
return mRef.getPath();

$ b @Override
public void cancel(){
//如果可能,取消任务
if(mStreamTask!= null&& mStreamTask .isInProgress()){
mStreamTask.cancel();



$ b code
$ b

XML file

 <?xml version =1.0encoding =utf-8?> 
< LinearLayout xmlns:android =http://schemas.android.com/apk/res/android
xmlns:tools =http://schemas.android.com/tools
android:id =@ + id / activity_main
android:layout_width =match_parent
android:layout_height =match_parent
android:paddingBottom =@ dimen / activity_vertical_margin
android:paddingLeft =@ dimen / activity_horizo​​ntal_margin
android:paddingRight =@ dimen / activity_horizo​​ntal_margin
android:paddingTop =@ dimen / activity_vertical_margin
tools:context = com.example.vikas.where_i_am.MainActivity
android:orientation =vertical>

TextView
android:id =@ + id / text
android:layout_width =wrap_content
android:layout_height =wrap_content
android:text =Hello World!
android:layout_weight =0/>
< ScrollView
android:layout_width =wrap_content
android:layout_height =wrap_content>
android:id =@ + id / action_image
android:layout_width =match_parent
android:layout_height =wrap_content
android: layout_weight = 1/>
< / ScrollView>
< / LinearLayout>

任何人都可以帮助我,让图像检索更快。




















$ b $ ($ true $)。code> DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheOnDisc(true).cacheInMemory(true)
.imageScaleType(ImageScaleType.EXACTLY)
.displayer(new 。FadeInBitmapDisplayer(300))建立();

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
getApplicationContext())
.defaultDisplayImageOptions(defaultOptions)
.memoryCache(new WeakMemoryCache())
。 discCacheSize(100 * 1024 * 1024).build();

ImageLoader.getInstance()。init(config);

final ImageLoader imageLoader = ImageLoader.getInstance();

DisplayImageOptions options = new DisplayImageOptions.Builder()。cacheInMemory(true)
.cacheOnDisc(true).resetViewBeforeLoading(true)
.showImageForEmptyUri(R.mipmap.preview)
.showImageOnFail(R.mipmap.preview)
.build();

$ b mdatabase.child(Table_item).addValueEventListener(new ValueEventListener(){

$ b @Override
public void onDataChange(DataSnapshot dataSnapshot ){
Map map =(Map)dataSnapshot.getValue();
String image1 =(String)map.get(imagename);
String image2 =(String)map.get (image2name);
imageLoader.displayImage(ImageView1,image1,options,new ImageLoadingListener(){
$ b @Override
public void onLoadingStarted(String imageUri,View view){


$ b @Override
public void onLoadingFailed(String imageUri,View view,FailReason failReason){
progressBar.setVisibility(View.GONE);
}

@Override
public void onLoadingComplete(String imageUri,View view,Bitmap loadedImage){


}

@Override
public void onLoadingCancelled(String imageUri,View view){
progressBar.setVisibility(View.GONE);
}
});
imageLoader.displayImage(ImageView2,image2name,options,new ImageLoadingListener(){
@Override
public void onLoadingStarted(String imageUri,View view){

}

@Override
public void onLoadingFailed(String imageUri,View view,FailReason failReason){
progressBar.setVisibility(View.GONE);
}

@Override
public void onLoadingComplete(String imageUri,View view,Bitmap loadedImage){
i ++;
progressstatus(i);
}

@Override
public void onLoadingCancelled(String imageUri,View view){
progressBar.setVisibility(View.GONE);


});


I have made an app which is retrieving image from Firebase storage.Image is retrieved and displayed in the imageview but it is taking more than 2 minutes to retrieve image.I have followed tthe below link to retrieve image from Firebase storage which is using glide library also.

https://github.com/firebase/FirebaseUI-Android/tree/master/storage

Here is my code

MainActivity.java

package com.example.vikas.where_i_am;

import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;

public class MainActivity extends AppCompatActivity
{

    private ImageView imageView;
    private TextView mtext;
    private StorageReference mstorage,filepath;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView=(ImageView)findViewById(R.id.action_image);
        mtext=(TextView)findViewById(R.id.text);

        mstorage= FirebaseStorage.getInstance().getReference().child("Screenshot (129).png");
        Glide.with(MainActivity.this)
                .using(new FirebaseImageLoader())
                .load(mstorage)
                .fitCenter()
                .centerCrop()
                .into(imageView);
    }
}

FirebaseImageLoader.java

package com.example.vikas.where_i_am;

/**
 * Created by vikas on 5/4/17.
 */


        import android.util.Log;

        import com.bumptech.glide.Priority;
        import com.bumptech.glide.load.data.DataFetcher;
        import com.bumptech.glide.load.model.stream.StreamModelLoader;
        import com.google.android.gms.tasks.Tasks;
        import com.google.firebase.storage.StorageReference;
        import com.google.firebase.storage.StreamDownloadTask;

        import java.io.IOException;
        import java.io.InputStream;

/**
 * ModelLoader implementation to download images from FirebaseStorage with Glide.
 *
 * Sample Usage:
 * <pre>
 *     StorageReference ref = FirebaseStorage.getInstance().getReference().child("myimage");
 *     ImageView iv = (ImageView) findViewById(R.id.my_image_view);
 *
 *     Glide.with(this)
 *         .using(new FirebaseImageLoader())
 *         .load(ref)
 *         .into(iv);
 * </pre>
 */
public class FirebaseImageLoader implements StreamModelLoader<StorageReference>
{

    private static final String TAG = "FirebaseImageLoader";

    @Override
    public DataFetcher<InputStream> getResourceFetcher(StorageReference model, int width, int height) {
        return new FirebaseStorageFetcher(model);
    }

    private class FirebaseStorageFetcher implements DataFetcher<InputStream> {

        private StorageReference mRef;
        private StreamDownloadTask mStreamTask;
        private InputStream mInputStream;

        FirebaseStorageFetcher(StorageReference ref) {
            mRef = ref;
        }

        @Override
        public InputStream loadData(Priority priority) throws Exception {
            mStreamTask = mRef.getStream();
            mInputStream = Tasks.await(mStreamTask).getStream();

            return mInputStream;
        }

        @Override
        public void cleanup() {
            // Close stream if possible
            if (mInputStream != null) {
                try {
                    mInputStream.close();
                    mInputStream = null;
                } catch (IOException e) {
                    Log.w(TAG, "Could not close stream", e);
                }
            }
        }

        @Override
        public String getId() {
            return mRef.getPath();
        }

        @Override
        public void cancel() {
            // Cancel task if possible
            if (mStreamTask != null && mStreamTask.isInProgress()) {
                mStreamTask.cancel();
            }
        }
    }
}

XML file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.vikas.where_i_am.MainActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_weight="0"/>
    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/action_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    </ScrollView>
</LinearLayout>

Can anyone help me so that image retrieval is faster.

解决方案

use UniversalLoader to load image faster instead of glide it speed up ur image loading better than glide.

    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .cacheOnDisc(true).cacheInMemory(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .displayer(new FadeInBitmapDisplayer(300)).build();

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
            getApplicationContext())
            .defaultDisplayImageOptions(defaultOptions)
            .memoryCache(new WeakMemoryCache())
            .discCacheSize(100 * 1024 * 1024).build();

    ImageLoader.getInstance().init(config);

    final ImageLoader imageLoader = ImageLoader.getInstance();

    final DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
            .cacheOnDisc(true).resetViewBeforeLoading(true)
            .showImageForEmptyUri(R.mipmap.preview)
            .showImageOnFail(R.mipmap.preview)
            .build();


 mdatabase.child(Table_item).addValueEventListener(new ValueEventListener() {


        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Map map = (Map) dataSnapshot.getValue();
            String image1 = (String) map.get("imagename");
            String image2 = (String) map.get("image2name");
            imageLoader.displayImage(ImageView1, image1, options, new ImageLoadingListener() {

                @Override
                public void onLoadingStarted(String imageUri, View view) {

                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    progressBar.setVisibility(View.GONE);
                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {


                }

                @Override
                public void onLoadingCancelled(String imageUri, View view) {
                    progressBar.setVisibility(View.GONE);
                }
            });
            imageLoader.displayImage(ImageView2, image2name, options, new ImageLoadingListener() {
                @Override
                public void onLoadingStarted(String imageUri, View view) {

                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    progressBar.setVisibility(View.GONE);
                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    i++;
                    progressstatus(i);
                }

                @Override
                public void onLoadingCancelled(String imageUri, View view) {
                    progressBar.setVisibility(View.GONE);

                }
            });

这篇关于从Firebase存储API检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 13:14