-1

Not all images are displaying when running my app. I am getting from json this result

{"result":[{"id":"1","name":null,"path":"http://api.androidhive.info/json/movies/1.jpg"},{"id":"2","name":null,"path":"http://www.justedhak.comlu.com/images/uploaded_images.jpg"},{"id":"32","name":null,"path":"http://www.justedhak.comlu.com/images/uploaded_images.jpg"},{"id":"31","name":null,"path":"http://www.justedhak.comlu.com/images/uploaded_images.jpg"},{"id":"30","name":null,"path":"http://www.justedhak.comlu.com/images/uploaded_images.jpg"},{"id":"29","name":null,"path":"http://www.justedhak.comlu.com/images/uploaded_images.jpg"}]}

the first 2 url images are displaying correctly in the app however the rest of the url are not displaying

these are working

[{"id":"1","name":null,"path":"http:\api.androidhive.info\json\movies\1.jpg"},{"id":"2","name":null,"path":"http:\justedhak.comlu.com\images\uploaded_images.jpg"}

these is my code of reading the image

//showlist() is under asynctask  prePostExecute
protected void showList(){
    try {
        JSONObject jsonObj = new JSONObject(myJSON);
        peoples = jsonObj.getJSONArray(TAG_RESULTS);

        for(int i=0;i<peoples.length();i++){
            JSONObject c = peoples.getJSONObject(i);
            String id = c.getString(TAG_ID);
            String url = c.getString(TAG_PATH); 
            Listitem.add(new Listitem(id,url));
        }

        GridViewAdapter adapter = new GridViewAdapter(this, R.layout.grid_item_layout, Listitem);
     //   gridView.setAdapter(gridAdapter); 

       list.setAdapter(adapter);

    } catch (JSONException e) {
        e.printStackTrace();
    }

}
public class GetDataJSON extends AsyncTask<String, Void, String>{
     @Override
        protected String doInBackground(String... params) {
            DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
            HttpPost httppost = new HttpPost("http://justedhak.comlu.com/get-data.php");

            // Depends on your web service
            httppost.setHeader("Content-type", "application/json");

            InputStream inputStream = null;
            String result = null;
            try {
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();

                inputStream = entity.getContent();
                // json is UTF-8 by default
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();

                String line = null;
                while ((line = reader.readLine()) != null)
                {
                    sb.append(line + "\n");
                }
                result = sb.toString();
            } catch (Exception e) {
                // Oops
            }
            finally {
                try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
            }
            return result;
        }

I guess my error is here grid view adapter

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder;

if (row == null) {
LayoutInflater inflater = LayoutInflater.from(mcontext);
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.imageTitle = (TextView) row.findViewById(R.id.text);
holder.imageView = (ImageView) row.findViewById(R.id.imageView);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
Listitem item = getItem(position);
System.out.println(item.getUrl());

holder.imageTitle.setText(item.getId());
Picasso.
with(mcontext).
load(item.getUrl())
.placeholder(R.drawable.ic_launcher)
.fit()
.into(holder.imageView);

return row;
}

static class ViewHolder {
TextView imageTitle;
ImageView imageView;
}
}

upload

public void upload()
{
      Calendar thisCal = Calendar.getInstance();
      thisCal.getTimeInMillis();

      //  android.util.Log.i("Time Class ", " Time value in millisecinds "+ thisCal);

   // Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);  
 //   ByteArrayOutputStream stream = new ByteArrayOutputStream();
   // bmp.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.

        Intent intent = getIntent();
        String selectedImage= intent.getStringExtra("imagePath");
        Uri fileUri = Uri.parse(selectedImage);

   // Uri selectedImage = intent.getData();
    System.out.println(fileUri);
    InputStream imageStream = null;
    try {
        imageStream = getContentResolver().openInputStream(fileUri);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    Bitmap bmp = BitmapFactory.decodeStream(imageStream);

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 30, stream);


    byte[] byteArray = stream.toByteArray();
    Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    imageview.setImageBitmap(bitmap);
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    System.out.println(width);
    System.out.println(height);


    getResizedBitmap( bitmap, 200);
    try {
        stream.close();
        stream = null;
    } catch (IOException e) {

        e.printStackTrace();
    }

    String image_str = Base64.encodeBytes(byteArray);
    final ArrayList<NameValuePair> nameValuePairs = new  ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("image",image_str));
    nameValuePairs.add(new BasicNameValuePair("caption",caption));
    nameValuePairs.add(new BasicNameValuePair("name","je"));
    nameValuePairs.add(new BasicNameValuePair("categorie",categorie));
     Thread t = new Thread(new Runnable() {

    @Override
    public void run() {
          try{

                 HttpClient httpclient = new DefaultHttpClient();
                 HttpPost httppost = new HttpPost("http://justedhak.comlu.com/images/upload_image.php");
                 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                 HttpResponse response = httpclient.execute(httppost);
                 final String the_string_response = convertResponseToString(response);
                 runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            Toast.makeText(AddImage.this, "Response " + the_string_response, Toast.LENGTH_LONG).show();                         
                        }
                    });

             }catch(final Exception e){
                  runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        Toast.makeText(AddImage.this, "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();                             
                    }
                });
                   System.out.println("Error in http connection "+e.toString());
             }  
    }
});
 t.start();
}
Moudiz
  • 7,211
  • 22
  • 78
  • 156
  • do you ever tried to call your image in browser ? – Sree Oct 05 '15 at 06:04
  • your question is unclear, what is the problem? did you get any error? how you show images? did item add to your list? – Shayan Pourvatan Oct 05 '15 at 06:05
  • http:\justedhak.comlu.com\images\uploaded_images.jpg is not loading in browser. how are you expecting it to load in mobile? Check if the image exists on server and the path is correct. – Farooq Arshed Oct 05 '15 at 06:06
  • Ofcourse .the url images are identical one them are shown the otjer are not – Moudiz Oct 05 '15 at 06:06
  • Is the result from `http://justedhak.comlu.com/get-data.php` ? IMO, you should post the code that loading the image Url into the ImageView, for example – BNK Oct 05 '15 at 06:08
  • your URLs are wrong Http:\justedhak.comlu.com\images\uploaded_images.jpg – e4c5 Oct 05 '15 at 06:08
  • your image url not working in browser – Aditya Vyas-Lakhan Oct 05 '15 at 06:08
  • @Lakhan yes check please http://www.justedhak.comlu.com/images/uploaded_images.jpg – Moudiz Oct 05 '15 at 06:11
  • @e4c5 also please check http://www.justedhak.comlu.com/images/uploaded_images.jpg – Moudiz Oct 05 '15 at 06:12
  • @bnk yes now I am accessed to my lap ill post the adapter I guess problem in it – Moudiz Oct 05 '15 at 06:13
  • @FarooqArshed yes the url is working now check please. I am loading this image identical in the app one time this shown others are not – Moudiz Oct 05 '15 at 06:13
  • 1
    @BNK you there ? did you check code ? – Moudiz Oct 05 '15 at 06:23
  • further to my previous comment note that your links are having '\' instead of '/' – e4c5 Oct 05 '15 at 06:25
  • @e4c5 I edit my question , I copied the url wrong. – Moudiz Oct 05 '15 at 06:30
  • Now I'm a little confused about: _"I guess my error is here grid view adapter"_ . Have you tried to load a images without the Adapter? E.g in a Imageview? Could you test that? Just to be sure it is not a matter of view reusing/recycling. It's hard to help at this point I guess. – daemmie Oct 05 '15 at 06:42
  • @oberflansch without the adapter the url image is loading. if you have any question tell me – Moudiz Oct 05 '15 at 06:46
  • Hmm well. We have got our own fuctions to cache and load image, so we don't use Picasso. I guess I'm not the right person to help you at this point. I would try to use an other load logic like [this](http://stackoverflow.com/questions/5776851/load-image-from-url) and remove the Viewholder (inflate every time). That might help to find the problem. – daemmie Oct 05 '15 at 06:55
  • @oberflansch can you please explain how to inflate everytime ? by the was thanks for your support – Moudiz Oct 05 '15 at 07:07
  • public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; LayoutInflater inflater = LayoutInflater.from(mcontext); View row = inflater.inflate(layoutResourceId, parent, false); return row; } – daemmie Oct 05 '15 at 07:09
  • @oberflansch sorry I didnt understand can you explain please – Moudiz Oct 05 '15 at 07:13
  • This is slow and somehow dirty. But create a new view every time, to ensure that one image is loaded for one view. – daemmie Oct 05 '15 at 07:13
  • As I said. Picasso might cause some problems but I am NOT sure. – daemmie Oct 05 '15 at 07:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91358/discussion-between-oberflansch-and-moudiz). – daemmie Oct 05 '15 at 07:19
  • Will you post your full code please – KishuDroid Oct 05 '15 at 09:14
  • @KishuDroid I did post all my code what you still y need – Moudiz Oct 05 '15 at 09:17
  • How you convert url to bitmap? – KishuDroid Oct 05 '15 at 09:18
  • @KishuDroid check my edit please . I dont think the convert is related becasue I am able to see the image in the app. I am sending same url to list image view one of them is displaying the otheres are not – Moudiz Oct 05 '15 at 09:31
  • I am not familiar with GridView, pls post your screenshot for more clarification – BNK Oct 05 '15 at 09:55

3 Answers3

4

Getting reference for GridView sample from here, I have just customized and tested loading all your images with it.

Item.java:

public class Item {
    String imageUrl;
    String title;
    public Item(String imageUrl, String title) {
        super();
        this.imageUrl = imageUrl;
        this.title = title;
    }
    public String getImageUrl() {
        return imageUrl;
    }
    public String getTitle() {
        return title;
    }
}

CustomGridViewAdapter.java:

public class CustomGridViewAdapter extends ArrayAdapter<Item> {
    Context context;
    int layoutResourceId;
    ArrayList<Item> data = new ArrayList<>();
    public CustomGridViewAdapter(Context context, int layoutResourceId,
                                 ArrayList<Item> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        RecordHolder holder;
        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new RecordHolder();
            holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
            holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
            row.setTag(holder);
        } else {
            holder = (RecordHolder) row.getTag();
        }
        Item item = data.get(position);
        holder.txtTitle.setText(item.getTitle());
        Picasso.with(context).load(item.getImageUrl()).into(holder.imageItem);
        return row;
    }
    static class RecordHolder {
        TextView txtTitle;
        ImageView imageItem;
    }
}

And MainActivity.java:

customGridAdapter = new CustomGridViewAdapter(this, R.layout.row_grid, gridArray);
gridView.setAdapter(customGridAdapter);
String url = "http://justedhak.comlu.com/get-data.php";
RequestQueue queue = Volley.newRequestQueue(mContext);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                if (response != null && !response.isNull("result")) {
                    try {
                        JSONArray jsonArray = response.getJSONArray("result");
                        if (jsonArray != null && jsonArray.length() > 0) {
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObject = jsonArray.getJSONObject(i);
                                if (jsonObject != null && !jsonObject.isNull("path")) {
                                    String imagePath = jsonObject.getString("path");
                                    if (imagePath != null && !imagePath.isEmpty()) {
                                        gridArray.add(new Item(imagePath,"BNK"));
                                    }
                                }
                            }
                            customGridAdapter.notifyDataSetChanged();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("VOLLEY", error.toString());
            }
        });
queue.add(jsonObjectRequest);

Other files such as layout... I think you know already

Here is the screenshot

BNK's screenshot

BNK
  • 23,994
  • 8
  • 77
  • 87
  • 1
    yes the problem was with duplicate links its a bug in picasso, if you execute a image url at the same time , the first image will shown, the rest will not . however thanks for asnwerting me – Moudiz Oct 06 '15 at 12:44
  • You mean because of Picasso? I don't think so, tomorrow I will test with same image url :-) – BNK Oct 06 '15 at 12:50
  • is this code still running at your side ? if yes give me 5 min and ill make all the url image the same and lets see if they will be shown – Moudiz Oct 06 '15 at 12:51
  • 1
    I am on mobile now, no PC to test – BNK Oct 06 '15 at 12:56
  • I have just tested by hard-code `gridArray.add(new Item("http://justedhak.comlu.com/images/uploaded_images.jpg", "BNK"));` the app still works normally, the same image displayed in all cell of gridview :) – BNK Oct 07 '15 at 01:06
  • Please check `http://justedhak.comlu.com/images/uploaded_images56140f0e0e94e.png`, this image not completed displayed (you can test in brower), so it caused IOException with Picasso – BNK Oct 07 '15 at 01:30
  • `Picasso.Builder builder = new Picasso.Builder(getContext()); builder.listener(new Picasso.Listener() { @Override public void onImageLoadFailed(Picasso picasso, Uri uri, Exception e) { Log.e("Picasso Error", uri.toString() + "\r\n" + e.toString()); } }); Picasso picasso = builder.build(); picasso.load(item.getImageUrl()) .placeholder(R.drawable.ic_action_android) .into(holder.imageItem);` – BNK Oct 07 '15 at 01:31
  • good morning man ( from my country:P) yes it is true this image wasent upload succefuly this that last code will check if the image is fail right ? – Moudiz Oct 07 '15 at 06:20
  • It will be callled (here I just log) if image failed load :) – BNK Oct 07 '15 at 06:25
  • uhmm later on i will search for a way to check if was upload succefuly or not . Mean while can you please assist me in this question ? if you have any question ask me there http://stackoverflow.com/questions/32983925/how-to-fix-the-inflation-another-r-layout – Moudiz Oct 07 '15 at 06:26
2

As per my study It's a bug that is reported to be fixed in the next release of the lib.

You can clone the repo of the lib and compile your own jar or wait.

I recommend you to take a look at Glide. Migrating from Picasso is quite trivial, it has better performance and it makes a nice smooth scrolling on lists.

Or try to remove .fit() from picasso.

Picasso.
with(mcontext).
load(item.getUrl())
.placeholder(R.drawable.ic_launcher)

.into(holder.imageView);

Hope it will help you.

KishuDroid
  • 5,411
  • 4
  • 30
  • 47
1

You should normalize your Uri cause this seems to be to problem here.

Take a look at normalizeScheme() of the Uri class. It fixes such problems regargng to upper/lowercase letters.

It will convert:

Http:\justedhak.comlu.com\images\uploaded_images.jpg

to

http:\justedhak.comlu.com\images\uploaded_images.jpg

If you want some more details you can take a look at this RFC 2396

Scheme names consist of a sequence of characters beginning with a lower case letter and followed by any combination of lower case letters[...]

daemmie
  • 6,361
  • 3
  • 29
  • 45
  • you are saying it should be lower letter ? like `http` ? – Moudiz Oct 05 '15 at 06:16
  • Exactly. I'm not familiar with all the Uri rules, but you can find everything in the provided link. There might be some other problems with picasso, but using a not wellformatted Uri is usually a bad idea. – daemmie Oct 05 '15 at 06:19