Reputation: 3133
Hello i have this Json output and i am calling in my android app. I am able to get picture path but how can i display picture instead of path . here is the code
public class Test extends ListActivity {
Prefs myprefs = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
this.myprefs = new Prefs(getApplicationContext());
// install handler for processing gui update messages
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json = JSONfunctions.getJSONfromURL("http://midsweden.gofreeserve.com/proj/androidjson.php?identifier=" +
Test.this.myprefs.getPersonalno());
try{
JSONArray earthquakes = json.getJSONArray("services");
for(int i=0;i<earthquakes.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = earthquakes.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("pic", "Picture : " + e.getString("employeepic"));
map.put("serviceinfo", "" + e.getString("employeename")+ " : "+ e.getString("starttime")
+" To " + e.getString("endtime"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.test,
new String[] { "pic", "serviceinfo" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(Test.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
});
}
This is path which i am getting in my android app
pictures/file83915.jpg
Here i am calling picture
map.put("pic", "Picture : " + e.getString("employeepic"));
Upvotes: 0
Views: 2194
Reputation: 37729
I have used this class for downloading image from server Hope it will help you...!! When you have got your image URLs list from your server or any source, then used it like this to download that particular Image.
GetImage.downloadFile("pictures/file83915.jpg", new ImageDownloaded()
{
@Override
public void imageDownloaded(Bitmap result){
image.setImageBitmap(result);
}
@Override
public void imageDownloadedFailed(){
}
});
Where the GetImage class is:
public class GetImage
{
public static void downloadFile(final String fileUrl, final ImageDownloaded img)
{
AsyncTask<String , Void, Bitmap> task = new AsyncTask<String , Void, Bitmap>()
{
@Override
protected Bitmap doInBackground(String... params) {
Bitmap bmImg;
URL myFileUrl =null;
if(fileUrl.equals(""))
{
return null;
}
try
{
myFileUrl= new URL("http://yourURl/" +fileUrl.replace(" ", "%20"));
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
try
{
HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
return bmImg;
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap results)
{
if(results != null)
img.imageDownloaded(results);
else
img.imageDownloadedFailed();
}
};
task.execute("");
}
public static abstract class ImageDownloaded
{
public abstract void imageDownloaded(Bitmap result);
public abstract void imageDownloadedFailed();
}
}
I used CustomAdapter
class to show the data in the list with Images like this.
in the method getView()
I used this thread like this.
public View getView(params....)
{
View row ;//Inflataion blah blah
Bitmap thumb = myCustomObject.getBitmap();
final ImageView image = (ImageView) row.findViewById(R.id.image);
if(thumb == null)
{
GetImage.downloadFile(myCustomObject.getImagePath(), new ImageDownloaded()
{
@Override
public void imageDownloaded(Bitmap result){
myCustomObject.setBmp(result);
image.setImageBitmap(myCustomObject.getBitmap());
}
@Override
public void imageDownloadedFailed(){
}
});
}
else
image.setImageBitmap(thumb);
}
MyCustomObject
is my class
that encapsulates the data from server as well as Image from server and implements Parcelable interface
. First I get data through JSON
and then get Image in Adapter
. It can also be passed to the any DetailActivity
Upvotes: 1