Getting JPEG with android

107 Views Asked by At

What is the laziest method to get JPEG from url and get it to use in my app? I would like to achieve it without any new libraries to my project.

Sincerely, Peter.

2

There are 2 best solutions below

0
On

You can also use ImageLoader

DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .cacheOnDisc(true).cacheInMemory(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .displayer(new FadeInBitmapDisplayer(300)).build();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                getApplicationContext())
                .defaultDisplayImageOptions(defaultOptions)
                .memoryCache(new WeakMemoryCache())
                .discCacheSize(100 * 1024 * 1024).build();

        ImageLoader.getInstance().init(config);

//your image url
String url = "http://javatechig.com/wp-content/uploads/2014/05/UniversalImageLoader-620x405.jpg";

ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
                .cacheOnDisc(true).resetViewBeforeLoading(true)
                .showImageForEmptyUri(fallback)
                .showImageOnFail(fallback)
                .showImageOnLoading(fallback).build();

//initialize image view
ImageView imageView = (ImageView) findViewById(R.id.imageView1)     

//download and display image from url
imageLoader.displayImage(url, imageView, options);

For more details , you can visit this post ImageLoader

1
On
URL webUrl = new URL("https://cdn2.iconfinder.com/data/icons/windows-8-metro-style/512/cool.png");
URLConnection connection = webUrl.openConnection();
Bitmap bitmap = BitmapFactory.decodeStream(connection.getInputStream());

but beware you can't run this code on UI Thread, it should be done on Thread or AsyncTask