I have an image being sent to me through a JSON string. I want to convert that string into an image in my android app and then display that image in my imageview.I have a problem, I am using Asynctask and this is my code in the doInBackground method:
protected Boolean doInBackground(final String... args){
JsonParser jsonParser = new JsonParser();
JSONArray json = jsonParser.getJSONFromUrl(url);
if(json!=null){
for (int i = 0; i < json.length(); i++){
try{
JSONObject c = json.getJSONObject(i);
String displayImageFromUrl = c.getString(imageUrl);
String clearUrl = displayImageFromUrl.substring(displayImageFromUrl.indexOf(",")+1);
byte[] decodingString = Base64.decode(clearUrl, Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(decodingString, 0 , decodingString.length);
String showCreatedDate = c.getString(createdDate);
String showArticleTitle = c.getString(articleTitle);
HashMap<String, String> map = new HashMap<String, String>();
map.put(createdDate, showCreatedDate);
map.put(articleTitle, showArticleTitle);
jsonlist.add(map);
}catch (Exception e){
e.printStackTrace();
}
}
}else{
}
return null;
}
and this is my code in the onPostExecute() method:
protected void onPostExecute(final Boolean success){
if (dialog.isShowing()) {
dialog.dismiss();
}
Log.d("image please",bitmap.toString());
ImageView showImage = (ImageView) findViewById(R.id.imageShow);
showImage.setImageBitmap(bitmap);
ListAdapter adapter = new SimpleAdapter(context, jsonlist, R.layout.activity_news,
new String[] {createdDate, articleTitle},
new int[] { R.id.createDate, R.id.articleTitle});
setListAdapter(adapter);
lv = getListView();
}
Unfortunately nothings shows up in my logcat. Can you help me?
What do I miss??
Here is a pseudocode. If you use it in project it should be working.
//my image (android logo)
private String base64 = "iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAACgUlEQVRIiZ2Wv4vUQBTHP2/ZBFlEFrGS44iniLCggggWh4yVoIX4F9gIFtYiFgqWNgqChfoXHBYWXuWJF05rwe0UMWE5RI7lEJUguSPPYvJjkk32xAcbvjPz8n3v++ZlZqHDYuMRG/9+bPxBt4+/LzLevdj4XS5I54olOK/oCSAUWFY4al/RSJANRY0gX4IwfdvF0Z8XQCEW5AnwCBiInUMQFE1AJqAX53F0KoiNfxp4CQSNoA4SgInA1SBMP7Tx9NrJvSHoCmgPeAhk6lCLVTEWeCZwGHQlMt7BfwoQGR+FWyDHgc/Ac9BUZsTqWNEXQApyDLjdttkzJYqNPwT9CLKojkOBCyXFfjh4U5BTQZhuz1UAGoAsdJBgt1jQolAlZkHRpSbbTBcpMhW4WY4cDQXO1WR1LBnwvcknAJHxLwncAcbAuqJP5yuwtM0n6A2QC6AnQR4cCdPVfv7yNYVlgXOgnwSpdcRsL0utp6okZAG4rogPfANWiz3o5eJn9kQ6cJvlgQqOfvkAeq78lo8Jt4/2Kl7u06sFqLJzO74tf2nxajZCtdaz02XOGZBVCtyjwWItc9QWLM7YCVCX1kZWS6LTtEZtrShRVgivcDlu4HklshVwAxQ7Ps1zSID0/0tECiT5eOoquAv6DmSi6AHq3VDDe9gmcEWQRdDXzRoAEBt/CXgFHAKGQHFE/gF+5ngA7M9xprAt6BTkchCmX12+mQ8rdzgLjBQ2nBK9sXM6UvSxU5aJwAg40ySHjiszCNMESCLjJc5pkwThzhQgMt6vwlftXbEVhDttVPPvZGDX6ZXdEtmTs/QJwrSToPXKrEzWgB/5b91ZeA+6BfwG7fxHAfAXfJIQh9RXB18AAAAASUVORK5CYII=";
//somewhere in code
imageView = (ImageView) view.findViewById(R.id.imageView);//layout params are wrap and wrap
//weak reference is for safe using imageView when app i.e will exit and task will still be running
weakReferenceImageView = new WeakReference<ImageView>(imageView);
new AsyncTask<Void, Void, Bitmap>() {
#Override
protected Bitmap doInBackground(Void... params) {
byte[] encode = Base64.decode(base64.getBytes(), 0);
Bitmap bitmap = BitmapFactory.decodeByteArray(encode, 0, encode.length);
return bitmap;
}
#Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if (bitmap != null) {
if (weakReferenceImageView != null) {
ImageView weak = weakReferenceImageView.get();
if (weak != null) {
weak.setImageBitmap(bitmap);
}
}
}
}
}.execute();
If decoding is wrong, returned bitmap will be null.
Please try with my image (it is very small) if it works. If it works it means something is wrong with your based64 image.
Fixed example, added weakReference as a good sample of coding.
ImageView image = (ImageView) findViewById(R.id.image);
String image_url = "http://api.androidhive.info/images/sample.jpg";
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
imgLoader.DisplayImage(image_url, loader, image);
Use the library found on the url.
http://www.androidhive.info/2012/07/android-loading-image-from-url-http/
ImageLoader.java, FileCache.java, and Utils.java
Just Use Picasso
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Couldn't be easier than this.
Related
Again hi. I return android parsing with JSON. Now, i can successfully parsing but i dont get a picture. We stored picture as String on database. So, i think must convert String to Image for display in ImageView. I try this function but i can't use:
public Bitmap StringToBitMap(String encodedString) {
byte[] encodeByte = Base64.decode(encodedString, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap; }
And this my activiy class:
newsList = new ArrayList<>();
lv = (ListView) findViewById(R.id.list);
new GetContacts().execute();
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray news= jsonObj.getJSONArray("news");
JSONObject r = news.getJSONObject(0);
String Id = r.getString("Id");
String NewsDetail = r.getString("NewsDetail");
String Picture= r.getString("Picture");
HashMap<String, String> News = new HashMap<>();
News.put("Id", Id);
News.put("NewsDetail", NewsDetail);
News.put("Picture", Picture);
newsList.add(News);
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
final ListAdapter adapter = new SimpleAdapter(
NewsDip.this, newsList,
R.layout.news_list, new String[]{"Picture"},
new int[]{R.id.imageView});
lv.setAdapter(adapter);
}
I think I use this function in adapter. But I can't do it. How can I solve this?
you can use Glide library to simply show your byte converted image
add this in Gradle
repositories {
mavenCentral()
google()
}
dependencies {
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
add this in your java file
Glide.with(context)
.load(encodeByte) //your byte array
.asBitmap()
.placeholder(R.drawable.ic_broken)
.into(your imageview);
I don't know how to get several images from a URL.
Those images come in as Bitmap; I need to convert them to drawable, but I don't know how.
Drawable icon = getResources().getDrawable(R.drawable.icon);
Am I doing it correctly calling the url(images), or is there a better way to do that?
This is the asynctask where I get the images
private class BajarImagenTask extends AsyncTask<String, Void, Bitmap> {
#Override
protected Bitmap doInBackground(String... urls) {
return BajarImagen(urls[0]);
}
protected void onPostExecute(Bitmap result){
Drawable icon = getResources().getDrawable(R.drawable.icon);
point.setImage(d);
}
}
This the connection:
private Bitmap BajarImagen (String URL)
{
Bitmap bitmap = null;
InputStream in = null;
try {
in=OpenHttpConnection(URL);
bitmap=BitmapFactory.decodeStream(in);
in.close();
}
catch (IOException e1) {
}
return bitmap;
}
Here is the method with a "FOR" inside to get several URLs and calling many times the asynctask
public void datosDesdeElXML(String[][] datos) {
for(int i = 0; i < moteles.length;i++){
String motel[] = moteles[i];
double lat = Double.valueOf(motel[0].trim());
double lng = Double.valueOf(motel[1].trim());
String name = motel[2].trim();
String address = motel[3].trim();
**String urldefotoglobo = motel[4].trim();**
// here i get url from server in a xml format
String aidis = motel[5].trim();
**new BajarImagenTask().execute(urldefotoglobo);**
// Here i call the asynctask
}
}
I've been having lots of troubles using adapter to display my images properly into my imageView. So basically I have a JSON string and I am parsing through it to get the url of each image. Then I want to display the each image with its title in a list view fashion.
I found this code online where it works perfectly when only one image needs to be displayed, but it doesn't work when I have to do this dynamically. Anyone has some good suggestions on how to get it to work? I attached parts of my codes for references.
Thank you!!!
//results => JSON string
ArrayList<HashMap<String, Object>> resultList = new ArrayList<HashMap<String, Object>>();
for(int i = 0; i < results.length(); i++){
JSONObject c = results.getJSONObject(i);
// Storing each json item in variable
cover = c.getString(TAG_COVER);
String title = c.getString(TAG_TITLE);
try {
URL urlS = new URL(cover);
new MyDownloadTask().execute(urlS);
}catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// creating new HashMap
HashMap<String, Object> map = new HashMap<String, Object>();
// adding each child node to HashMap key => value
map.put(TAG_COVER, cover);
map.put(TAG_TITLE, title);
// adding HashList to ArrayList
resultList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, resultList,
R.layout.list_item,
new String[] {TAG_TITLE}, new int[] {
R.id.title});
setListAdapter(adapter);
And here is the image downloaded codes I found :
private class MyDownloadTask extends AsyncTask<URL, Integer, Bitmap> {
#Override
protected Bitmap doInBackground(URL... params) {
URL url = params[0];
Bitmap bitmap = null;
try {
URLConnection connection = url.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bitmap = BitmapFactory.decodeStream(bis);
bis.close();
//is.close(); THIS IS THE BROKEN LINE
} catch (Exception e) {
e.printStackTrace();
return null;
}
return bitmap;
}
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null) {
ImageView myImage = (ImageView) findViewById(R.id.list_image);
myImage.setImageBitmap(bitmap);
} else {
Toast.makeText(getApplicationContext(), "Failed to Download Image", Toast.LENGTH_LONG).show();
}
}
}
There is pretty good library calling AQuery. YOu can use it and simple get all this stuff by writting only 2 line of code, plus it will cache all images for you and it's very good especcially if you are using them in ListView
AQuery aq = new AQuery(activity);
aq.id(R.id.image).image(url, false, true);
Also i think it will be better to use CustomAdapter. I'm not sure it's possible to handle this situation using SimpleAdapter.
Hope this will help you.
In my custom ListView contains two textview and one Imageview im using AsyncTask to read the Text from Internet same with the imageView .As im reading and assigning all three view elemnt at the same time it takes to much time . In such case ineed to Convert url to Bitmap in another AsyncTask when the text part is done.
As a logic it recquire some concept of updating my ImageView resource .But i do not
know how to do it....
Thanks In Advance..
private class AsynchTask extends AsyncTask<Void, Integer, Void> {
URLConnection tc;
BufferedReader in;
URL twitter;
int num=0;
#Override
protected void onPreExecute() {
super.onPreExecute();
try {
mProgressBar.setVisibility(View.VISIBLE);
} catch (Exception e) {
Log.e(TAG,""+e.getMessage());
}
}
#Override
protected Void doInBackground(Void... params) {
try{
twitter = new URL("https://twitter.com/statuses/public_timeline.json");
tc = twitter.openConnection();
my = new ArrayList<HashMap<String,Object>>();
in = new BufferedReader(new InputStreamReader(
tc.getInputStream()));
ImageList=new ArrayList<String>();
while ((line = in.readLine()) != null) {
JSONArray ja = new JSONArray(line);
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
/**Data Insert into the HashMap Object*/
hm=new HashMap<String, Object>();
hm.put(TEXT,jo.getString("text"));
hm.put(USER,jo.getJSONObject("user").getString("name"));
// String str=jo.getJSONObject("user").getString("profile_image_url"); hm.put(URL,"http://twitter.com/#!/"+jo.getJSONObject("user").getString("screen_name"));
// hm.put(IMAGEURL,getDrawable_from_url(str));
ImageList.add(jo.getJSONObject("user").getString("profile_image_url"));
Log.e(TAG,""+num);
my.add(hm);
num++;
Log.e("Count",""+num);
publishProgress(num);
}
num++;
publishProgress(num);
}
} catch (Exception e) {
Log.e(TAG,""+e.getMessage());
}
return null;
}
#Override
protected void onProgressUpdate(Integer... values) {
mProgressBar.setProgress(values[0]);
super.onProgressUpdate(values);
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
mProgressBar.setProgress(0);
mProgressBar.setVisibility(View.GONE);
adapter = new Simpleadapter(HelloWorldActivity.this, my, R.layout.listcontent,
new String[]{TEXT,USER}, new int[]{R.id.text2,R.id.text1});
listView.setAdapter(adapter);
new AsynchTaskForImageLoading().execute();
}
}
/**Method to convert Url to the Bitmap*/
private Bitmap getDrawable_from_url(String url) {
try{
Bitmap x;
HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection();
connection.setRequestProperty("User-agent","Mozilla/4.0");
connection.connect();
InputStream input = connection.getInputStream();
x = BitmapFactory.decodeStream(input);
return x;
}
catch (Exception e) {
Log.e(TAG,""+e.getMessage());
return null;
}
}
I've used this LazyList with great success: https://github.com/thest1/LazyList
For your needs, you can swap out the supplied stub image with the one you'd like to use. I've also used a 1x1 blank png to show no image.
Also, one change that i've made in my code and that I strongly suggest when using this package is to change the code from using the SD card to use the built in cache. You do this by modifying the FileCache.java file from using .getExternalStorageDirectory() to .getCacheDir().
Hope this helps.
How do I replace the following lines of code with an Asynctask ?
How do you "get back" the Bitmap from the Asynctask ? Thank you.
ImageView mChart = (ImageView) findViewById(R.id.Chart);
String URL = "http://www...anything ...";
mChart.setImageBitmap(download_Image(URL));
public static Bitmap download_Image(String url) {
//---------------------------------------------------
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
Log.e("Hub","Error getting the image from server : " + e.getMessage().toString());
}
return bm;
//---------------------------------------------------
}
I thought about something like this :
replace :
mChart.setImageBitmap(download_Image(graph_URL));
by something like :
mChart.setImageBitmap(new DownloadImagesTask().execute(graph_URL));
and
public class DownloadImagesTask extends AsyncTask<String, Void, Bitmap> {
#Override
protected Bitmap doInBackground(String... urls) {
return download_Image(urls[0]);
}
#Override
protected void onPostExecute(Bitmap result) {
mChart.setImageBitmap(result); // how do I pass a reference to mChart here ?
}
private Bitmap download_Image(String url) {
//---------------------------------------------------
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
Log.e("Hub","Error getting the image from server : " + e.getMessage().toString());
}
return bm;
//---------------------------------------------------
}
}
but How do I pass a reference to mChart in onPostExecute(Bitmap result) ???
Do I need to pass it with the URL in some way ?
I would like to replace all my lines of code :
mChart1.setImageBitmap(download_Image(URL_1));
mChart2.setImageBitmap(download_Image(URL_2));
with something similar ... but in Asynctask way !
mChart1.setImageBitmap(new DownloadImagesTask().execute(graph_URL_1));
mChart2.setImageBitmap(new DownloadImagesTask().execute(graph_URL_2));
Is there an easy solution for this ?
Do I get something wrong here ?
If there is no good reason to download the image yourself then I would recommend to use Picasso.
Picasso saves you all the problems with downloading, setting and caching images.
The whole code needed for a simple example is:
Picasso.with(context).load(url).into(imageView);
If you really want to do everything yourself use my older answer below.
If the image is not that big you can just use an anonymous class for the async task.
This would like this:
ImageView mChart = (ImageView) findViewById(R.id.imageview);
String URL = "http://www...anything ...";
mChart.setTag(URL);
new DownloadImageTask.execute(mChart);
The Task class:
public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> {
ImageView imageView = null;
#Override
protected Bitmap doInBackground(ImageView... imageViews) {
this.imageView = imageViews[0];
return download_Image((String)imageView.getTag());
}
#Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
private Bitmap download_Image(String url) {
...
}
Hiding the URL in the tag is a bit tricky but it looks nicer in the calling class if you have a lot of imageviews that you want to fill this way. It also helps if you are using the ImageView inside a ListView and you want to know if the ImageView was recycled during the download of the image.
I wrote if you Image is not that big because this will result in the task having a implicit pointer to the underlying activity causing the garbage collector to hold the whole activity in memory until the task is finished. If the user moves to another screen of your app while the bitmap is downloading the memory can't be freed and it may make your app and the whole system slower.
Try this code:
ImageView myFirstImage = (ImageView) findViewById(R.id.myFirstImage);
ImageView mySecondImage = (ImageView) findViewById(R.id.mySecondImage);
ImageView myThirdImage = (ImageView) findViewById(R.id.myThirdImage);
String URL1 = "http://www.google.com/logos/2013/estonia_independence_day_2013-1057005.3-hp.jpg";
String URL2 = "http://www.google.com/logos/2013/park_su-geuns_birthday-1055005-hp.jpg";
String URL3 = "http://www.google.com/logos/2013/anne_cath_vestlys_93rd_birthday-1035005-hp.jpg";
myFirstImage.setTag(URL1);
mySecondImage.setTag(URL2);
myThirdImage.setTag(URL3);
new DownloadImageTask.execute(myFirstImage);
new DownloadImageTask.execute(mySecondImage);
new DownloadImageTask.execute(myThirdImage);
public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> {
ImageView imageView = null;
#Override
protected Bitmap doInBackground(ImageView... imageViews) {
this.imageView = imageViews[0];
return download_Image((String)imageView.getTag());
}
#Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
private Bitmap download_Image(String url) {
Bitmap bmp =null;
try{
URL ulrn = new URL(url);
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
InputStream is = con.getInputStream();
bmp = BitmapFactory.decodeStream(is);
if (null != bmp)
return bmp;
}catch(Exception e){}
return bmp;
}
}
you can create a class say..BkgProcess which contains an inner class that extends AsyncTask. while instantiating BkgProcess pass the context of your Activity class in BkgProcess constructor. for eg:
public class BkgProcess {
String path;
Context _context;
public Download(Downloader downloader, String path2){
this.path = path2;
_context = downloader;
}
public void callProgressDialog(){
new BkgProcess().execute((Void)null);
}
class Downloads extends AsyncTask<Void, Void, Boolean> {
private ProgressDialog dialog = new ProgressDialog(_context);
protected void onPreExecute(){
dialog.setMessage("Downloading image..");
dialog.show();
}
protected void onPostExecute(Boolean success) {
dialog.dismiss();
if(success)
Toast.makeText(_context, "Download complete", Toast.LENGTH_SHORT).show();
}
#Override
protected Boolean doInBackground(Void... params) {
return(startDownload(path));
}
public boolean startDownload(String img_url) {
// download img..
return true;
}
}
}
from your activity class..
BkgProcess dwn = new BkgProcess (Your_Activity_class.this, img_path);
dwn.callProgressDialog();
This will get you images of any size...
if you dont want the progress dialog just comment the codes in onPreExecute();
for(int i = 0 ; i < no_of_files ; i++ )
new FetchFilesTask().execute(image_url[i]);
private class FetchFilesTask extends AsyncTask<String, Void, Bitmap> {
private ProgressDialog dialog = new ProgressDialog(FileExplorer.this);
Bitmap bitmap[];
protected void onPreExecute(){
dialog.setMessage("fetching image from the server");
dialog.show();
}
protected Bitmap doInBackground(String... args) {
bitmap = getBitmapImageFromServer();
return bitmap;
}
protected void onPostExecute(Bitmap m_bitmap) {
dialog.dismiss();
if(m_bitmap != null)
//store the images in an array or do something else with all the images.
}
}
public Bitmap getBitmapImageFromServer(){
// fetch image form the url using the URL and URLConnection class
}