Android: load images with zoom from URL - android

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?

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);

I actually solved this by creating a Bitmap from the provided URL string extra, although I think this may defeat the purpose of SubsamplingScaleImageView because I'm loading the entire Bitmap.
private void locateImageView() throws URISyntaxException, IOException {
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
if (bundle.getString("imageUrl") != null) {
String imageUrl = bundle.getString("imageUrl");
Log.w(getClass().toString(), imageUrl);
imageView = (SubsamplingScaleImageView) findViewById(R.id.image);
URL newUrl = new URL(imageUrl);
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
imageView.setImage(ImageSource.bitmap(myBitmap));
} catch (IOException e) {
// Log exception
Log.w(getClass().toString(), e);
}
}
}
}

Related

Android with plus sign ("+") in url

I cannot download the picture:
http://www.wallpick.com/wp-content/uploads/2014/02/08/Water+Sports_wallpapers_242-640x480.jpg
This is my code:
// from web
try {
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setConnectTimeout(25000);
conn.setReadTimeout(25000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
// save file to m_FileCache
copyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Throwable ex) {
return null;
}
With this code, I can download all image urls as:
http://www.wallpick.com/wp-content/uploads/2014/02/08/pictures-of-lotus-flowers-on-water-640x480.jpg
Root cause is plus sign ("+") in first link. Please help me! Thank you very much!
You can use Uri builder classes. As an example,
String url = Uri.parse("http://www.wallpick.com/wp-content/uploads/2014/02/08/").buildUpon()
.appendEncodedPath("Water+Sports_wallpapers_242-640x480.jpg")
.build().toString();
This will correctly encode your url String.

Download image URL contains "è"

I try to download the image in the following url:
http://upload.tapcrowd.com//cache//_cp_100_100_stand_filière_300x212.jpg
As you can see in the browser this shows an image, but in my app I get a FileNotFoundException.
However if i change the url of the image from "è" to "e". I can succesfully download it into my app. This however is only a temporary solution as it needs to be able to download images with unicode sign.
How can I achieve this?
Method used to download images:
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f, maxheight, maxwidth);
result code that works for me:
Bitmap bitmap = null;
int slashIndex = url.lastIndexOf('/');
String filename = url.substring(slashIndex + 1);
filename = URLEncoder.encode(filename, "UTF-8");
url = url.subSequence(0, slashIndex + 1) + filename;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f, maxheight, maxwidth);
Encode the url using URLEncoder:
String baseUrl = "http://upload.tapcrowd.com//cache//";
String imageName = "_cp_100_100_stand_filière_300x212.jpg";
URL imageUrl = new URL(baseUrl+URLEncoder.encode(imageName ,"UTF-8"));
It works with your browser, because browser is smart enough to do the encoding when you type accent in your url bar.

How to get bitmap from a url in android? [duplicate]

This question already has answers here:
Android load from URL to Bitmap
(20 answers)
Closed 7 years ago.
I have a uri like which has an image
file:///mnt/...............
How to use this uri to get the image but it returns null, please tell me where i am wrong.
Bitmap bitmap = BitmapFactory.decodeFile(uri.getPath());
Bitmap bitmap = BitmapFactory.decodeFile(uri.toString());
This is a simple one line way to do it:
try {
URL url = new URL("http://....");
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch(IOException e) {
System.out.println(e);
}
This should do the trick:
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
} // Author: silentnuke
Don't forget to add the internet permission in your manifest.
Okay so you are trying to get a bitmap from a file? Title says URL. Anyways, when you are getting files from external storage in Android you should never use a direct path. Instead call getExternalStorageDirectory() like so:
File bitmapFile = new File(Environment.getExternalStorageDirectory() + "/" + PATH_TO_IMAGE);
Bitmap bitmap = BitmapFactory.decodeFile(bitmapFile);
getExternalStorageDirectory() gives you the path to the SD card.
Also you need to declare the WRITE_EXTERNAL_STORAGE permission in the Manifest.

Problem in loading images from URL in android

I am making an application in android which loads Icon size images from URL
i have tried downloading images using the following code.
One image labeled default.png was downloaded from the given url but there was another image labeled v_1234.jpg is not being downloaded. I dont know whats the problem. it just returns me null for jpg image.
I am not sure that its a problem for .jpg format that my code is not downloading the jpg format images or Its the labeled name problem that due to Underscore (_) in the label makes it not downloadable..
Please help Friends you are professional in that field.
CODE:
URL url = new URL(detail.voucher_image.toString());
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.getImageBitmap(bmp);
Thanks alot.
try this code
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
InputStream reader;
reader=conn.getInputStream();
System.out.println("Compressed2!!!"+conn.getContentLength());
int available = reader.available();
int i=0;
int count=0;
int cc=0;
while(reader.read()!=-1){
cc++;
}
System.out.println("available"+cc);
data2 = new byte[cc];
while ((i = reader.read(data2, count, data2.length-count)) != -1) {
count +=i;
cc++;
}
System.out.println("Compressed3!!!");
// reader.read(data2,0,cc);
System.out.println("Compressed!!!");
// printBytes(data1,data2,"after");
System.out.println("length b4!!!"+data2);
System.out.println("data::"+new String(data2));
System.out.println("The length is "+data2.length);
bmp2=BitmapFactory.decodeByteArray(data2, 0, data2.length);
if(bmp2==null)
System.out.println("The bitmap value is null");
iv.setImageBitmap(bmp2);undefined
use the following code to get bitmap from url
public Bitmap imageConvert(String url){
URL aURL = null;
Bitmap bm = null;
try {
final String imageUrl =imgstr.replaceAll(" ","%20");
Log.e("Image Url",imageUrl);
aURL = new URL(imageUrl);
URLConnection conn = aURL.openConnection();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(new PatchInputStream(is));
is.close();
}
catch (Exception e) {
Log.e("ProPic Exception",e.getMessage());
}
return bm;
}

how to display external image in android?

I want to display external image like:
"http://abc.com/image.jpg"
in my android phone application.
can any one guide me how to achieve this?
There are many ways to achieve your request. Basically you have to download the image with an urlrequest and then using the InputStream to create a Bitmap object.
Just a sample code:
URL url = new URL("http://asd.jpg");
URLConnection conn = url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
After you obtain the Bitmap object you can use it on your ImageView for instance
Just another approach to download the image from a url
try {
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("http://abc.com/image.jpg").getContent());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Categories

Resources