Tuesday 19 April 2016

Encode and Decode the Bitmap to Base64 String and vice versa

// Encode the Bitmap to Base64 String:

public static String encodeTobase64(Bitmap image) {
        Bitmap immagex = image;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] b = baos.toByteArray();
        String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);

        Log.e("LOOK", imageEncoded);
        return imageEncoded;

Decode the Base64 String to Bitmap:

public static Bitmap decodeBase64(String input) {
        byte[] decodedByte = Base64.decode(input, 0);
        return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);

Sunday 17 April 2016

DVM vs ART - Android

There are some major performance improvements that ART brings which were lacking in Dalvik. But every pros have some cons too. I will try to discuss both the advantages and disadvantages here.
Android 4.4 KitKat, Google decided to introduce a new way of executing apps on top of the Android operating system. Let's take a closer look at what this new runtime, called ART
ART is Google's 2-year-long ongoing secret project, which aims to boost the performance of our Android devices.
Android L (5.0) ART has been made as the default runtime (ART has completely replaced Dalvik).
ART (Android RunTime) is the next version of Dalvik. Dalvik is the runtime, bytecode, and VM used by the Android system for running Android applications.
Dalvik is based on JIT (just in time) compilation. It means that each time you run an app, the part of the code required for its execution is going to be translated (compiled) to machine code at that moment. As you progress through the app, additional code is going to be compiled and cached, so that the system can reuse the code while the app is running. Since JIT compiles only a part of the code, it has a smaller memory footprint and uses less physical space on the device.
ART vs Dalvik / AOT vs JIT
Advantages of ART over Dalvik:
  1. The apps launch speed is amazingly fast in case of ART since nothing is compiled at execution.
  2. Boot speed is faster than dalvik since nothing is execued from dalvik partition as in case of odexed ROM in dalvik
  3. Increases battery backup by reducing CPU work due to absence of compilation work on apps execution.
  4. Improved Garbage Collection (GC)
  5. And finally it is a great reward to developers because most of developers are worried that odexed ROMs are faster than deodexed ROMs but they will deodex their ROMs since they heavily theme it. In case of ART whether the ROM is odexed or deodexed it doesn't matter, it compiles the full code into machine language on installation of apps. so even deodexed ROMs are as fast as odexed in ART
Disadvantages of ART
  1. Since ART precompiles apps on installation, it takes 10-20% more space upon installation than dalvik.
  2. As dex bytecodes are converted to native machine code on installation itself, installation takes more time.
Install times on my Nexus 4, for one of our larger projects, jumped from ~17 s to ~25 s.
Dalvik vs. ART Benchmark Results (Android 4.4) : Linpack
Single Thread ---> Dalvik (135) ---> ART(149) ---> 10.93%
Multi- Thread ---> Dalvik (336) ---> ART(383) ---> 13.82%
1) Compilation Approach
This is by far the biggest advantage of ART over Dalvik. The old guy Dalvik used Just-In-Time (JIT) approach in which the compilation was done on demand. All the dex files were converted into their respective native representations only when it was needed.
But ART uses the Ahead-Of-Time (AOT) approach, in which the dex files were compiled before they were demanded. This itself massively improves the performance and battery life of any Android device.
For example
In case of Dalvik, whenever you touch an app icon to open it, the necessary dex files gets converted into their equivalent native codes. The app will only start working when this compilation is done. So, the app is unresponsive until this finishes.
Moreover, this process is repeated every single time you open an app wasting CPU cycles and valuable battery juice.
But in case of ART, whenever you install an app, all the dex files gets converted once and for all. So the installation takes some time and the app takes more space than in Dalvik, but the performance is massively improved and battery life is smartly conserved.

2) Boot Time

In case of Dalvik, the cache is built with time the device runs and apps are used as is indicated by the JIT approach. So the boot time is very fast.
But in case of ART, the cache is built during the first boot, so the boot time is considerably more in case of ART. You might see an "Optimizing apps" dialog box sometimes you boot.

3) Space Usage

The space used by apps being run on ART is much more than that of Dalvik. Like a 20 MB app on Dalvik, takes more than 35 MB on ART.
So if you are on a low storage device, then this can be a huge disadvantage for you.

4) ART is Damn Fast

As discussed above, ART is extremely fast and smooth. Apps are very snappy and responsive. Any comparison between Dalvik and ART, will surely make the ART device win by a significant margin.
ART is the answer to all those who argued that iOS is faster and smoother than Android and is also more battery efficient.

Thanks & Courtesy: