图像从 firebase 存储到 google 地图标记

Image from firebase storage into google map marker

我正在尝试从 firebase 检索图像,然后将其上传到标记图标中。 根据 firebase 教程中的示例,我成功检索了图像 uri,当我将 uri 放入图像视图时,我可以将它工作得很好,然后我尝试将它放入标记的图标中,但出现错误:

   D/AndroidRuntime: Shutting down VM
E/UncaughtException: java.lang.NullPointerException
                         at maps.f.g.a(Unknown Source)
                         at maps.af.g$a.<init>(Unknown Source)
                         at maps.af.g.a(Unknown Source)
                         at maps.af.R.<init>(Unknown Source)
                         at maps.af.t.a(Unknown Source)
                         at vl.onTransact(:com.google.android.gms.DynamiteModulesB:167)
                         at android.os.Binder.transact(Binder.java:380)
                         at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.addMarker(Unknown Source)
                         at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
                         at com.htaka.mywall.MapsActivity.onSuccess(MapsActivity.java:142)
                         at com.htaka.mywall.MapsActivity.onSuccess(MapsActivity.java:137)
                         at com.google.android.gms.tasks.zze.run(Unknown Source)
                         at android.os.Handler.handleCallback(Handler.java:739)
                         at android.os.Handler.dispatchMessage(Handler.java:95)
                         at android.os.Looper.loop(Looper.java:135)
                         at android.app.ActivityThread.main(ActivityThread.java:5421)
                         at java.lang.reflect.Method.invoke(Native Method)
                         at java.lang.reflect.Method.invoke(Method.java:372)
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:914)
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)
I/DynamiteModule: Considering local module com.google.android.gms.tagmanager:3 and remote module com.google.android.gms.tagmanager:3
I/DynamiteModule: Selected local version of com.google.android.gms.tagmanager
W/GoogleTagManager: Tag Manager's event handler WILL NOT be installed (no container loaded)
I/GoogleTagManager: Tag Manager initilization took 29ms
D/FirebaseCrashApiImpl: throwable java.lang.NullPointerException
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.htaka.mywall, PID: 1013
                  java.lang.NullPointerException
                      at maps.f.g.a(Unknown Source)
                      at maps.af.g$a.<init>(Unknown Source)
                      at maps.af.g.a(Unknown Source)
                      at maps.af.R.<init>(Unknown Source)
                      at maps.af.t.a(Unknown Source)
                      at vl.onTransact(:com.google.android.gms.DynamiteModulesB:167)
                      at android.os.Binder.transact(Binder.java:380)
                      at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.addMarker(Unknown Source)
                      at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
                      at com.htaka.mywall.MapsActivity.onSuccess(MapsActivity.java:142)
                      at com.htaka.mywall.MapsActivity.onSuccess(MapsActivity.java:137)
                      at com.google.android.gms.tasks.zze.run(Unknown Source)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5421)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:914)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)
W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
I/Process: Sending signal. PID: 1013 SIG: 9
Application terminated.

我不知道有什么问题...这是我正在使用的代码 class 我使用 loadImage 将其设置为 imageview :

public void setMarkers(DataSnapshot dS ,boolean re) {
        if(re) {mMap.clear();}

        post = dS.getValue(Post.class);

        storageRef.child("mountains.jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                ImageView im=new ImageView(MapsActivity.this);
                GlideUtil.loadImage(uri.toString(),im );
                marker = mMap.addMarker(new MarkerOptions()
                        .position(new LatLng(post.latitude, post.longitude))
                        .title(post.title)
                        .icon(BitmapDescriptorFactory.fromBitmap(im.getDrawingCache()))
                );

            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                System.out.print("image "+exception.getMessage());
                marker = mMap.addMarker(new MarkerOptions()
                        .position(new LatLng(post.latitude, post.longitude))
                        .title(post.title)
                );
            }
        });

PS:如果我删除图标(BitmapDescriptorFactry ......),应用程序会正常运行。

这里是 loadImage,我从 firebase github 部分的示例中得到它,我测试了它并且它有效,你基本上发送带有 imageview 的 uri 并将它加载到 imageview,在如果有人感兴趣: Gradle : 编译 'com.github.bumptech.glide:glide:3.6.0'

public class GlideUtil {
    public static void loadImage(String url, ImageView imageView) {
        Context context = imageView.getContext();
        ColorDrawable cd = new ColorDrawable(ContextCompat.getColor(context, R.color.wallet_holo_blue_light));
        Glide.with(context)
                .load(url)
                .placeholder(cd)
                .crossFade()
                .centerCrop()
                .into(imageView);
    }

我想问题是你的 im.getDrawingCache() 方法 returns 无效,因为你的 ImageView 在你调用它的时候还没有被测量。因此,您可以尝试将 Glidepreload() 方法与 RequestListener 一起使用,并将加载的 Bitmap 设置为 Marker 的图标:

@Override
public void onSuccess(Uri uri) {
    ColorDrawable cd = new ColorDrawable(ContextCompat.getColor(MapsActivity.this, R.color.wallet_holo_blue_light));
    Glide.with(MapsActivity.this)
        .load(uri.toString())
        .asBitmap()
        .listener(new RequestListener<String, Bitmap>() {
            @Override
            public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
                return false;
            }

            @Override
            public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
                marker = mMap.addMarker(new MarkerOptions()
                    .position(new LatLng(post.latitude, post.longitude))
                    .title(post.title)
                    .icon(BitmapDescriptorFactory.fromBitmap(resource))
                );
                return true;
            }
        })
        .placeholder(cd)
        .centerCrop()
        .preload();    
}