Loading bitmap to ImageView using Async - android

I am loading a bitmap asynchronous (ASyncTask) to the ImageView (its a background picture). The user can pick a custom background picture. When I start the application, I am loading the bitmap to the ImageView, but that takes some time (like 0.3 seconds). In this 0.3 seconds, I see the default background (black screen). I do like to see the custom background immediately.
How can I achieve this?
EDIT
With Glide I get exactly the same result, again a black screen for half a second.
If I use this code, without async, it works, but is good practise?
inputStream = new FileInputStream(createFile());
return BitmapFactory.decodeStream(inputStream);

Take a look at Picasso Library
http://square.github.io/picasso/
It works like this :
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
With internal storage bitmap :
Picasso.with(context).load(new File(path)).into(imageView);
You can add an image that will be displayed during loading :
.placeholder(getResources().getDrawable(R.drawable.default_image))

Related

(Android) How to download an image/gif without a black background?

I am using Glide to get an image as a byte array as such
Glide.with(context)
.as(byte[].class)
.load(url)
.submit()
.get();
and it works well with an image with a background, as well as GIFs. However, when there is a static image with a transparent background (.PNG), it will produce a black background instead of keeping its transparency. I know you can avoid this problem by using a bitmap, but I want to retain both .PNG and .GIF files. Is there a possible way to retain the transparent background when loading a byte array using Glide?

How can I use the glide to load partial image from Internet

My Android application need load amounts image from Internet with api,those pictures so big and massive that need more Lots of bandwidth,So I want load partial(compress) image to save bandwidth.
At first,I tried to use the 'thumbnail' of Glide,it was seem work,the image is compressed but it load full image final.
Then,I tried to use the 'override' of Glide,but it only resized the picture in ImageView,The full image is downloaded too.
//The Thumbnail() can load the partial image at first,but download full picture final
Glide.with(mContext).load(item.imgUrl).thumbnail( 0.5f )
.apply(bitmapTransform(new RoundedCornersTransformation(50, 0)))
.into(myViewHolder.commodityImage);
//The override() can resize the picture inside ImageView,but it download the full picture too!
Glide.with(mContext).load(item.imgUrl)
.apply(bitmapTransform(new RoundedCornersTransformation(50, 0)).override(400,400))
.into(myViewHolder.commodityImage);
So,is there a way to download the image partially like the Glide thumbnial but do not download full picture.
Try to used this..
Glide.with(imageView)
.load(path)
.apply(RequestOptions().placeholder(R.drawable.place_holder).override(500, Target.SIZE_ORIGINAL))
.into(imageView)

RenderScript blur breaks bitmap loaded by Glide

So, the thing is that I am fetching some images using Glide. I fetch them directly into bitmap, and then I blur that bitmap using RenderScript and than show on UI blurred one.
UI itself has "All Images" activity and "Single Image" activity. User clicks on the image on the first activity, and the blurred version is showed on the second one, so it is possible to go back and forth opening and closing the same image.
The issue is that this cause image to become broken, and there is no way to fix it unless you clear all app data.
The issue even survives app reinstall (using Android Studio). So, if I open image, and it is displayed as it should, than make some changes in code, and install the app again, image would show broken immediately after
installation, unless I clear data.
It happens only with bitmaps loaded using glide. If I get some drawable resource as bitmap, everything works well.
UPDATE:
This is the code used here;
Bitmap logo = Glide.with(context)
.load(url)
.asBitmap()
.into(80, 80)
.get();
return BlurBuilder.blur(context, logo);
And BlurBuilder is the class copied from here: Create blurry transparent background effect
Had the same problem. Solved it by setting the Glide decode format to ARGB_8888
https://github.com/bumptech/glide/wiki/Configuration#bitmap-format

Android image view makes app slow

In my app I have a list view, which contains an image view. The image is loaded from bas64 encoded string. First the string is decoded and then converted it to bitmap and then the bitmap is loaded to the image view.
All the decoding is done in an async task and concurrency is handled according to the below documentation.
Processing Bitmaps Off the UI Thread
The problem is the app is scrolling slow, and all other async tasks are not executing after that.
any possible solutions?
You probably insterting a huge Bitmap into a tiny ImageView like assume the image is FHD 1920x1280 and your ImageView is 192x128. You should load a smaller or same size Bitmap into ImageView. I guess this is a reason of scroll lags. Also it could be that your layout is too complex and should be optimized.
As for
async tasks are not executing after that
nobody can tell you anything without seeing your code.
If you are getting image URL from server you can store if you want to in local DB. Don't store base64 in database just store the image URL's.But for image loading you can simply use libraries that are available like Glide or Picasso for image loading. Your list view is lagging as you are doing heavy operation like decoding base64 and loading that bitmap. Just give it a try, it will work. You can load image using single line code like
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
Glide.with(this).load("http://goo.gl/gEgYUd").into(imageView);
you might need to put images into a folder "drawable-nodpi", since normaly android app will try to resize large image first, then load it, then show show the original image(by resize again), this process will cost much CPU which make your app respond slow. So just create a "drawable-nodpi" folder as the "drawable" folder, cut/paste all large images in the new folder

Load bitmap lazy, just before it is seen

I've a Listview with images. I am using an adapter to display the images because i get just a URL from a web service. At the moment i load all images Async, that works but its very inefficient. I get 100-200 images / URLs and mostly i do not need all.
I am looking for a solution to load a bitmap just before it is seen.
for example:
Bitmap 1 (i see it on the screen -> load bitmap)
Bitmap 2 (i see it on the screen -> load bitmap)
Bitmap 3 (i don't see it on the screen but its the next one -> load bitmap)
Bitmap 4 (i don't see it, nothing to do)
Bitmap 5 (i don't see it, nothing to do)
How can I do this?
You should implement the Holder pattern for your listview. See the documentation here
I use Universal Image Loader library, and lazy loading works perfect, take a look at it here:
Universal Image Loader at Github

Categories

Resources