簡體   English   中英

Android Studio 在警報對話框中顯示圖像

[英]Android Studio Display Image in Alert Dialog

我在 firebase 實時數據庫中有一個圖像存儲作為 url。 我正在嘗試下載圖像並在警報對話框消息中顯示圖像。 執行代碼時會出現警告對話框,但不顯示圖像。

在此處輸入圖片說明

圖像下載器類:

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() 調用顯示的警報對話框,這將完美運行。

你已經為此創建了一個布局,但你沒有在任何地方使用它我認為你是新手,無論你在做什么,都可以通過使用 Dialog 而不是使用 AlertDialog 來完成這里我給你一個例子:

            // Create a custom dialog object
            final Dialog dialog = new Dialog(getContext());
            // Include dialog.xml file
            dialog.setContentView(R.layout.your_dialg_layout);
            // Set dialog title
            dialog.setTitle("Custom Dialog");

            // set values for custom dialog components - text, image or button

            ImageView image = dialog.findViewById(R.id.your_imageview_id);
           // image.setImageResource(R.drawable.image0);

現在,我建議您不要創建 AsyncTask,而是使用 Glide 或 Picasso(在 ImageView 中加載 URL 的庫),並且您還必須在對話框布局中創建一個關閉按鈕

            dialog.show();

            Button declineButton = dialog.findViewById(R.id.your_dialog_button);
            // if decline button is clicked, close the custom dialog
            declineButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Close dialog
                    dialog.dismiss();
                }
            });

快樂編碼:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM