How to speed up image loading from a server - Android - android

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.

Related

Correct method to display Bitmap from url on ImageView that works on emulator and physical device?

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.

Corrupted Images From BitmapFactory

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

Why size of image is not same when i download an image from a url?

I am downloading an image from a url and create a bitmap image in my android app. Size of image is 126 kb. When i create an image in my app by downloading it from url then size is around 3.25 mb. I am using following code to downlad image from url:
URL urlConnection = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) urlConnection.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
Why downloaded size of image is bigger and is there any way to download it in the same size? Thanks in advance.
Image you download is probably in some compressed format (.jpg .png). Bitmap you get from BitmapFactory.decodeStream on the other hand is in uncompressed format.
If you dont need to show your bitmap in original resolution you can scale it down (detailed instructions here). Otherwise such memory usage is normal.
Android converts this image to bmp. You can convert it by youself and you get size ~3.5 mb.
Dont use Bitmapfactory for downloads. Use an InputStream and a FileOutputStream to save to internal, external or removable memory directly.
Image size depends on bits per pixel .during conversion you have to give bits per pixel
file size is given by Resolution^2 x Width x Height x Bits per sample รท 8,192

Show reCaptcha from third-party site

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?

bad bitmap error when setting Uri

I would like to make an ImageView display an image on a website. So I create a new ImageView and execute imgView.setImageURI(uri);
When I launch the app the image does not appear and I get the error, "resolveUri failed on bad Bitmap Uri (uri)".
Any ideas on how to solve this?
If it's not a content URI, this link may help. It seems to indicate that imgView.setImageURI() should not be used for regular URIs. Copying in the relevant bit:
Yes, ImageView.setImageURI(ContentURI uri) works, but it is for content URIs particular to the Android platform, not URIs specifying Internet resources. The convention is applied to binary objects (images, for example) which cannot be exposed directly through a ContentProvider's Cursor methods. Instead, a String reference is used to reference a distinct content URI, which can be resolved by a separate query against the content provider. The setImageURI method is simply a wrapper to perform those steps for you.
I have tested this usage of setImageView, and it does work as expected. For your usage, though, I'd look at BitmapFactory.decodeStream() and URL.openStream().
Also to make this answer self-contained, the sample code from another post at that link, showing how to do it:
private Bitmap getImageBitmap(String url) {
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
Log.e(TAG, "Error getting bitmap", e);
}
return bm;
}
I haven't tested this code, I'm just paranoid and like to ensure SO answers are useful even if every other site on the net disappears :-)
Glide is an awesome library for displaying images!
Glide.with(context)
.load(uri)
.into(imageView);
You need to download the image and then set it as bitmap.
Here is one of the many examples:
http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html
Use library Picasso
Manifest
uses-permission android:name="android.permission.INTERNET"
build gradle
implementation 'com.squareup.picasso:picasso:2.71828'
Activity:
Picasso.get().load(photoUrl).into(imageView);
First, you should download the image and save it in your device(sdcard or memory).
Then, get its file path, using Uri.parse(filePath) to convert path to uri
finally, call ImageView's setImageURI(Uri) to fullfill.
-- I use this way to achieve my purpose and there is a bug:if the image is to large(maybe exceed 1Mb or so, it may report outOfMemeroy Exception!!!)

Categories

Resources