EN perumal EN perumal - 5 months ago 15
Android Question

My Json array contains two images, How to download that through url/http and displays in activity

My json array contains two images, how to display that in activity? Those images are in the form of url. After and before are the imageviews, I want to know what is image download task and how can I use that.

after = (ImageView) findViewById(R.id.bfrdoogret);
before = (ImageView) findViewById(R.id.afterdogret);
}

class BackGround extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = ProgressDialog.show(Serialno.this, "",
"Searching. Please wait...", true);
dialog.setCancelable(true);
dialog.show();
}

@Override
protected String doInBackground(String... params)
{
String values=getvalues();
return values;
}



@Override
protected void onPostExecute(String s)
{
dialog.hide();
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
dis.setText(district);
city.setText(cit);
serialno.setText(serial_no);
gender.setText(gende);

loction.setText(latitude+ longitude);


}
}

public String getvalues()
{
String uri = "http://www.lorryguru.com/domains/dogs/retusingsno.php?district="+District+"&serialno="+SerialNo;
HttpGet httpGet = new HttpGet(uri);
HttpClient client = new DefaultHttpClient();
HttpResponse response;
StringBuilder stringBuilder = new StringBuilder();

try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1)
{
stringBuilder.append((char) b);
}
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
JSONArray arr = new JSONArray(stringBuilder.toString());
//result = jsonObj.getJSONArray(TAG_RESULT);
JSONObject c = arr.getJSONObject(0);
id = c.getString(TAG_ID);
district = c.getString(TAG_DISTRICT);
cit = c.getString(TAG_CITY);
serial_no = c.getString(TAG_SERIALNO);
gende = c.getString(TAG_GENDER);
wareno = c.getString(TAG_WARENO);
befor = c.getString(TAG_BEFORE);
afte = c.getString(TAG_AFTER);
latitude = c.getString(TAG_LATITUDE);
longitude = c.getString(TAG_LONGITUDE);
return "success";
}
catch (JSONException e)
{
return "accessproblem";
//e.printStackTrace();
}

Answer

You have to add another async task after to your existing one, Code is following below.

 class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap>
{
    private final WeakReference<ImageView> imageViewReference;
    String murl;
    ImageView showView;
    public ImageDownloaderTask(ImageView imageView,String url) {
        imageViewReference = new WeakReference<ImageView>(imageView);
        murl=url;
        showView=imageView;
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        final Bitmap bitmap=downloadBitmap(murl);
        if(murl!=null && bitmap!=null)
        {
            addBitmapToMemoryCache(murl, bitmap);
        }
        return bitmap;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (isCancelled()) {
            bitmap = null;
        }

        if (imageViewReference != null) {
            final ImageView imageView = imageViewReference.get();
            if (imageView != null) {

                if (bitmap != null) {
                    imageView.setImageBitmap(bitmap);
                    //addBitmapToMemoryCache(murl, bitmap);
                }

            }

        }
    }
}

private Bitmap downloadBitmap(String url)
{
    HttpURLConnection urlConnection = null;
    Bitmap bitmap;
    try {
        URL uri = new URL(url);
        urlConnection = (HttpURLConnection) uri.openConnection();
        int statusCode = urlConnection.getResponseCode();
        if (statusCode != HttpStatus.SC_OK)
        {
            return null;
        }
        InputStream inputStream = urlConnection.getInputStream();

        bitmap = BitmapFactory.decodeStream(inputStream);


        return bitmap;
    }

    catch (Exception e)
    {
        urlConnection.disconnect();
        Log.w("ImageDownloader", "Error downloading image from " + url);
    }
    finally
    {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
    return null;
}
Comments