Is there a way to load the images that will come on the screen soon as opposed to loading the images that are currently in view?
If you're asking how to pre-load images so they are freshly cached, so that when the corresponding ImageView comes on screen the loading is nice and fast, you could use (somewhat untested):
String uri = getUriOfImageAboutToComeOnScreen(...);
ImageLoader.getInstance().loadImage(uri, null /* or use a listener */);
Of course, the magic contained in getUriOfImageAboutToComeOnScreen() is up to you :)
Related
I need a bitmap to work with and I am provided an URI that is shared from another apps implicit intent. I currently work with
Bitmap btmp = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), urinormal);
but it takes up to half a second to create the bitmap. I need it to work as fast as possible, what can I do?
I can't find a way to reduce the quality of the bitmap, I can only apply BitmapFactory.options afterwards to create a smaller bitmap, but that doesn't help.
There can be many reasons why image loads slow. Slow connection, large file etc.
I would suggest you to use Glide, an image loading library for android.
GlideApp
.with(ctx)
.load(url)
.centerCrop()
.placeholder(R.drawable.loading_spinner)
.into(myImageView);
This will show a custom loading image until the image is loaded.
You will have to use Generated API of Glide to have GlideApp class though. Read the docs, there are many other cool features.
Hope it helps.
Probably the file size is too large. If it is large it takes the required time. You can do nothing about it.
If it takes around half seconds, you may want to perform the decoding using AsyncTask so that the UI won't freeze.
As you mentioned that you are developing a gallery type app. You may want to store the thumbnail of the bitmaps in the storage when loading them the first time. So next time your app can quickly decode the image from the thumbnail file. When you click on the image, you can load full size image from the original location.
As #faztp12 mentioned in his answer, you may also use some third party image loading libraries like Picasso or Glide to achieve the similar functionality.
I have a big set of images saved in one atlas, but due to the 2048*2048 size limitation of a single page I had to split my packs in 12 pages.
My problem is that when I use atlas.findRegion(imageName).getTexture() to load the texture, it loads the whole 2048*2048 page and not the single texture inside the page (the image pack contains the correct image though, so I know it's loading the correct file, I just don't understand why it's not cropping the texture around the image I pass as parameter to findRegion).
Thanks in advance, hope the question is detailed enough to receive an answer.
The problem was actually calling the getTexture() method, I just needed to load the Region. More info here loading regions of textureatlas in libgdx loads the entire file
I am trying to change an image in my app depending on the position of the phone using the accelerometer. This isn't where I'm stuck, its not to hard to do.
I have an array of images from my drawable folders that change depending on the angle of the device. This works fine (switching the image of the imageview), with low res images but what would like to know is, is there a way or preloading images in java like I can in JavaScript when creating rollover images. So the hi res images would change seamlessly like the low res images.
Yes.
Create a subclass MyApp of Application.
In MApp.onCreate() you can preload your images; e.g.
image1 = getResources().getDrawable (R.drawable.image1);
And in your app, you simply do something like
image1 = ((MyApp)getApplication()).getImage1();
where getImage1 is a getter you provide in MyApp.
Hope this works for you!
https://github.com/luckybilly/PreLoader can resolve your problem.
you can pre-load almost everything you need with PreLoader. And you don`t need to worry about it not completed when you should get it: DataListener.onDataArrived(data) will be called after pre-load is completed
Im creating a simple application that converts the image dimensions to a size that the user wants, im laying it out over a 3 screens
the first screen allows a user to select an image and it is displayed on screen.
the second screen then displays the attributes of the file (path, name, height and width) with the option of adjusting the name, height and width.
the third screen displays the resized image with an option to save the new image.
at the moment im only passing the image url between the classes and decoding the bitmap within each class eg
Bitmap bmap = BitmapFactory.decodeImage(image_URL);
my question is it better to pass the URL between the classes or pass the Bitmap?
many thanks
You have several options:
Share the image through static data - Preferably in your
Application class. Example: Using a class to store static data in Java?
Pass the image through the Intent data which is possible, but
highly discouraged.
Pass the URL as you suggest and re-open and resize it each time.
Use a combination. For example, you should pass only image
information to your second screen which displays said information.
I suggest that the first bullet is best practice and will work well for you.
That should be the correct approach. Passing Bitmap shouldn't be done from one activity to another. Passing the path is always recommended
I think that dependent on where is your image if (you have the image on device ) I think the best is to pass Image path , but if you have the image on web and you have to download it I think passing bitmap is better than pass URL(Just to decries internet connection & faster for the user )
Let us consider this code snippet
Image img=Toolkit.getDefaultToolkit().getImage("1.png");
g.drawImage(img,0,0,null);
What the code does is load the image 1.png and draw it on a graphic context. Now what I observe is drawImagedo not draw any image the first time it is called. Instead it draws the image on further calls. Now I think this behavior is due to asynchronous image load or the lazy behavior of the method.
To correct the problem what I could do is use the javax.swing.ImageIcon class like this.
Image img=new ImageIcon("1.png").getImage();
g.drawImage(img,0,0,null);
I want to know what are the other better ways to perform the same task.
It depends.
If you are loading the image over a "slow" connection (like the internet) or you are loading a sizable image, it's best to use a lazy loading approach.
This allows the application to continue running while the image is begin loaded.
Andrew is correct, you should be using g.drawImage(img,0,0,this), as this allows the component to automatically update itself once the image has completed loading, without you needing to perform any other actions.
If you application relies on the image for some part of it's operation OR you are loading small images locally, it would be sufficent to use something like...
BufferedImage image = ImageIO.read(imageResource);
// Where image resource is either a file or local URL (such as an embedded resource)
I personally, tend to use a background thread to load my images in most cases and use ImageIO. In my paint method, I might use a small placeholder image if required to let the user know that I am loading the image.
You will also need to take into consideration the type of image you are loading as well. While ImageIO has a larger support for image formats, loading animated GIFs is problematic and requires significant more work on you part to achieve.
ps - Don't load images in you component paint methods - paint can be called numerous times and loading images (or other resources) will dramatically slow the repaint process, making your application lag...
Try using ImageIO.read() for loading the images. This will block until the image is fully loaded. Check this page.