Вопрос

As I am newbie in android and PHP getting lots of struggles, here is a new one.

I successfully uploaded an image from my android application to WAMP server. 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".

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.

Это было полезно?

Решение

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

  • 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. You may get more info here.

Другие советы

Pass your url to this method and set the bmp to your 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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top