I'm trying to do listview with base64 images.
Listviews data is json-array from server and created to hashmap.
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString("pid");
String createdAt = c.getString("created_at");
String description = c.getString("description");
String image = c.getString("image");
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put("pid", id);
map.put("description", description);
map.put("image", image);
// adding HashList to ArrayList
productsList.add(map);
}
And:
protected void onPostExecute(String file_url) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/*Updating parsed JSON data into ListView*/
ListAdapter adapter = new SimpleAdapter(
listActivity.this, productsList,
R.layout.list_item, new String[] { "product_id",
"product_description", "product_image" },
new int[] { R.id.pid, R.id.description, R.id.image });
// updating listview
setListAdapter(adapter);
}
});
}
How can I make that image string converted to imageview?
I'm new with Android Studio, so I don't know if thats smart way to do that..
Above code is from: http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/
If that helps.
First what you have to do is to convert base64 to bitmap, and then you can set it on imageView.
How to decode base64 to bitMap:
byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
How to set bitmap to imageView:
ImageView mImg = (ImageView) findViewById(R.id.(your xml img id));
mImg.setImageBitmap(decodedByte );
Related
There's someway for me to adapt a encoded string image in a Listview?
I got from my server, in base64 format, a image in a string, and I want to set it in a Listview with some text.
Here is part of my code:
for (int i = 0; i < makers.length(); i++) {
JSONObject c = makers.getJSONObject(i);
// Storing each json item in variable
String name = c.getString(TAG_NAME);
String age = c.getString(TAG_AGE);
String username = c.getString(TAG_USERNAME);
String rating = c.getString(TAG_RATING);
String disc = c.getString(TAG_DISC);
Long date = c.getLong(TAG_DATE);
String num = c.getString(TAG_NUM);
// creating new HashMap
//HashMap<String, String> map = new HashMap<String, String>();
Map map = new HashMap<String, Bitmap>();
String image=c.getString(TAG_IMAGE1);
byte[] decodedString = Base64.decode(image, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
map.put(TAG_NAME, name);
map.put(TAG_USERNAME, username);
map.put(TAG_AGE, age);
map.put(TAG_RATING, rating);
map.put(TAG_DISC, disc);
map.put(TAG_NUM, num);
map.put(TAG_IMAGE1, bitmap);
// map.put(TAG_CIDADE, cidade);
long curdate = System.currentTimeMillis();
long onem = curdate - date;
long intm = 3*30*24*60*60*1000 ;
if(onem >= intm){
} else {
// adding HashList to ArrayList
userdataList.add((HashMap<String, String>) map);
}
}
}
And here is my Adapter:
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting the related idioms
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
ListResult1.this, userdataList,
R.layout.list_view, new String[] { TAG_NAME, TAG_USERNAME, TAG_AGE, TAG_RATING, TAG_DISC, TAG_NUM, TAG_IMAGE1},
new int[] {R.id.name, R.id.username, R.id.age, R.id.rating, R.id.disc, R.id.num, R.id.ivimage1s});
// updating listview
setListAdapter(adapter);
}
});
}
And i want also to transfer that image through a intent, it's possible?
The actual problem is that I can't set a adapter for my image, all the text part work just fine, but the image did not loaded.
Sorry for any English issue, it's not my mother language.
I was trying to put a JSON array of object into a hashmap.The object contains images which is in base64 format.I converted the image to bit map and need to put the image in a List view using a hashmap.But i am getting a null pointer exception
#Main Activity#
protected void onPostExecute(JSONObject json) {
try {
// Getting JSON Array from URL
details = json.getJSONArray(TAG_Root);
for(int i = 0; i < details.length(); i++){
JSONObject c = details.getJSONObject(i);
// Storing JSON item in a Variable
String branch = c.getString(TAG_Branch);
String address = c.getString(TAG_Add);
String uname = c.getString(TAG_User);
String photo = c.getString(TAG_Photo);
//decoding the base64 image to an png format
byte[] imageAsBytes = Base64.decode(photo.getBytes(), Base64.DEFAULT);
ImageView image = (ImageView)findViewById(R.id.imageView1);
image.setImageBitmap(
BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_Branch, branch);
map.put(TAG_Add, address);
map.put(TAG_User, uname);
oslist.add(map);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(TAG_Photo,image);
oslist.add(map);
//String.valueOf(TAG_Photo
//,R.id.imageView1
list=(ListView)findViewById(R.id.listView1);
ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
R.layout.list_activity,
new String[] { TAG_Branch,TAG_Add, TAG_User }, new int[] {
R.id.textView1,R.id.textView2, R.id.textView3});
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int pos=position;
Toast.makeText(MainActivity.this, "You Clicked at "+oslist.get(+pos).get("name"), Toast.LENGTH_SHORT).show();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
You're getting null because of the map
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(TAG_Photo,image);
the adapter won't find the needed tags in it so it will give null exception.
If photo's value is base64 string content, I think you don't need photo.getBytes()
String photo = jsonObject.getString(TAG_Photo);
byte[] bytes = Base64.decode(photo, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
PHP file
$json = array();
if(mysql_num_rows($result)){
while($row=mysql_fetch_assoc($result)){
$row['EventPic'] = base64_encode($row['EventPic']);
$json['event'][]=$row;
}
}
dbDisconnect($conn);
echo json_encode($json);
?>
I followed online solution to base64_encode the blob array
Android side
try{
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("event");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
name = jsonChildNode.optString("TITLE");
number = jsonChildNode.optString("EVENTID");
diploma = jsonChildNode.optString("DIPLOMA");
date = jsonChildNode.optString("DATE").toString();
BlobPicture = jsonChildNode.optString("EventPic");
byte[] decodedString = Base64.decode(BlobPicture, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
// creating new HashMap
HashMap<String, Object> map = new HashMap<String, Object>();
// adding each child node to HashMap key => value
map.put(TAG_DATE, diploma);
map.put(TAG_TITLE, name);
map.put(TAG_ID, number);
map.put(TAG_DAY, date);
map.put(TAG_PIC, decodedByte);
// adding HashList to ArrayList
eventsList.add(map);
}
} catch (JSONException e) {
Toast.makeText(getActivity(), "No events on this date" , Toast.LENGTH_SHORT).show();
}
ListAdapter adapter = new SimpleAdapter(
getActivity(), eventsList, R.layout.eventuse, new String[] { TAG_DATE, TAG_TITLE, TAG_ID, TAG_DAY, TAG_PIC},
new int[] { R.id.title1, R.id.date1, R.id.hideeventid, R.id.course1, R.id.imh });
listView.setAdapter(adapter);
I need help to retrieve from the Android side, my method doesn't seems to work as my SimpleAdapter requires R.id.
Any advice or solutions are much appreciated.
I have yet another question regarding displaying image on listview from json object. I checked almost every possible answers on stackoverflow but did not find any solution. I have following implementation
contact = json.getJSONArray(TAG_CONTACT);
for(int i = 0; i < contact.length(); i++){
JSONObject c = contact.getJSONObject(i);
//put json obkject on variable
String fname = c.getString(TAG_NAME);
String uname = c.getString(TAG_UNAME);
String type = c.getString(TAG_TYPE);
String email = c.getString(TAG_EMAIL);
String thumb = c.getString(TAG_THUMB);
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_NAME, fname);
map.put(TAG_UNAME, uname);
map.put(TAG_TYPE, type);
map.put(TAG_EMAIL, email);
map.put(TAG_THUMB, thumb);
contactlist.add(map);
// Finding the list view
list=(ListView)findViewById(R.id.contactlist);
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
// Put data on listview content
ListAdapter adapter = new SimpleAdapter(ContactPage.this, contactlist,
R.layout.contact_list,
new String[] {TAG_NAME,TAG_UNAME,TAG_TYPE,TAG_EMAIL,TAG_THUMB}, new int[] {
R.id.contact_name,R.id.contact_usertype,R.id.contact_status,R.id.contact_email,R.id.contact_image});
list.setAdapter(adapter);
Now where and how shall I implement the imgLoader.DisplayImage(url, imageView) in my code if I have image URL on thumb , i.e c.getString(TAG_THUMB);
Thanks in advance.
I'm building a a Gallery app using GridView and ViewPager. I'm getting the image URLs from JSON. I've got the GridView displaying the images correctly, and now I'm moving on to the ViewPager. What I think I need to do is generate a String array of the image URLs for the ViewPager Adapter. In the GridView Activity, I've created a HashMap with the id, URL, and image description strings stored. My question now is: Is it possible to generate a String Array by retrieving all the stored URL strings from the HashMap? What I need is this from the HashMap:
public static final String[] imagesStr = new String[] {
"http://www.mysite/images/building0001.jpg",
"http://www.mysite/images/building00011.jpg",
"http://www.mysite/images/building0010.jpg" };
Here's how I'm generating the HashMap:
#Override
protected Void doInBackground(Void... params) {
// Retrieve JSON Objects from the given URL address
galleryArrList = new ArrayList<HashMap<String, String>>();
jsonobject = JIJSONfunctions.getJSONfromURL(urlPathStr);
try {
// Locate the array name in JSON
JSArrGallery = jsonobject.getJSONArray("gallery");
for (int i = 0; i < JSArrGallery.length(); i++) {
JSONObject galleryJO = JSArrGallery.getJSONObject(i);
idStr = galleryJO.getString(TAG_ID);
urlStr = galleryJO.getString(TAG_URL);
descrStr = galleryJO.getString(TAG_DESCR);
// creating new HashMap
HashMap<String, String> map = new HashMap<String,String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, idStr);
map.put(TAG_URL, urlStr);
map.put(TAG_DESCR, descrStr);
galleryArrList.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
I'm a bit unclear on how a HashMap works, but I assume that the code is going through the JSON Array and pulling each URL string out and storing it in the HashMap, right? So now can I pull all the gathered URL Strings and create a String Array with them? How would I do that?
Seems straightforward enough. After you have populated your List<HashMap<String,String>>, iterate over each map and pull the value you need from it to add to a String[]. Please refer to the following snippet:
galleryArrList = new ArrayList<HashMap<String, String>>();
jsonobject = JIJSONfunctions.getJSONfromURL(urlPathStr);
try { ... }
final int length = galleryArrList.size();
final String[] imagesStr = new String[length];
for (int i = 0; i < length; i++) {
final Map<String, String> map = galleryArrList.get(i);
imagesStr[i] = map.get(TAG_URL);
}
You might try using an ArrayList and add the URLs to the ArrayList within the for loop. When the loop is done, you can use the ArrayList.toArray(T[] array) method copy the URLs from the ArrayList to a String array.
ArrayList.toArray
ArrayList arlist = new ArrayList();
for (int i = 0; i < JSArrGallery.length(); i++) {
JSONObject galleryJO = JSArrGallery.getJSONObject(i);
idStr = galleryJO.getString(TAG_ID);
urlStr = galleryJO.getString(TAG_URL);
descrStr = galleryJO.getString(TAG_DESCR);
// creating new HashMap
HashMap<String, String> map = new HashMap<String,String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, idStr);
map.put(TAG_URL, urlStr);
map.put(TAG_DESCR, descrStr);
galleryArrList.add(map);
arlist.add(urlStr);
}
// Create a String array to hold the URLs
String[] arUrls = new String[arlist.size()];
// Copy the URLs from the ArrayList to the String array
arlist.toArray(arUrls);