从firebase下载图片时出错

时间:2018-02-04 21:13:19

标签: android firebase firebase-storage

从firebase下载图像时出现问题。我可以找到URI并获得链接,但我无法在ImageView中下载/保存图像。我已经粘贴了我收到错误的方法以及我们在logcat上显示的错误:

//Method download image
mStorageRedImagenes = mStorageRef.child("images/"+miUIDUserMomentaneo+"/"+claveMomentanea+"/altavoz.png");

Log.d("PATH", "images/"+miUIDUserMomentaneo+"/"+claveMomentanea+"/altavoz.png");

mStorageRedImagenes.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                        // Got the download URL for 'users/me/profile.png'
                        Log.d("PATH", ""+uri.getPath());

                        Log.d("DENTRO DE ONSUCCES", uri.getPath());
                        InputStream imageStream = null;
                        try {
                            imageStream = getContentResolver().openInputStream(uri);

                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }

                        Bitmap bmp = BitmapFactory.decodeStream(imageStream);
                        //ImageView miImagencilla = null;
                          miImagen.setImageBitmap(bmp);

                        producto.setMiImagen(miImagen);
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception exception) {
                        // Handle any errors
                    }

              });

ERROR LOGCAT

02-04 16:02:40.837 11768-11768/com.example.angelus.firebaseandroidangel W/System.err: java.io.FileNotFoundException: No content provider: https://firebasestorage.googleapis.com/v0/b/fir-androidangel.appspot.com/o/images%2FNI6e8KqBzJfLYco7SGPb9MyvV6j2%2F-L4UxVYdG6MenFR0hs6S%2Faltavoz.png?alt=media&token=59f600d0-65ee-44b1-a200-e0ee29bb7069
02-04 16:02:40.837 11768-11768/com.example.angelus.firebaseandroidangel W/System.err:     at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1066)
02-04 16:02:40.837 11768-11768/com.example.angelus.firebaseandroidangel W/System.err:     at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:921)
02-04 16:02:40.837 11768-11768/com.example.angelus.firebaseandroidangel W/System.err:     at android.content.ContentResolver.openInputStream(ContentResolver.java:646)
02-04 16:02:40.837 11768-11768/com.example.angelus.firebaseandroidangel W/System.err:     at com.example.angelus.firebaseandroidangel.PantallaUser$10$2.onSuccess(PantallaUser.java:600)
02-04 16:02:40.837 11768-11768/com.example.angelus.firebaseandroidangel W/System.err:     at com.example.angelus.firebaseandroidangel.PantallaUser$10$2.onSuccess(PantallaUser.java:584)
02-04 16:02:40.837 11768-11768/com.example.angelus.firebaseandroidangel W/System.err:     at com.google.android.gms.tasks.zzj.run(Unknown Source)
02-04 16:02:40.837 11768-11768/com.example.angelus.firebaseandroidangel W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
02-04 16:02:40.837 11768-11768/com.example.angelus.firebaseandroidangel W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
02-04 16:02:40.837 11768-11768/com.example.angelus.firebaseandroidangel W/System.err:     at android.os.Looper.loop(Looper.java:135)
02-04 16:02:40.837 11768-11768/com.example.angelus.firebaseandroidangel W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5254)
02-04 16:02:40.837 11768-11768/com.example.angelus.firebaseandroidangel W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
02-04 16:02:40.837 11768-11768/com.example.angelus.firebaseandroidangel W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
02-04 16:02:40.837 11768-11768/com.example.angelus.firebaseandroidangel W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
02-04 16:02:40.837 11768-11768/com.example.angelus.firebaseandroidangel W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

1 个答案:

答案 0 :(得分:0)

将图像从Internet加载到ImageView有点复杂。这就是为什么有一些库来缓解这个过程的原因。我个人建议使用Picasso。要进行设置,只需将此行添加到应用的build.gradle文件中:

compile 'com.squareup.picasso:picasso:2.5.2'

然后你可以像这样使用它:

mStorageRedImagenes.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                        // Got the download URL for 'users/me/profile.png'
                        Log.d("PATH", ""+uri.getPath());

                        Log.d("DENTRO DE ONSUCCES", uri.getPath());

                        Picasso.with(getContext()).load(uri).into(miImagen);

                        producto.setMiImagen(miImagen);
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception exception) {
                        // Handle any errors
                    }

              });