Image compression before uploading in android - android

I have a image uploading module in my app where user can select an image from the gallery. The problem is the size of the image can be upto 10MB. Which is very large, I want to apply some compression technique to these images before uploading them.
I did some research on the internet and found some libraries like ImageMagick, ImgMin which allows easy optimization of the images. Is there any way I can use them in my android project without the involvement of any backend server.
References:
ImgMin
https://github.com/rflynn/imgmin
ImageMagick
http://www.imagemagick.org/script/index.php

An easy option you can try is this method from the Bitmap class.
You can select the compression format of a bitmap and to optimise either the quality, or the file size. A downside is that the you need to get a Bitmap instance to start the compression, which may be something you don't want to do.

Related

Image loading from server

Which is the fastest and best way to get image from server in android
app.
1. Image url
2. base64 format
or any other format which load image very quick as on normal internet speed.
As mentioned here base64 format is %37 larger than normal image. So if you use base64 format, you will have larger image and longer download time. In this case, you need to use normal image url.
To download images as fast as possible, you can use Picasso or Glide.
Just use glide for faster image load in your application because glide uses caching of images and it loads an image after compressing it a little bit. Even Google recommend this library to developers , Google used this library in Gmail.

Preferred way to fetch/show images on Android?

Lets consider the following, I have an app with several dozen photos I want to show the user at any given time.
Right now I'm creating multiple files for each image, sizing them for different screen sizes and storing them in their respective drawable folders.
It's increasing the size of my app dramatically.
So here is my question: Is it possible to store the images on a server and use an image library like Picasso, Fresco or something else (open to anything) to fetch that image and scale it down for the device it's running on without risking running out of memory?
I'm worried that fetching a large image, loading it into memory and then resizing it will cause the same problem as trying to display it on older devices with little memory available to them.
You can write methods to request different images sizes from your server based on client info. Just write a method to measure the screen size and then request the appropriate image based on a URL endpoint (like http://myimageserver.com/images/ldpi/image1.png).
You can do optimization post-download, such as scaling, before saving the image to a local file store.
Using a reputable image loading library is a valid method (my own favourite is Glide).
The answer to your question really depends on the number of images you want to show! If there are lots, then yes storing them on a server is probably best, but also the most time-consuming and expensive (both in time and money).
Your other (easier) option is to keep the originals in the assets folder, and use your image loader to scale and load them for you. The correct path for an image in your assets folder is file:///android_asset/your_image_here.jpg. This way, you're only keeping one version of each photo in your apk and they'll load much faster.

Loading drawable from web file format to drawable, not using bitmap Android

I have a slight problem. I'm trying to dynamically load images (Jpeg, PNG, etc) from the web and display them in my Android App. This works 99% of the time. However, sometimes the images tend to be so large, that the bitmap blows through the available memory and causes OutOfMemoryExceptions.
I find that, even though a 3000px by 3000px Jpeg might be only 200 kilobytes, the resulting bitmap can be hundreds of megabytes. Is there a way to load Jpeg/PNG files from the web, into a drawable without converting to a bitmap first?
Playing with bitmap in android is a little tricky, I highly recommend you to read THIS and THIS link from android developer site to get better idea how to load bitmap into memory efficiently.
You can also use Universal Image Loader library which is very powerful and plugable library.
It takes care of loading bitmap efficiently into memory and there are lots of configuration for you. It also download the image for you from the web.
You can set memory or disc cache for it. Also specify amount of cache size.
You can provide you custom image downloader for example in case you need authentication.
You can set FileNameGenerator for cache files.
You can set to consider ExifParams.
Set image scale type
UIL can load bitmap from file based on its ImageView container size. So you have better memory management. instead of loading a big size image, it will load a scaled down version of image base on ImageView size.
and lost of other options

get pixels from jpeg byte array

I need to get the pixels info from a Jpeg image without instanciating a Bitmap and then pass it to the JNI.
It is impossible with android to get anything else than a jpeg image from camera (except if you need a low resolution in which case you can use thePreviewCallback), so I got the byte[] from the jpegCallback.
Is it possible to get pixels info in an int[] without using Bitmap.getPixels() ?
If you don't want to construct a Bitmap, your only option is to decode the JPEG buffer yourself; this means either finding another Java JPEG decompression library, or using JNI and a C JPEG library such as libjpeg. Or, you can write one from scratch, which I don't recommend unless you're already pretty conversant with image compression routines, and you have plenty of time for implementation and debugging.
As Scott asked, why is using Bitmap unacceptable? No matter what route you take, you'll have to call something to decompress the image data, and using BitmapFactory.decodeByteArray is a straightforward, known-to-work option.
I do not see how this is possible with the standard Android APIs. I'm sure there are 3rd party JPEG libraries you could use (Googling finds may possibilities). Why are you trying to avoid Bitmaps? If it is a memory constraint, you might want to decode small horizontal chunks with BitmapRegionDecoder

Fastest way to read/write a Bitmap from/to file?

I'm currently writing Bitmaps to a png file and also reading them back to a Bitmap. I'm looking for ways to improve the speed at which writing and reading happens. The images need to be lossless since I'm reading them back to edit them.
The place where I see the worst performance is the actual BitmapFactory.decode(...).
Few questions:
1. Is there a faster solution to read/write from file to a Bitmap using NDK?
2. Is there a better library to decode a Bitmap faster?
3. What is the best way to store and read a Bitmap?
Trying to resolve the best/fastest possible way to read/write image to file came down to using plain old BitmapFactory. I have tried using NDK to do the encoding/decoding but that really didn't make a difference.
Essentially the format to use was lossless PNG since I didn't want to loose any quality after editing an image.
The main concept from all this was that I needed to understand was how long encoding took versus decoding. The encoding numbers where in the upper 300-600ms, depending on image size, and decoding was just fast, around 10-23ms.
After understanding all that I just created a worker thread that I passed images needing encoding and let it do the work without affecting the user experience. The image was kept cached in memory just in case it was needed right away before it was completely encoded and saved to file.

Categories

Resources