The following steps allow me to fetch an image kept on my TomCat server. but i am unable to access an image from google's web site . What could be the reason ?
URL aURL = new URL(url);
URLConnection con = aURL.openConnection();
con.connect();
InputStream is = con.getInputStream();
/* Buffered is always good for a performance plus. */
BufferedInputStream bis = new BufferedInputStream(is);
/* Decode url-data to a bitmap. */
bm = BitmapFactory.decodeStream(bis);
bitmapDrawable = new BitmapDrawable(bm);
My URL :http://172.29.26.34:8080/MyService/hb.gif
Google image :
http://www.google.co.in/imgres?imgurl=http://www.androidguys.com/wp-content/uploads/2010/04/android_apps.jpeg&imgrefurl=http://www.androidguys.com/2010/04/25/hardware-standards-android-handset-manufacturers-implement/&usg=__VCGb9CrKDVzOjFJxQ0yFHGewblk=&h=356&w=570&sz=156&hl=en&start=0&sig2=N3UL3C995r94inBx8X822w&zoom=1&tbnid=wISGPUJqRDxB7M:&tbnh=127&tbnw=165&ei=h3-ITKDXMov0vQOsg7GPCQ&prev=/images%3Fq%3Dandroid%2Bimages%26um%3D1%26hl%3Den%26sa%3DN%26biw%3D963%26bih%3D525%26tbs%3Disch:1&um=1&itbs=1&iact=rc&dur=188&oei=h3-ITKDXMov0vQOsg7GPCQ&esq=1&page=1&ndsp=15&ved=1t:429,r:2,s:0&tx=107&ty=75
It is because the google link you pasted is the link for an image viewer in a frame, not for the picture itself. (And actually, strictly speaking the image is not from google website, it was just indexed and displayed by google)
In the right column there is a link 'view full size picture' to go to the real image.
If you use pictures from the web on your website, be sure to check the license if it is acceptable to do so or seek permission.
Set User-Agent
URLConnection con = aURL.openConnection();
con.setRequestProperty("User-Agent","");
Related
Help please solve my problem. I use the code for asynchronous image downloading from this site http://www.technotalkative.com/android-asynchronous-image-loading-in-listview/. With HTTP it works as it should, but if I change the piece when with
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is=conn.getInputStream();
On this one
Socket socket1 = new Socket();
socket1.connect(new InetSocketAddress(mSettings.getString(APP_PREFERENCES_IPSERVER, ""), 30100), 5000);
ObjectOutputStream outp1 = new ObjectOutputStream(socket1.getOutputStream());
ObjectInputStream inp1 = new ObjectInputStream(socket1.getInputStream());
outp1.writeObject("ASprUserEdir,-,fileget,-," + url);
outp1.flush();
String messageReceived = (String) inp1.readObject();
int file_size = Integer.valueOf(messageReceived.split(",-,")[1]);
InputStream is=socket1.getInputStream();
Then if the connection is slow, if you scroll the Sheet, the picture is not displayed in its place. How can this be fixed? Maybe need a different code for this.
Thank you!
When you try to download an image you need to consider few problem. Example caching, out of memory due to the image size and more and more.
The simlest way is using library such as Picasso or glide. They download the image asynchronous, caching the image, memory caching to prevent out of memory etc. It is easy to learn and use.
Use Library Picasso or Glide
and then used following code
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Please go through the following link for more detail on Picasso
Once in a while when I am downloading images, some of them will appear with gray lines as displayed in the sample image below:
This tends to happen exclusively on poor network conditions.
My code for decoding images is essentially a modded version of that from UIL:
Bitmap decodedBitmap = BitmapFactory.decodeStream(stream, null, decodingOptions);
where stream is just an InputStream to the remote file
Now I've seen this behaviour on the Google Play Music app which makes me wonder if it's an issue with the BitmapFactory.
The only solution I've come up with so far is maybe doing a checksum comparison to make sure the entire image was downloaded.
Edit:
HttpURLConnection conn = getConnection(path);
InputStream inputStream = conn.getInputStream()
stream = new ContentLengthInputStream(new BufferedInputStream(download, BUFFER_SIZE), conn.getContentLength())
Solved it by using the following steps:
Open an InputStream to the image as normal
Write the stream to a ByteArrayOutputStream
Compare the length of the ByteArrayOutputStream to the Content-Length header from the HttpURLConnection
If mismatch then you've lost data in transit
Otherwise you can convert your ByteArrayOutputStream to a ByteArrayInputStream and use with the BitmapFactory as you see fit
So I want my app to scrape some data from various sites, and one of them shows a recaptcha.
I was able to detect it in my app but I'm having problems figuring out a way to show the captcha to the user. The site is loaded in background with AndroidHttpClient.
I did some research and found nothing that could help me accomplish this for a third party site. The captcha needs to be solved and my quasi-browser (the androidhttpclient which just loads a bunch of sites in background) needs to get access to the site to gather the needed data.
I'm doing a similar project and I used a bitmap to capture the image (captcha) e show to the user in Android.
public Bitmap getCodigoCaptcha(String src){
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(inputStream);
return myBitmap;
}
catch(Exception e){
e.printStackTrace();
return null;
}
}
I get this bitmap from the url that I need and set a ImageView in Android to show to the user.
Why not use a TSimpleCaptcha or JCaptcha?
I am still a beginner in Android and java. I am trying to load an image from a server by using AsyncTask. The size of the image is around 50kb. Yet it takes several seconds to show up. The following code is used to download the image from the server.
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();
BufferedInputStream bis = new BufferedInputStream(input);
Bitmap myBitmap = BitmapFactory.decodeStream(bis);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
Can someone please tell me how to speed up this process. What are the factors this process depends on apart from the network speed?
Thank you in advance.
Firstly, I suggest you to search image size reduce algorithms. Then, you can choose some methologies to load picture. For example, in first step, you can load low sized images. After that , you can load normal size images in background and update each reduced sized images to normal sized images.
Here is a great resource on loading Bitmaps efficiently.
Android Developer site - Loading Large Bitmaps Efficiently
It also includes a code sample.
i uploaded image to facebook using facebook SDK example. It looks like as show below :
image after uploading to facebook
image before uploading to facebook which is on my server
is anyone facing same problem with the image after uploading image on facebook?? If so how to solve this problem?
I am using the same code which is given in Facebook-Android Example:
HttpURLConnection conn= (HttpURLConnection)uploadFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
int length = conn.getContentLength();
byte[] imgData =new byte[length];
InputStream is = conn.getInputStream();
is.read(imgData);
params.putByteArray("picture", imgData);
Thanks in advance, aby