Search code examples
androidimageviewandroid-volleyandroid-alertdialog

How to display downloaded image into alert dialog correctly not based on the size?


what I am trying to do is show some images I download from an url. I managed to download and show the images, but I can not figure out how to show the correctly based on the size, by size I mean hight and width not kb.

I am using volley to download the images. The alert dialog is shown after a click within a fragment view.

This is the layout

    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <ImageView
            android:id="@+id/imgProduct"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

And this is how I show the alert dialog

    LayoutInflater layoutInflater = (LayoutInflater)getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View imageDialog = layoutInflater.inflate(R.layout.popup_product_image, null);
    Dialog dialog = new Dialog(context);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(imageDialog);
    dialog.setCanceledOnTouchOutside(true);
    //dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

    ImageView imgImage = imageDialog.findViewById(R.id.imgProduct);

    String url = product.urlImage.replaceAll("\\s+","");

    ImageRequest imgRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
        @Override
        public void onResponse(Bitmap response) {
            imgImage.setImageBitmap(response);
            imgImage.startAnimation(AnimationUtils.loadAnimation(context, android.R.anim.fade_in));
        }
    }, 0, 0, ImageView.ScaleType.CENTER_CROP, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            imgImage.setImageResource(RES_ERROR);
            Toast.makeText(context, VolleyManager.getInstance(context)
                    .checkError(error), Toast.LENGTH_LONG).show();
        }
    });
    imgRequest.setTag(CANCELABLE_REQUEST_TAG);
    VolleyManager.getInstance(context).getRequestQ().add(imgRequest);

    // Set your image

    dialog.show();
    dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

Any help or suggestions would be great, thanks


Solution

  • As the comment above, use Piccaso. Try this and modify from "ImageView imgImage = imageDialog.findViewById(R.id.imgProduct);"

    Target target = new Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                    imgImage.setImageBitmap(bitmap);
                    doFoo();
                }
    
            @Override
            public void onBitmapFailed(Drawable errorDrawable) {
                imgImage.setImageDrawable(errorDrawable);
                Toast.makeText(context, "download failed", Toast.LENGTH_SHORT).show();
                doBar();
            }
    
            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {
                imgImage.setImageDrawable(placeHolderDrawable);
            }
        };
    
        //this will help us to avoid the Target being gc'd
        imgImage.setTag(target);
    
        Picasso.with(context)
                .load(url)
                .placeholder(RES_PLACEHOLDER)
                .error(RES_ERROR)
                .resize(400, 400)
                .into(target);
    
        // Set your image
    
        dialog.show();
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    

    and then used dummy functions

    private void doFoo() {
        //dummy
    }
    private void doBar() {
        //dummy
    }