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.
Related
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);
}
}
}
}
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.
I have a problem with loading favicon.ico from some sites.
For example http://deti.fm/favicon.ico.
On decktop computer it loads good, but on android device it loads with defect pixels.
Here is screenshot
https://www.dropbox.com/s/2wt73pwk2jnj86b/favicon.jpg
Here is code
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);
bitmap = BitmapFactory.decodeStream(is);
is.close();
conn.disconnect();
os.close();
return bitmap;
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;
}
I am android newbie, I have following code:
URL url = new URL(userpic);
URLConnection conn = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection)conn;
httpConn.setRequestMethod("GET");
httpConn.connect();
if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream inputStream = httpConn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
inputStream.close();
// make a Drawable from Bitmap to allow to set the BitMap
gdrawable = new BitmapDrawable(bitmap);
myNotification = new Notification(gdrawable, "Notification!", System.currentTimeMillis());
for these code "Notification(gdrawable, "Notification!",System.currentTimeMillis());" which will get an error, it need me to get int id.
How can I get gdrawable ID?
You can put image file in res->drawable (or drawable-hdpi,ldpi,mdpi), for example notify.png
and use it
myNotification = new Notification(R.id.notify,
"Notification!", System.currentTimeMillis());