java - Android Studio 在警报对话框中显示图像

标签 java android firebase imagedownload

我在 firebase 实时数据库中有一个图像存储作为 url。我正在尝试下载图像并在警报对话框消息中显示图像。执行代码时会出现警报对话框,但不显示图像。

enter image description here

ImageDownloader 类:

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;

import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.URL;


public class ImageDownload extends AsyncTask<String, Void, Bitmap>

{
    private final WeakReference<ImageView> imageViewWeakReference;

    public ImageDownload(ImageView imageView)
    {
        imageViewWeakReference = new WeakReference<>(imageView);
    }

    @Override
    protected Bitmap doInBackground(String... string)
    {
        return downloadBitmap(string[0]);
    }

    @Override
    protected void onPostExecute(Bitmap bitmap)
    {
        if(isCancelled())
        {
            bitmap = null ;
        }

        ImageView imageView = imageViewWeakReference.get();
        if (imageView != null)
        {
            if (bitmap != null)
            {
                imageView.setImageBitmap(bitmap);
            }
        }
    }

    private Bitmap downloadBitmap(String url)
    {
        HttpURLConnection connection = null;

        try
        {
            URL url1 = new URL(url);
            connection = (HttpURLConnection) url1.openConnection();
            int StatusCode = connection.getResponseCode();

            if(StatusCode != connection.HTTP_OK)
            {
                return null;
            }

            InputStream inputStream = connection.getInputStream();
            if(inputStream != null)
            {
                return BitmapFactory.decodeStream(inputStream);

            }
        }
        catch (Exception e)
        {
            connection.disconnect();
        }
        finally
        {
            if(connection != null)
            {
                connection.disconnect();
            }
        }
        return null;
    }
}

我为警报对话框创建了一个布局 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageView
        android:id="@+id/dialog_imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
  btVoucher1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                databaseReference.addValueEventListener(new ValueEventListener() {

                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot)
                    {
                        String qrcode = dataSnapshot.child("Voucher").child("1").getValue().toString();

                        Toast.makeText(getActivity(), qrcode, Toast.LENGTH_LONG).show();
                        AlertDialog.Builder alertVoucher1 = new AlertDialog.Builder(getContext());
                        alertVoucher1.setNeutralButton("Close",
                                new DialogInterface.OnClickListener()
                                {
                                    public void onClick(DialogInterface dialog, int id)
                                    {
                                        dialog.cancel();

                                    }
                                });

                        new ImageDownload(qrCodeVoucher).execute(qrcode);
                        AlertDialog voucher = alertVoucher1.create();
                        voucher.show();

                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError)
                    {
                        System.out.println("The read failed: " + databaseError.getCode());
                    }
                });
            }
        });

有人可以提供帮助吗?谢谢。

最佳答案

在这里,我可以看到您正在异步任务上并行加载图像,这可能需要一些时间来下载图像,在此之前您已经调用了警报对话框。因此,在图像出现之前会显示警报。

解决方案:您可以调用 asyncTask 的 postExecute() 显示的警报对话框,这将完美工作。

关于java - Android Studio 在警报对话框中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60653992/

相关文章:

java - 有没有办法在嵌入式 jetty 中以编程方式设置上下文参数?

android - 保存的图像需要一段时间才能出现在图库中

安卓 native SIP DTMF

javascript - 无法启用电子邮件/密码身份验证提供商

android - Firebase Cloud Messaging android 项目不发送推送通知

java - Android 应用程序可以在模拟器上正常运行,但在 Android 手机上运行时速度非常慢

java - 如何将字符串转换为整数以用于 Velocity 中的循环?

android - 是否存在监听 RecyclerView 项目更改的现有方法?

swift - 循环遍历 Firebase 数据库以在 Swift 中构建表列表

java - 没有为 OPTIONS/DELETE 正确启用 Spring Boot Data Rest + CORS