从Firebase存储中检索图像URL并将其放置在警报对话框中

时间:2019-07-10 21:25:27

标签: android firebase firebase-realtime-database imageview

用户注册后,个人资料图片将保存到Firebase存储中。我想做的是从firebase的“用户/驱动程序”节点中检索它(配置文件图片),并将其放在AlertDialog中的imageView中。

此代码没有执行应做的操作有什么问题?

DatabaseReference driverArrivedRef = rootRef.child("Users/Drivers").child(driverId);
    driverArrivedRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            // get driver profile pic from database 'Users/Drivers'
            for (DataSnapshot ss : snapshot.getChildren()) {

                String imageUrl = ss.child("photoUrl").toString();

                DialogInterface.OnClickListener dialogClickListener = new DialogInterface
                        .OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case DialogInterface.BUTTON_POSITIVE:
                                //Yes button clicked
                                Toast.makeText(RiderHome.this, "Confirmed!",
                                        Toast.LENGTH_SHORT).show();
                                isRiderInVehicle();
                                dialog.dismiss();
                                break;

                            case DialogInterface.BUTTON_NEGATIVE:
                                //No button clicked
                                Toast.makeText(RiderHome.this, "Not Confirmed, Try Again.",
                                        Toast.LENGTH_SHORT).show();
                                dialog.cancel();
                                break;
                        }
                    }
                };

                AlertDialog.Builder builder = new AlertDialog.Builder(RiderHome.this);
                getImageBitmapFromURL(context, imageUrl); // code below
                builder.setTitle("Driver Has Arrived")
                        .setMessage("Please proceed to vehicle.")
                        .setPositiveButton("CONFIRM", dialogClickListener)
                        .setNegativeButton("CANCEL", dialogClickListener);

                AlertDialog alert = builder.create();
                alert.getWindow().setGravity(Gravity.CENTER);
                alert.show();
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

getImageBitmapFromURL()

public static Bitmap getImageBitmapFromURL(final Context context, final String imageUrl){
    Log.e(TAG, "getImageBitmapFromURL");

    Bitmap imageBitmap = null;
    try {
        imageBitmap = new AsyncTask<Void, Void, Bitmap>() {
            @Override
            protected Bitmap doInBackground(Void... params) {
                try {
                    int targetHeight = 200;
                    int targetWidth = 200;

                    return Picasso.with(context).load(String.valueOf(imageUrl))
                            //.resize(targetWidth, targetHeight)
                            .placeholder(R.drawable.ic_default_user)
                            .get();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute().get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
    return imageBitmap;
}

Firebase-用户/驱动程序节点

Firebase - Users/Drivers node

1 个答案:

答案 0 :(得分:2)

您正在遍历该驱动程序特定节点的所有子级。完全不需要,您可以使用直接获取图片网址,

String imageUrl = "";
if(snapshot.child("photoUrl").exists()) {
  imageUrl = snapshot.child("photoUrl").getValue().toString();
}

因此,onDataChange方法中的最终代码如下所示,

    @Override
    public void onDataChange(DataSnapshot snapshot) {

            String imageUrl = "";
            If(snapshot.child("photoUrl").exists()) {
              imageUrl = snapshot.child("photoUrl").getValue().toString();
            }

            DialogInterface.OnClickListener dialogClickListener = new DialogInterface
                    .OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which) {
                        case DialogInterface.BUTTON_POSITIVE:
                            //Yes button clicked
                            Toast.makeText(RiderHome.this, "Confirmed!",
                                    Toast.LENGTH_SHORT).show();
                            isRiderInVehicle();
                            dialog.dismiss();
                            break;

                        case DialogInterface.BUTTON_NEGATIVE:
                            //No button clicked
                            Toast.makeText(RiderHome.this, "Not Confirmed, Try Again.",
                                    Toast.LENGTH_SHORT).show();
                            dialog.cancel();
                            break;
                    }
                }
            };

            AlertDialog.Builder builder = new AlertDialog.Builder(RiderHome.this);
            getImageBitmapFromURL(context, imageUrl); // code below
            builder.setTitle("Driver Has Arrived")
                    .setMessage("Please proceed to vehicle.")
                    .setPositiveButton("CONFIRM", dialogClickListener)
                    .setNegativeButton("CANCEL", dialogClickListener);

            AlertDialog alert = builder.create();
            alert.getWindow().setGravity(Gravity.CENTER);
            alert.show();
    }

通过方式从imageURL获取位图的调用是异步的。因此,您需要等到该代码返回位图,然后在对话框中使用imageView设置该位图。