Question

i am having an issue with populating the image in imageview from the url i used bitmap it hasn't worked i used output statement to check the url in logcat the url id displaying fine but when it comes to bitmap it is not populating the image in url dynamically from mysql database

public class StoreHomeFragment extends Fragment {
View rootView;
ImageView imageview;
final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

rootView = inflater.inflate(R.layout.store_home, container, false); 
return rootView;
}



@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            String url = "http://192.168.1.132/Android/App/good.php";

            try {
                JSONArray data = new JSONArray(getJSONUrl(url));

                HashMap<String, String> map;

                for (int i = 0; i < data.length(); i++) {
                    JSONObject c = data.getJSONObject(i);

                    map = new HashMap<String, String>();
                    map.put("name", c.getString("name"));
                    map.put("artist", c.getString("artist"));
                    map.put("price", c.getString("price"));
                    map.put("image", c.getString("image"));

                    MyArrList.add(map);
                }

            } catch (JSONException e) {

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

                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            final GridView gridView1 = (GridView) rootView.findViewById(R.id.store_home_gridview);
            gridView1.setAdapter(new ImageAdapter(getActivity(), MyArrList));

        }
    }.execute();}      

public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {  //not working
ImageView bmImage;

  public DownloadImageTask(ImageView bmImage) {
      this.bmImage = bmImage;
  }

  protected Bitmap doInBackground(String... urls) {
      String urldisplay = urls[0];
      Bitmap mIcon11 = null;
      try {
        InputStream in = new java.net.URL(urldisplay).openStream();
        mIcon11 = BitmapFactory.decodeStream(in);
      } catch (Exception e) {
          bmImage.setImageResource(android.R.drawable.ic_menu_report_image);
      }
      return mIcon11;
  }

  protected void onPostExecute(Bitmap res) {
        bmImage.setImageBitmap(res);
  }
}

class ImageAdapter extends BaseAdapter {
private Context context;
public ImageView imageView;
private ArrayList<HashMap<String, String>> MyArr = new ArrayList<HashMap<String, String>>();

public ImageAdapter(Context c,ArrayList<HashMap<String, String>> list) 
{   
    context = c;

    MyArr = list;
}

public int getCount() {

    return MyArr.size();
}

public Object getItem(int position) {

    return position;
}

public long getItemId(int position) {

    return position;
}
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.store_home_gridview_row, null); 
    }

    TextView tv_title = (TextView) convertView.findViewById(R.id.textview_name);
    tv_title.setText("Title:"+MyArr.get(position).get("name"));

    TextView tv_artist = (TextView) convertView.findViewById(R.id.textview_artist);
    tv_artist.setText("Artist:"+MyArr.get(position).get("artist"));

    TextView tv_duration = (TextView) convertView.findViewById(R.id.textview_price);
    tv_duration.setText("Price:"+MyArr.get(position).get("price"));

    String abc = (MyArr.get(position).get("image"));
    String abcd = "http://192.168.1.132/onlinestore/images/products/"+abc;
    System.out.println("abcd" +abcd);

    //i can fetch the correct url in logcat but when i give to url
    new DownloadImageTask((ImageView) convertView.findViewById(R.id.imageView1))
    .execute(abcd); //image is not printing if i give the url which is stored in abcd
} }

/* Get JSON Code from URL */

 public String getJSONUrl(String url) {
    StringBuilder str = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    try 
    {
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        System.out.println ( "status Code : " + statusCode );
        if (statusCode == 200) 
        { 
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) 
            {
                str.append(line);
            }
        }   
        else 
        {
            Log.e("Log", "Failed to download file..");
        }
    } 
    catch (ClientProtocolException e) 
    {
        e.printStackTrace();
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }
    System.out.println ( "str : " + str.toString() );
    return str.toString();
}}
Was it helpful?

Solution

Maybe you should use some cool library for your image task. I recommand you to use the cool async image library -------Picasso. With this library, you could get your image from web very easy. Such as:

Picasso.with(context).load("http://YOUR-IMAGE-URL").placeholder(YOUR-LOADING-IMAGE-_RESID).error(YOUR-ERROR-IMAGE-_RESID).into(imageView);`
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top