In the AsyncTask, I am getting a json with some of the values as images. Below is the sample of my json
{
"text": "some text",
"html1": "Hi, check out my fb profile picture at <img src = 'http:\/\/abc.com/image1.png'\/> ",
"html2": "Hi, check out my whatsapp profile picture at <img src = 'http:\/\/abc.com\/image1.png'\/> ",
.
.
.
}
In async task postExecute Method I have
( ( TextView ) findViewById( R.id.html1 ) ).setText( Html.fromHtml( ( String ) jsonObj.get( "html1" ), new ImageGetter()
{
@Override
public Drawable getDrawable( String source )
{
return loadImage( source );
}
}, null ) );
( ( TextView ) findViewById( R.id.html2) ).setText( Html.fromHtml( ( String ) jsonObj.get( "html2" ), new ImageGetter()
{
@Override
public Drawable getDrawable( String source )
{
return loadImage( source );
}
}, null ) );
and my loadImage method looks like below
private Drawable loadImage( String source )
{
Drawable drawable = null;
URL sourceURL;
try
{
sourceURL = new URL( source );
URLConnection urlConnection = sourceURL.openConnection();
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream( inputStream );
Bitmap bm = BitmapFactory.decodeStream( bufferedInputStream );
// convert Bitmap to Drawable
drawable = new BitmapDrawable( getResources(), bm );
drawable.setBounds( 0, 0, bm.getWidth(), bm.getHeight() );
}
catch ( MalformedURLException e )
{
e.printStackTrace();
}
catch ( IOException e )
{
e.printStackTrace();
}
return drawable;
}
I think the code is suffice but I am getting android.os.NetworkOnMainThreadException
I have added the internet permission in manifest.xml