У меня есть хранилище изображений в базе данных Firebase в реальном времени в качестве URL. Я пытаюсь загрузить изображение и отобразить его в диалоговом сообщении с предупреждением. При исключении кода появляется диалоговое окно с предупреждением, но изображение не отображается.
![enter image description here](https://i.stack.imgur.com/Nsbda.png)
ImageDownloader Class:
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());
}
});
}
});
Кто-нибудь может помочь? Спасибо.