Android ImageView by URL, Drawable and Library

Hello for beginners android developers, you are now with us CodingSheep to try and create ImageView using url and also take from drawable / mipmap. You can directly Copy and Paste my code below hehehe. Lets go

For first you in ImageView on XML you just need this code :



Good, now you want in comes from JAVA simple add.

//Example One
ImageView MyImage = (ImageView)findViewById(R.id.imageView);
MyImage.setImageDrawable(getResources().getDrawable(R.drawable.myphoto));
//Example Two   
ImageView MyImage = (ImageView)findViewById(R.id.imageView);
Drawable img = getResources().getDrawable(R.drawable.myphoto);
MyImage.setImageDrawable(img);
//Example Three
ImageView MyImage = (ImageView)findViewById(R.id.imageView);
Drawable img = ContextCompat.getDrawable(getActivity(), R.drawable.myphoto);
MyImage.setImageDrawable(img);

From URL and add to ImageView you need this code bro. I have different example for getting ImageView from URL, check below.

Example One Using Drawable For InpurStream Content
//Code Drawable
public static Drawable LoadImageFromWebOperations(String url) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
    } catch (Exception e) {
        return null;
    }
} 

//Usage
Drawable photo = LoadImageFromWebOperations("https://goo.gl/NUaQ4N")
ImageView image = (ImageView)findViewById(R.id.myimageView);
MyImage.setImageDrawable(photo);
Get ImageView by AsynTask Bitmap
//Code
    private class CodingSheepBitmap extends AsyncTask {

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            IMAGE.setImageBitmap(result); 
        }
    }

//Usage
ImageView image = (ImageView)findViewById(R.id.imageV);
new CodingSheepBitmap().execute("https://goo.gl/NUaQ4N");

//Set Image Bitmap From AsynTask OnPostExecute
IMAGE.setImageBitmap(result); 

Getting Fast Using UniversalImageLoader Using The Library

For this i just give you to know, :D But in future dont using any library. It will makes you independent for coding.
Add this to dependency app > build.grandle
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.4'
Code on activity
//For Configuration and Setup Bitmap
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .resetViewBeforeLoading(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .delayBeforeLoading(0)
                .imageScaleType(ImageScaleType.EXACTLY)
                .build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
                .defaultDisplayImageOptions(defaultOptions)
                .build();
        ImageLoader.getInstance().init(config);
This code for create bitmap and add to ImageView
//Define ImageView
ImageView image = (ImageView)findViewById(R.id.ImageV);

//Getting Bitmap
ImageLoader.getInstance().displayImage("https://goo.gl/NUaQ4N",  image , new ImageLoadingListener() {
                @Override
                public void onLoadingStarted(String imageUri, View view) {
                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                }

                @Override
                public void onLoadingCancelled(String imageUri, View view) {
                }
            });

Whats is help you, hehehe. GoodJob and SeeYouSoon, Great ImageView and dont try to stop increase and make your application beauty :D

1 Response to "Android ImageView by URL, Drawable and Library"