从android中的服务器调用图像

时间:2011-06-30 14:09:50

标签: android json

你好我有this Json输出,我在我的Android应用程序中调用。我能够获得图片路径,但我怎么能显示图片而不是路径。这是代码

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(); 

            }
        });

    }

这是我在Android应用程序中获取的路径

pictures/file83915.jpg

我在这里叫图片

map.put("pic", "Picture : " + e.getString("employeepic"));

1 个答案:

答案 0 :(得分:1)

我已经使用此类从服务器下载图像 希望它会帮助你...... !!当您从服务器或任何来源获得图像URL列表时,请使用它来下载该特定图像。

GetImage.downloadFile("pictures/file83915.jpg", new ImageDownloaded()
{
    @Override
    public void imageDownloaded(Bitmap result){
        image.setImageBitmap(result);
    }

    @Override
    public void imageDownloadedFailed(){
    }

});

GetImage类的位置是:

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();
    }


}

我使用CustomAdapter类来显示列表中包含这样的图像的数据。 在getView()方法中,我使用了这个线程。

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是我的class,它封装了来自服务器的数据以及来自服务器和implements Parcelable interface的图像。首先,我通过JSON获取数据,然后在Adapter中获取图片。它也可以传递给任何DetailActivity