Anand
Anand

Reputation: 1959

Image displaying on imageview crashes app

I used glide for displaying image from url.When i used the debugger the url contains the image.But the problem is when ever the app run it closes.Is there any thing to do in code of imageview?Please help me to recover this problem.Forgive me if i asked blunder.Thanks in advance.My code is provided below.

    public class ProductImages extends AsyncTask<Void , Void, Void> {
        int status;StringBuilder sb;
        String strJson, postData;
        JSONArray jsonArray;
        String msg;
        boolean pass=false;
        ArrayList<String> imgurls=new ArrayList<>();
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            String url =getResources().getString(R.string.url) + "WebServices/WebService.asmx/ProductImages";
            HttpURLConnection c = null;
            try {
                postData = "{\"productID\":\"" + productID + "\",\"TID\":\"" + constants.TID + "\"}";
                String response=db.GetResponses(url,postData);
                if(!response.equals("")){
                    strJson=response;
                }
                else {
                    URL u = new URL(url);
                    c = (HttpURLConnection) u.openConnection();
                    c.setRequestMethod("POST");
                    c.setRequestProperty("Content-type", "application/json; charset=utf-16");
                    c.setRequestProperty("Content-length", Integer.toString(postData.length()));
                    c.setDoInput(true);
                    c.setDoOutput(true);
                    c.setUseCaches(false);
                    c.setConnectTimeout(10000);
                    c.setReadTimeout(10000);
                    DataOutputStream wr = new DataOutputStream(c.getOutputStream());
                    wr.writeBytes(postData);
                    wr.flush();
                    wr.close();
                    status = c.getResponseCode();
                    switch (status) {
                        case 200:
                        case 201: BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
                            sb = new StringBuilder();
                            String line;
                            while ((line = br.readLine()) != null) {
                                sb.append(line).append("\n");
                            }
                            br.close();
                            int a=sb.indexOf("[");
                            int b=sb.lastIndexOf("]");
                            strJson=sb.substring(a, b + 1);
                            //   strJson=cryptography.Decrypt(strJson);
                            strJson="{\"JSON\":" + strJson.replace("\\\"","\"").replace("\\\\","\\") + "}";
                    }
                }
            } catch (Exception ex) {
                Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
                msg=ex.getMessage();
            } finally {
                if (c != null) {
                    try {
                        c.disconnect();
                    } catch (Exception ex) {
                        Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
                        msg=ex.getMessage();
                    }
                }
            }
            if(strJson!=null)
            {try {
                JSONObject jsonRootObject = new JSONObject(strJson);
                jsonArray = jsonRootObject.optJSONArray("JSON");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    msg=jsonObject.optString("Message");
                    pass=jsonObject.optBoolean("Flag",true);
                    imgurls.add(getResources().getString(R.string.url) + jsonObject.optString("Image").substring((jsonObject.optString("Image")).indexOf("Media")));
                    if(jsonObject.optBoolean("IsMain")){
                        productImage=getResources().getString(R.string.url) + jsonObject.optString("Image").substring((jsonObject.optString("Image")).indexOf("Media"));

                    }
                }
                if(pass){
                    if(c!=null)
                        db.ResponsesSaving(url,postData,strJson,"null");

                }
            } catch (Exception ex) {
                msg=ex.getMessage();
            }}
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            if(!pass) {
                new AlertDialog.Builder(EnquiryItem.this).setIcon(android.R.drawable.ic_dialog_alert)//.setTitle("")
                        .setMessage(msg)
                        .setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        }).setCancelable(false).show();
            }
            else {

                for (int i=0;i<imgurls.size();i++) {
                    final int fi=i;

                    ImageView imageView = (ImageView) findViewById(R.id.image);

                    Glide.with(EnquiryItem.this).load(imgurls).into(imageView);



                }
            }
        }


**My image view**

  <ImageView
        android:id="@+id/image"
        android:layout_width="170dp"
        android:layout_height="170dp"
        android:layout_above="@+id/textView22"
        android:layout_alignLeft="@+id/textView22"
        android:layout_alignStart="@+id/textView22"
        android:layout_gravity="center"
        android:layout_marginBottom="16dp"
        android:background="@drawable/back"

Logcat

java.lang.IllegalArgumentException: Unknown type class java.util.ArrayList. You must provide a Model of a type for which there is a registered ModelLoader, if you are using a custom model, you must first call Glide#register with a ModelLoaderFactory for your custom model class
   at com.bumptech.glide.RequestManager.loadGeneric(RequestManager.java:629)
   at com.bumptech.glide.RequestManager.load(RequestManager.java:598)
   at com.tech.test.testapp.EnquiryItem$ProductImages.onPostExecute(EnquiryItem.java:524)
   at com.tech.test.testapp.EnquiryItem$ProductImages.onPostExecute(EnquiryItem.java:412)
   at android.os.AsyncTask.finish(AsyncTask.java:651)
   at android.os.AsyncTask.-wrap1(AsyncTask.java)
   at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:5417)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Upvotes: 1

Views: 132

Answers (1)

Asteroid
Asteroid

Reputation: 758

Glide need not have declare GlideModule in AndroidMinifest.xml. You just need to following steps:

  1. YourGlideModule extends AppGlideModule, you can override function applyOptions in the YourGlideModule class.

  2. You should make project in android studio -> build -> make project, it will generate the GlideApp class.

  3. Use the GlideApp such as

    GlideApp.with(this)
            .load(imagegUrl)
            .into(imageview)
    

Upvotes: 1

Related Questions