从Android的服务器调用图像 [英] calling image from server in android

查看:85
本文介绍了从Android的服务器调用图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有的JSON输出和我我打电话在我的Andr​​oid应用程序。我能够得到的图片路径,但我怎么能显示画面,而不是路径。这里是code

 公共类测试扩展ListActivity {
      preFS我的preFS = NULL;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.listplaceholder);
        this.my preFS =新的preFS(getApplicationContext());
        //安装处理程序处理图形用户界面更新消息
        ArrayList的< HashMap的<字符串,字符串>> mylist中=新的ArrayList< HashMap的<字符串,字符串>>();
       JSONObject的JSON = JSONfunctions.getJSONfromURL(http://midsweden.gofreeserve.com/proj/androidjson.php?identifier=+
        Test.this.my prefs.getPersonalno());

        尝试{

            JSONArray地震= json.getJSONArray(服务);

            的for(int i = 0; I< earthquakes.length();我++){
                HashMap的<字符串,字符串>图=新的HashMap<字符串,字符串>();
                JSONObject的E = earthquakes.getJSONObject(我);

                map.put(ID,将String.valueOf(I));
                map.put(PIC,图片+ e.getString(employeepic));
                map.put(serviceinfo,+ e.getString(employeename)+:+ e.getString(开始时间)
                        +到+ e.getString(结束时间));
                mylist.add(图)
            }
        }赶上(JSONException E){
             Log.e(log_tag,错误分析数据+ e.toString());
        }

        ListAdapter适配器=新SimpleAdapter(这一点,mylist中,R.layout.test,
                        新的String [] {PIC,serviceinfo},
                        新的INT [] {R.id.item_title,R.id.item_subtitle});

        setListAdapter(适配器);

        最终的ListView LV = getListView();
        lv.setTextFilterEnabled(真正的);
        lv.setOnItemClickListener(新OnItemClickListener(){
            公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长的id){
                @燮pressWarnings(未登记)
                HashMap的<字符串,字符串> O =(HashMap的<字符串,字符串>)lv.getItemAtPosition(位置);
                Toast.makeText(Test.this,ID+ o.get(ID)+'被点击了。,Toast.LENGTH_SHORT).show();

            }
        });

    }
 

这是路径,我得到在我的Andr​​oid应用程序

 图片/ file83915.jpg
 

下面我打电话图片

  map.put(PIC,图片+ e.getString(employeepic));
 

解决方案

我已经使用这个类从服务器下载图像 希望它会帮助你......!当你有你的图片网址列表,您的服务器或任何来源,然后用它这样的下载特定的图像。

  GetImage.downloadFile(图片/ file83915.jpg,新ImageDownloaded()
{
    @覆盖
    公共无效imageDownloaded(位图的结果){
        image.setImageBitmap(结果);
    }

    @覆盖
    公共无效imageDownloadedFailed(){
    }

});
 

凡的getImage类:

 公共类的getImage
{
    公共静态无效downloadFile(最后弦乐fileUrl,最终ImageDownloaded张图片)
    {
        AsyncTask的<字符串,太虚,位图>任务=新的AsyncTask<字符串,太虚,位图>()
        {
            @覆盖
            受保护的位图doInBackground(字符串... PARAMS){
                位图bmImg;
                URL myFileUrl = NULL;
                如果(fileUrl.equals())
                {
                    返回null;
                }
                尝试
                {
                    myFileUrl =新的URL(HTTP:// yourURl /+ fileUrl.replace(,%20));
                }
                赶上(MalformedURLException的E)
                {
                    e.printStackTrace();
                }
                尝试
                {
                    HttpURLConnection的康恩=(HttpURLConnection类)myFileUrl.openConnection();
                    conn.setDoInput(真正的);
                    conn.connect();

                    InputStream的是= conn.getInputStream();
                    bmImg = BitmapFactory.de codeStream(是);

                    返回bmImg;
                }
                赶上(IOException异常E)
                {
                    e.printStackTrace();
                }
                返回null;
            }
            @覆盖
            保护无效onPostExecute(位图的结果)
            {
                如果(结果!= NULL)
                img.imageDownloaded(结果);
                其他
                    img.imageDownloadedFailed();
            }

        };
        task.execute();


    }

    公共静态抽象类ImageDownloaded
    {
        公共抽象无效imageDownloaded(位图的结果);
        公共抽象无效imageDownloadedFailed();
    }


}
 

我用 CustomAdapter 类来显示数据列表中与这样的图像。 在方法 getView()我用这个线程是这样的。

 公开查看getView(PARAMS ....)
{
    查看排; // Inflataion等等等等
    位图拇指= myCustomObject.getBitmap();
    最终ImageView的形象=(ImageView的)row.findViewById(R.id.image);

    如果(拇指== NULL)
    {
        GetImage.downloadFile(myCustomObject.getImagePath(),新ImageDownloaded()
        {

            @覆盖
            公共无效imageDownloaded(位图的结果){
                myCustomObject.setBmp(结果);
                image.setImageBitmap(myCustomObject.getBitmap());
            }

            @覆盖
            公共无效imageDownloadedFailed(){
            }

        });
    }
    其他
    image.setImageBitmap(拇指);
}
 

MyCustomObject 是我的封装了数据服务器以及图片来自服务器和实现Parcelable接口。首先,我打通 JSON 数据,然后获取图像的适配器。它也可以被传递到任何 DetailActivity

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

解决方案

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

这篇关于从Android的服务器调用图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆