Android - Set ImageView to URL

android imageview set image from uri
android download image from url and save to drawable
how to get image url from imageview in android
android set wallpaper programmatically from url
download image from url android
android json parsing and loading images from url
android picasso
kotlin load image from url

I am trying to set an Imageview to a URL.

Below is my code

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.getpic);
    ImageView i = (ImageView)findViewById(R.id.ivget);

    URL url;
    try {

        url = new URL("http://0-media-cdn.foolz.us/ffuuka/board/sp/thumb/1359/41/1359419073599s.jpg");
        Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        i.setImageBitmap(image);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

But whenever I try to open/run the app it crashes, why isnt it working?

06-15 00:37:20.977: E/AndroidRuntime(8579): FATAL EXCEPTION: main
06-15 00:37:20.977: E/AndroidRuntime(8579): java.lang.RuntimeException: Unable to  instantiate activity  ComponentInfo{com.OptimusApps.stayhealthy/com.OptimusApps.stayhealthy.Practice2}:  java.lang.NullPointerException
06-15 00:37:20.977: E/AndroidRuntime(8579):     at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1993)
 06-15 00:37:20.977: E/AndroidRuntime(8579):    at  android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
06-15 00:37:20.977: E/AndroidRuntime(8579):     at android.app.ActivityThread.access$600(ActivityThread.java:132)
06-15 00:37:20.977: E/AndroidRuntime(8579):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
06-15 00:37:20.977: E/AndroidRuntime(8579):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-15 00:37:20.977: E/AndroidRuntime(8579):     at android.os.Looper.loop(Looper.java:137)
06-15 00:37:20.977: E/AndroidRuntime(8579):     at android.app.ActivityThread.main(ActivityThread.java:4575)
06-15 00:37:20.977: E/AndroidRuntime(8579):     at java.lang.reflect.Method.invokeNative(Native Method)
06-15 00:37:20.977: E/AndroidRuntime(8579):     at  java.lang.reflect.Method.invoke(Method.java:511)
06-15 00:37:20.977: E/AndroidRuntime(8579):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-15 00:37:20.977: E/AndroidRuntime(8579):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
06-15 00:37:20.977: E/AndroidRuntime(8579):     at dalvik.system.NativeStart.main(Native Method)
06-15 00:37:20.977: E/AndroidRuntime(8579): Caused by: java.lang.NullPointerException
06-15 00:37:20.977: E/AndroidRuntime(8579):     at android.app.Activity.findViewById(Activity.java:1794)
06-15 00:37:20.977: E/AndroidRuntime(8579):     at com.OptimusApps.stayhealthy.Practice2.<init>(Practice2.java:17)
06-15 00:37:20.977: E/AndroidRuntime(8579):     at java.lang.Class.newInstanceImpl(Native Method)
06-15 00:37:20.977: E/AndroidRuntime(8579):     at java.lang.Class.newInstance(Class.java:1319)
06-15 00:37:20.977: E/AndroidRuntime(8579):     at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
06-15 00:37:20.977: E/AndroidRuntime(8579):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1984)
06-15 00:37:20.977: E/AndroidRuntime(8579):     ... 11 more

above is the logcat, I am not sure where the error is in it specificaly though..............................................................................................................................................................

You should not do network related operation on the main ui thread.

You can findViewById of the current view hierachy set to the activity. check if you have imageview in ivget.xml. Also use a asynctask as below and make a http get request to get the image.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="173dp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

MainActivity.java

    public class AndroidCustomGalleryActivity extends Activity { 
ImageView iv;
Bitmap image ;
ProgressDialog pd;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    iv = (ImageView) findViewById(R.id.imageView1);
    pd = new ProgressDialog(this);
    pd.setMessage("Loading..");
    new TheTask().execute();    
}
class TheTask extends AsyncTask<Void,Void,Void>
{

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pd.show();
    }


    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        try
        {
        //URL url = new URL( "http://a3.twimg.com/profile_images/670625317/aam-logo-v3-twitter.png");


        image = downloadBitmap("http://a3.twimg.com/profile_images/670625317/aam-logo-v3-twitter.png");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        pd.dismiss();
        if(image!=null)
        {
            iv.setImageBitmap(image);
        }

    }   
}
 private Bitmap downloadBitmap(String url) {
     // initilize the default HTTP client object
     final DefaultHttpClient client = new DefaultHttpClient();

     //forming a HttoGet request 
     final HttpGet getRequest = new HttpGet(url);
     try {

         HttpResponse response = client.execute(getRequest);

         //check 200 OK for success
         final int statusCode = response.getStatusLine().getStatusCode();

         if (statusCode != HttpStatus.SC_OK) {
             Log.w("ImageDownloader", "Error " + statusCode + 
                     " while retrieving bitmap from " + url);
             return null;

         }

         final HttpEntity entity = response.getEntity();
         if (entity != null) {
             InputStream inputStream = null;
             try {
                 // getting contents from the stream 
                 inputStream = entity.getContent();

                 // decoding stream data back into image Bitmap that android understands
                 image = BitmapFactory.decodeStream(inputStream);


             } finally {
                 if (inputStream != null) {
                     inputStream.close();
                 }
                 entity.consumeContent();
             }
         }
     } catch (Exception e) {
         // You Could provide a more explicit error message for IOException
         getRequest.abort();
         Log.e("ImageDownloader", "Something went wrong while" +
                 " retrieving bitmap from " + url + e.toString());
     } 

     return image;
 }
}

Android Image Loading from a String URL, Android Image Loading from a String URL. Occasionally, adding images to your android application is simple and straightforward, and all you have to do is place the image in the drawable folder and set the ImageView source (“src”) property in your layout xml file to the correct path. Now I want to set it to an imageview when activity is loading. Below is how I get the bean from the rest api and then get the URL out of it. Message message=new Message(); String imageUrl=message.getImageUrl(); I get Message object from my database and image url is include in that Message object. Then I used Url object to get that image url.

android.app.Activity.findViewById(Activity.java:1794)

=> It says you haven't initialized your ImageView i.e. "i" in your code.

ImageView i = (ImageView) findViewById(R.id.myImageViewInXML);

How to set ImageView image from URL in Android, set ImageView image from URL in Android. activity_main.xml. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  In this tutorial, we will see how to load an image from URL into Android ImageView. For downloading the image from URL and loading it in ImageView, we use AsyncTask. Environment Used. JDK 6 (Java SE 6) Eclipse Indigo IDE for Java EE Developers (3.7.1) Android SDK 4.0.3 / 4.1 Jelly Bean

Use Glide

Implementation 'com.github.bumptech.glide:glide:4.2.0'

Or

in java

Glide.with(getApplicationContext())
                .load(image)
                .into(imageView);

Downloading and Showing Image with Glide Library in Android, How do you use image from URL on Glide Android? Home » Android » android – how to set image from url for imageView android – how to set image from url for imageView Posted by: admin May 10, 2020 Leave a comment

How to Load Image from URL in ImageView in Android |, by step guide to display image from URL with source code for Android Studio to load an image from your web URL into Android ImageView. How to Display Image on ImageView with Image Url in Android In this android programming code example, we will illustrate how easy it is to display image on ImageView with image url in android. There are different ways you can use to show remote image on ImageView but here we will make use of Picasso which is an android network library.

How to load an ImageView by URL on Android using Picasso?, This example demonstrates about how do I load an ImageView on Android using Picasso.Step 1 − Create a new project in Android Studio,  I have Imageview and a link to the picture on the Internet. I set this picture to the ImageView like this, public ImageView iv; iv = (ImageView) findViewById(R.id.imageView); String img = "https:/

How to Display Image on ImageView with Image Url in Android, How to Display Image on ImageView with Image Url in Android How to Set Padding in LinearLayout in Android Programmatically · How to send a notification​  This example demonstrates about how do I load an ImageView on Android using Picasso. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Comments
  • are you using asynctask? post the stack trace?
  • include logcat error here
  • include the whole code looks like one of the views is not initialized
  • @rahulkapoor I added the logcat error
  • have you initialize your imageview.??
  • @user222786 where have you written this code of fetching image from web and setting to ImageView?
  • you should use a asynctask you cannot do network related operation on the main ui thread