user4606231
user4606231

Reputation:

Url image to imageview

    ImageView imgview = (ImageView)findViewById(R.id.image);
    Drawable drawable = LoadImage("http://192.168.172.1/myproject/images/st1.jpg");
    imgview.setImageDrawable(drawable);
    ...
    ...
public Drawable LoadImage(String url)
{

    try
    {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable b = Drawable.createFromStream(is, url);
    return b;
    }catch(Exception e){
        System.out.println(e);
        return null;
    }

}

.java

     ...
     ...
     <ImageView 
        android:id="@+id/image"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"/>

.xml

getting the image from wamp server as a Url and display it onto my xml but image doesn't shown

Upvotes: 1

Views: 171

Answers (5)

Vindhya Pratap Singh
Vindhya Pratap Singh

Reputation: 556

Are you given internet permission on manifest

and running this code in background such as asyn task

 class LoadImageLoader extends AsyncTask<Void, Void, Void>{

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
         drawable = LoadImage("http://i.imgur.com/DvpvklR.png");
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        imgview.setImageDrawable(drawable);

    }

}

Upvotes: 0

Adarsh Yadav
Adarsh Yadav

Reputation: 3770

Use http://square.github.io/picasso/ it's really easy to implement, follow the following steps:-

1- If you haven’t done it already. If you are using eclipse as your development IDE, then just copy the downloaded picasso-2.5.2.jar file into your application lib folder. If you are using Android Studio IDE, then you have to add below dependency in build.gradle file.

dependencies {
 ...
 compile "com.squareup.picasso:picasso:2.5.2"
 ...
 } 

2- Now let us download the image and display on imageView:-

//Loading image from below url into imageView

Picasso.with(this)
.load("YOUR IMAGE URL HERE")
.into(imageView);//Your image view

That's all, Hope this will help you !!

Upvotes: 1

Chris
Chris

Reputation: 6311

I use the following code to get an image from url and add it to an ImageView:

ImageRequest imgRequest = new ImageRequest(url,
            new Response.Listener<Bitmap>() {
                @Override
                public void onResponse(Bitmap response) {
                    mImageView.setImageBitmap(response);
                    Toast.makeText(MainActivity.this, "Succes", Toast.LENGTH_SHORT).show();
                }
            }, 0, 0, ImageView.ScaleType.FIT_XY, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
            mImageView.setBackgroundColor(Color.parseColor("#ff0000"));
            error.printStackTrace();
        }
    });

    Volley.newRequestQueue(this).add(imgRequest);

All you need to do is to use Volley Library made by google developers(trusted source). More info here

Hope it helps!

Upvotes: 1

pelotasplus
pelotasplus

Reputation: 10052

There are many third party libraries that can help you with that. Their use is strongly recommended, ie. don't create your own tool for that.

  • Picasso by guys from Square
  • Glide used in sample apps by Google

Upvotes: 0

christoandrew
christoandrew

Reputation: 437

Use Picasso works great and is easy to use.

Upvotes: 0

Related Questions