简体   繁体   English

如何从WAMP服务器下载图像文件?

[英]How to download an image file from WAMP server?

As I am newbie in android and PHP getting lots of struggles, here is a new one. 由于我是android和PHP的新手,因此遇到了很多麻烦,因此这里是一个新手。

I successfully uploaded an image from my android application to WAMP server. 我已成功将图像从我的Android应用程序上传到WAMP服务器。 The process I used is, select an image from SD card, place it on Image view and the same image will get uploaded on WAMP server in the directory "www\\Images". 我使用的过程是,从SD卡中选择一个图像,将其放置在“图像”视图中,并且同一图像将被上载到WAMP服务器上的目录“ www \\ Images”中。

Now, the reverse way, when a user successfully logs into the system, he/she should see his/her last uploaded image on image view. 现在,以相反的方式,当用户成功登录系统时,他/她应该在图像视图中看到他/她最后上传的图像。

This process works fine till uploading image on server. 在将图像上传到服务器之前,此过程可以正常工作。 But I am unable to download the same image. 但是我无法下载相同的图像。

I tried something like the following, 我尝试了以下方法

 ImageView imcage = (ImageView)findViewById(R.id.ObjImgNormalUser);
 //start the download
 Bundle bundle = getIntent().getExtras();
 String usrName = bundle.getString("usrName");
 String Url = "http://172.25.64.188/Images/";
 Url = Url + "ABC";  //image name on server
 Url = Url + ".JPG";
 try 
 {
    InputStream inputStream =  (InputStream)new URL(Url).getContent();
    Bitmap bmp = BitmapFactory.decodeStream(inputStream);
    imcage.setImageBitmap(bmp);
 }

But it is not working. 但这是行不通的。

Please suggest me how to download an image from WAMP server and display it on image view in an android application. 请建议我如何从WAMP服务器下载图像并将其显示在Android应用程序的图像视图中。

Something like this should work: 这样的事情应该起作用:

URL url = new URL("http://www.yourdomain/your/path/image.jpg");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
final InputStream input = connection.getInputStream();
Bitmap yourpic = BitmapFactory.decodeStream(input);

However, a few clarifications: 但是,有一些澄清:

  • This is a network operation, you'll have to run it in a Thread or an AsyncTask 这是一项网络操作,您必须在ThreadAsyncTask运行它

  • I recommend using the above code just for testing purposes or for the case that the image is very small. 我建议将以上代码仅用于测试目的或用于图像很小的情况。

  • If you need to load more pics in background without interfering with the user's ability to interact with your app, I recommend using Lazy Loading . 如果您需要在后台加载更多图片而不干扰用户与您的应用进行交互的能力,建议您使用Lazy Loading You may get more info here . 您可以在此处获得更多信息。

Pass your url to this method and set the bmp to your imageview 将您的网址传递给此方法,然后将bmp设置为imageview

public Bitmap getImage(String url){
        Bitmap bmp =null;
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(url);
        HttpResponse getResponse = null;
        try {
            getResponse = client.execute(get);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String entityContents="";
        HttpEntity responseEntity = getResponse.getEntity();
        BufferedHttpEntity httpEntity = null;
        try {
            httpEntity = new BufferedHttpEntity(responseEntity);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        InputStream imageStream = null;
        try {
            imageStream = httpEntity.getContent();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        bmp = BitmapFactory.decodeStream(imageStream);
        return bmp;

    }

remember to call this method via an asyctask as it is a network operation, yuo whould always perform it in the background 请记住通过asyctask调用此方法,因为它是一种网络操作,所以您总是在后台执行它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM