Charlie Charlie - 4 years ago 139
Android Question

Android: load images with zoom from URL

I am using to load my images with high resolution and zoom quality with the tutorial: https://github.com/davemorrissey/subsampling-scale-image-view

It uses images from

Assets
. How I could do to load images from a
URL
?

Answer Source

The class SubsamplingScaleImageView has also a method to load an image that isn't from Assets:

SubsamplingScaleImageView.setImageFile(String extFile)

So you can get the image from the URL and save it on internal storage. Then you can load the image using the path from internal storage. To get the image as a Bitmap from an URL:

URL myFileUrl = new URL (StringURL);
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();

InputStream is = conn.getInputStream();
Bitmap bm = BitmapFactory.decodeStream(is);

To save the Bitmap on internal storage:

FileOutputStream out = new FileOutputStream(context.getCacheDir() + filename);
bm.compress(Bitmap.CompressFormat.PNG, 90, out);
// Don't forget to add a finally clause to close the stream

Finally

SubsamplingScaleImageView imageView =  (SubsamplingScaleImageView)findViewById(R.id.imageView);
imageView.setImageFile(context.getCacheDir() + filename);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download