Search code examples
javaandroidandroid-imageview

Loading an iImage in a custom DialogBox from a URL with Picasso


I have created a method where I can load an image from Drawable folder which is working fine but when I was trying load image from URL and set it on my custom layout's Imageview, it returns just:

java.lang.IllegalArgumentException: Target must not be null.

My Java Code is:

public void CustomDialogLoadImage() {
    Dialog dialog = new Dialog(this);
    ImageView FF_ReceiptImage = (ImageView) dialog.findViewById(R.id.FF_ReceiptImage);
    Picasso.with(ConveyanceAmtUpdateApprove.this).load("http://i.imgur.com/DvpvklR.png").into(FF_ReceiptImage);
    dialog.setContentView(R.layout.custom_dialogbox_image);
    dialog.show(); 
}

My Custom Dialog Box is:

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/FF_ReceiptImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:src="@drawable/mobile_sales_img"
        android:layout_gravity="center"
        android:background="@drawable/border"/>

</LinearLayout>

I Have tried may way to solved this but it is Resolved Please Help Me...


Solution

  • After i check it one more time, i see weird things in 4th line.

    Try this

    public void CustomDialogLoadImage() {
        Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.custom_dialogbox_image);
        ImageView FF_ReceiptImage = (ImageView) dialog.findViewById(R.id.FF_ReceiptImage);
        Picasso.with(ConveyanceAmtUpdateApprove.this).load("http://i.imgur.com/DvpvklR.png").into(FF_ReceiptImage);
    
        dialog.show(); 
    }
    

    Or you have to do it inside View which is it already inflate first, this is sample code

    public class ImageDialog extends DialogFragment {
    
        public static final String ARG_IMAGE_URI = "arg_image_uri";
        public static final String ARG_IMAGE_NAME = "arg_image_name";
        public static final String ARG_IMAGE_ID_CHANNEL = "arg_image_id_channel";
    
        public ImageDialog(){ // if no argument you can use empty constructor }
        public static ImageDialog getInstance(String uriStr, String imageName, int idChannel){
    
            ImageDialog imageDialog = new ImageDialog();
            Bundle bundle = new Bundle();
            bundle.putString(ARG_IMAGE_URI, uriStr);
            bundle.putString(ARG_IMAGE_NAME, imageName);
            bundle.putInt(ARG_IMAGE_ID_CHANNEL, idChannel);
            imageDialog.setArguments(bundle);
            return imageDialog;
        }
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
           // inflate first with your own layout dialog 
           return inflater.inflate(R.layout.popup_image_dialog, container, false);
        }
    
        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
    
            // Look this part
            ImageView imageView = view.findViewById(R.id.popup_image);
            //-------------
            Bundle bundle = getArguments();
            if(bundle != null){
                String name = bundle.getString(ARG_IMAGE_NAME);
                getDialog().setTitle(name);
                String imageUri = bundle.getString(ARG_IMAGE_URI);
                int idChannel = bundle.getInt(ARG_IMAGE_ID_CHANNEL);
                if(idChannel == 0){
                    Picasso.with(getContext())
                            .load(new File(imageUri))
                            .into(imageView);
    
                }else {
                    Picasso.with(getContext())
                            .load(imageUri)
                            .into(imageView);
                }
    
            }
    
        }
    }
    

    And to call this dialog perform this action

    ImageDialog dialog = ImageDialog.getInstance(item.getImageUrl(), item.getName(), 1);
                    dialog.show(fragmentManager, null);