如何从Firebase存储中显示或获取图像

时间:2017-01-19 08:40:11

标签: android firebase firebase-storage

我是Android的新用户,我在从FirebaseStorage显示图片时遇到问题。

这是MainActivity.java中的代码:

ImageView image = (ImageView) findViewById(R.id.profilePic_image_view);

mStorage = FirebaseStorage.getInstance().getReference();
Uri downloadURI = mStorage.child("circleako.png").getDownloadUrl().getResult();

Picasso.with(MainActivity.this).load(downloadURI).fit().centerCrop().into(image);

在我的gradle里面是这些:

compile 'com.google.firebase:firebase-database:10.0.1'
compile 'com.google.firebase:firebase-storage:9.6.1'
compile 'com.google.android.gms:play-services-base:10.0.1'
compile 'com.google.android.gms:play-services-maps:10.0.1'
compile 'com.google.android.gms:play-services-location:10.0.1'
compile 'com.google.android.gms:play-services-analytics:10.0.1'
compile 'com.squareup.picasso:picasso:2.5.2'
testCompile 'junit:junit:4.12'

这是错误:

01-19 16:35:09.998 7334-7334/com.example.asuspc.timeclock E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.example.asuspc.timeclock, PID: 7334
  java.lang.NoSuchMethodError: No static method zzy(Ljava/lang/Object;)Ljava/lang/Object;
  in class Lcom/google/android/gms/common/internal/zzac; or its super classes
  (declaration of 'com.google.android.gms.common.internal.zzac' appears in
  /data/data/com.example.asuspc.timeclock/files/instant-run/dex/slice-com.google.android.
  gms-play-services-basement-10.0.1_a22c13f35fb245022b215534e2f1b995cc0c66d1-classes.dex)
at com.google.android.gms.internal.zzann.zzug(Unknown Source)
at com.google.android.gms.internal.zzann.zzuf(Unknown Source)
at com.google.firebase.storage.StorageReference.child(Unknown Source)
at com.example.asuspc.timeclock.MainActivity.onCreate(MainActivity.java:198)
at android.app.Activity.performCreate(Activity.java:6323)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

2 个答案:

答案 0 :(得分:1)

你可以使用Glide。使用Glide,您也可以显示GIF动画。

String urlImage = "You URL Image";

Glide.with(yourActivity.this)
        .load(urlImage)
        .into(yourImageView);

在gradle-app中:

compile 'com.github.bumptech.glide:glide:3.7.0'

答案 1 :(得分:0)

清理项目并在设备上完全重新安装应用程序,这似乎是即时运行的问题。

另外,我建议使用Glide而不是Picasso和Firebase存储(或者从URL加载图像的情况)。毕加索没有很好地处理EXIF,因此在某些情况下它可能会导致图像方向问题等问题。

编辑:

mStorage.child("circleako.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
    @Override
    public void onSuccess(Uri uri) {
        Picasso.with(MainActivity.this).load(uri).fit().centerCrop().into(image);
    }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle any errors
        }
});