从Firebase存储API中检索图像

时间:2017-04-05 04:48:51

标签: android firebase firebase-storage android-glide

我已经创建了一个从Firebase存储中检索图像的应用程序。图像被检索并显示在图像视图中,但是检索图像需要2分钟以上。我已经按照以下链接从Firebase存储中检索图像。使用滑动库也是。

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

这是我的代码

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文件

<?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>

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

1 个答案:

答案 0 :(得分:0)

使用UniversalLoader可以更快地加载图像而不是滑动它可以比滑翔更快地加速图像加载。

    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);

                }
            });