Wrong display favicon.ico on android device - android

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;

Related

android send image to servlet using the imageview and HttpPost

I am developing an android application in which I use ImageView to capture an image using camera.
So how can I send the captured image in imageview to a java servlet as multipart using httppost.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageView view = (ImageView)findViewById(R.id.imageView1);
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bm = view.getDrawingCache();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
Thanks.
Take a look here: http://developer.android.com/reference/java/net/HttpURLConnection.html
Example code:
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);
OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
writeStream(out);
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
finally {
urlConnection.disconnect();
}
Please refer to section Posting Content.

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

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.

Bitmap as Notification icon

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

Categories

Resources