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
Related
I am building an Android app for local media and parse the relevant images and such from The Movie Database API, Everything is working fine on the Android Studio emulator but when i build the apk and install on a real device none of the downloaded images are displayed on their ImageViews.
The code i am using is;
String Url = Actionlist1.getString(t);//pasrsed from a JSONArray object
java.net.URL url = new java.net.URL(Url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap b = BitmapFactory.decodeStream(input);
Bitmap b1 = Bitmap.createScaledBitmap(b, 100, 140, false);//hard coded scaling to the ImageView width and height.
ImageView B1 = findViewById(R.id.Button1);
B1.setImageBitmap(b1);
This code works fine when ran on the emulator but when ran on a device all images are black, i'm at a loss as the code should work and other than the image being missing the app behaves correctly.
Try this
Glide.with(this)
.asBitmap()
.load(//url)
.placeholder(R.drawable.vijay)
.into(imageView);
add this dependency also
implementation 'com.github.bumptech.glide:glide:4.9.0'
Just add this line in your app.gradle file
implementation 'com.squareup.picasso:picasso:2.71828'
after that you can easily load your image from online.
Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(imageView);
Going to drop the answer in for any one who faces the same problem. The discrepancy between the network methods used between the Android versions, newer versions will not load image assets that did not come from a secure connection. If your facing the same issue change the URL as below;
//Old URL
String URL= "http://image.tmdb.org/t/p/w500/l4iknLOenijaB85Zyb5SxH1gGz8.jpg";
//New URL
String URL= "https://image.tmdb.org/t/p/w500/l4iknLOenijaB85Zyb5SxH1gGz8.jpg";
the s really does make all the difference.
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?
So I am trying to load about 25 images at once using the AsyncTask class.
In short I'm calling this inside the AsyncTask to download the image from the server:
Url = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection) Url.openConnection();
bitmap = BitmapFactory.decodeStream(urlConnection.getInputStream());
urlConnection.disconnect();
As you can see in this video, it results in poor performance. Presumably because it is loading the images one at a time as the AsyncTask queues each of them up, I think?
http://www.youtube.com/watch?v=7dqVqLn5Ibs
So the solution would be to load them all asynchronously, but I dont know how I would achieve this without needed a whole lot of new code?
You can do that easily using Universal image loader.
It loads each image in separate thread.It is very easy to use and takes care of most of the things like caching..etc.
Ex usage:
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(context));
imageLoader.displayImage(imageUrl, imageView);
I want to download an image from server, I am able to download some images but not all, I dont understand why. I have search all over the net but did not find any solution. my code is
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
drawable= Drawable.createFromStream(conn.getInputStream(), "");
I have also tried this code :
bitmap = BitmapFactory.decodeStream(new URL(url).openConnection().getInputStream());
nothing seem to work both bitmap and drawable are null.
Did you check the return code of the HttpURLConnection by using the getResponseCode method? Maybe the resource wasn't found or it contains a redirect.
I'm currently developing an Android application that fetches images using http requests. It would be quite swell if I could cache those images in order to improve to performance and bandwidth use.
I came across the CacheManager class in the Android reference, but I don't really know how to use it, or what it really does.
I already scoped through this example, but I need some help understanding it:
/core/java/android/webkit/gears/ApacheHttpRequestAndroid.java
Also, the reference states:
"Network requests are provided to this component and if they can not be resolved by the cache, the HTTP headers are attached, as appropriate, to the request for revalidation of content."
I'm not sure what this means or how it would work for me, since CacheManager's getCacheFile accepts only a String URL and a Map containing the headers. Not sure what the attachment mentioned means.
An explanation or a simple code example would really do my day. Thanks!
Update
Here's what I have right now. I am clearly doing it wrong, just don't know where.
public static Bitmap getRemoteImage(String imageUrl) {
URL aURL = null;
URLConnection conn = null;
Bitmap bmp = null;
CacheResult cache_result = CacheManager.getCacheFile(imageUrl, new HashMap());
if (cache_result == null) {
try {
aURL = new URL(imageUrl);
conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
cache_result = new CacheManager.CacheResult();
copyStream(is, cache_result.getOutputStream());
CacheManager.saveCacheFile(imageUrl, cache_result);
} catch (Exception e) {
return null;
}
}
bmp = BitmapFactory.decodeStream(cache_result.getInputStream());
return bmp;
}
I don't think the CacheManger can be used outside of a WebView as noted in this bug report
http://code.google.com/p/android/issues/detail?id=7222
I came across this issue awhile ago as well. The cache manager is only for the webview and not really useful outside of that. For my application I needed to cache xml responses and images so I ended up writing my own cache manager to accomplish that. Nothing too terrible but certainly not as easy as using a would-be built-in one.
If you have any questions about the specifics, add a comment to my post, I check back frequently.