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.
Related
I have image saved in phone memory. I need to get some part of the entire image in desired resolution. I could possibly first downscale the entire image and then "decodeRegion" from it. Is there any way to "decodeDownScaledRegion" in one go rather than first downscaling original image and decoding region from it?
Maybe you could follow the document "Loading Large Bitmaps Efficiently" here:
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
I am working on an application where i need to load the server images in list. But the way of displaying images is bit different. I need to display image as it gets downloaded i.e. pixel by pixel. Initially the image sets as blur image than as it gets downloaded, its gets more sharper and results into the original image. If you want to see what i am talking about than you can refer to the below link:
Progressive Image Rendering
In this you can see the demo of the superior PNG style two-dimensional interlaced rendering(the second one). I goggled a lot but did not find any relevant answer to my query.
Any sort of help would be appreciable.
Thanks in advance.
Try Facebook's Freso project: http://frescolib.org/#streaming
If you use Glide, the library recommended by Google for loading images in Android, you can achieve this kind of behavior with .thumbnail this receives the fraction of the resolution you'll like your image to show while the full resolution finishes loading.
Glide.with( context )
.load( "image url" )
.thumbnail( 0.1f ) //rate of the full resoultion to show while loading
into( imageView ); //ImageView where you want to display the image
Here is a more extensive explanation of the library and the thumbnails:
https://futurestud.io/blog/glide-thumbnails
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 :)
I want to edit the photo that was just taken by the camera in my app. Got everything working in terms of taking a picture and saving that picture to the gallery; however, I want to get that picture before saving it so I can overlay an logo on it. My idea was running this bit of code in activity result:
/* Decode the JPEG file into a Bitmap */
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
then creating another bitmap with my logo and overlay that on the first bitmap. However the picture and drawable source are too big to decode into a bitmap and I get an OutOfMemoryError. I don't want to scale down my images, because it must be a high res image. What's the way to go now?
Set Android:LargeHeap="true" option in the Manifest file. And allow image loading action only when you have enough available memory. (this param is available I think only since Android 3.0)
There is no magic. If image exceeds available memory, then you could read and edit parts of image. But it's not the most convenient way to process images.
This is a bit of a followup to my last question: Canvas is drawing too slowly
Now that I can draw images more quickly, the problem I am faced with is that the actual loading of the images takes far too long.
In the app I am working on, the user is able to play back video frames (jpegs) in succession, as though he is viewing the video in realtime. I have been using BitmapFactory.decodeFile() to load each jpeg in a Bitmap. I'm unable to load all images at once since there are about 240 of them, and that would use up all of my heap space. What I have been doing is preloading up to 6 at a time into an array by way of a separate thread in order to cut down on the time it takes for each image to display.
Unfortunately, it takes somewhere between 50 and 90ms to load an image, and I need to show an image every 42ms. Is there a faster way to load images possibly?
For clarification, these images are in a folder on the SD card, and they are all 720x480 jpegs. I am sampling them at half that size to cut down on memory usage.
I ended up doing this quite a bit differently than I had originally envisioned. There was quite a bit to it, but here's the gist of how I achieved my goal:
All images are stored on SD card and written to one file (each image takes up X bytes in the file)
Use native code to read from and write to the image file
When requesting an image, I pass the index of the image in the list and a bitmap object (RGB_565) to the native code using a JNI wrapper
The native code locks the bitmap surface, writes pixel data (as a uint8_t**) directly to the bitmap, then unlocks it
The image is rendered to the screen
By doing it this way, I only needed to store one image in memory at a time, and I was able to bypass garbage collection (since the bitmap was only created once and then repopulated natively). I hope someone else might find this strategy useful.
Guess you already tried all methods in this tutorial http://www.higherpass.com/Android/Tutorials/Working-With-Images-In-Android/2/ and chosen the fastest. Maybe tweaking resizing can decrease loading time.
Best of all would of course be if you didn't have to resize the images at all. If you have full control of the images maybe you could try to pack them as sprites, see article http://www.droidnova.com/2d-sprite-animation-in-android,471.html