Tuesday, September 15, 2015

Android: Saving Bitmap as Serializable Object

The Problem

In Android you cannot save the any bitmap Object as serilizable objects and you cannot add implement Serializable Interface as it is a final class.
so In general the problem is "Saving bitmap Images as Serializable Object"


The Solution

Luckily there is a very simple way to do this
Those who are experts of you will find the next two lines sufficient

bitmapObject.getPixels(pixels,0,width,0,0,width,height);
 &
Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);

Still not getting it

Well here it is how,

  • First we create a simple class, we will call it ProxyBitmap, why use the word "proxy" ?
    public class ProxyBitmap
  • Make the class serializable
    public class ProxyBitmap implements Serializable {}
  • Add the following attributes
    private final int [] pixels;
     // this array will hold the pixels of the bitmap Image
    
    private final int width , height;
    // those will hold both the width and height attributes
  • Now adding the constructor, we take bitmap as input in the constructor to get its pixel data.
    public ProxyBitmap(Bitmap bitmap){
       width = bitmap.getWidth();
       height = bitmap.getHeight();
       pixels = new int [width*height];
       bitmap.getPixels(pixels,0,width,0,0,width,height);
    }
  • Now to create a method that will return the bitmap object
    
    public Bitmap getBitmap(){
        return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
    }
  • The whole class will look something like this according to your preference 
    
    import android.graphics.Bitmap;
    import java.io.Serializable;
    /** * Created by John on 07-Sep-15. */
    public class ProxyBitmap implements Serializable{
        private final int [] pixels;
        private final int width , height;
    
        public ProxyBitmap(Bitmap bitmap){
            width = bitmap.getWidth();
            height = bitmap.getHeight();
            pixels = new int [width*height];
            bitmap.getPixels(pixels,0,width,0,0,width,height);
        }
    
        public Bitmap getBitmap(){
            return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
        }
    }
  • Now you can use the ProxyBitmap object whenever you want to save Bitmap objects, I wrote a whole example , showing how I save and load serialized ProxyBitmap objects here it is 

Saturday, June 27, 2015

Selecting Files and Folders in Android

The Problem

I have a program that I wanted the user to do both selecting files and selecting directories; the libraries that I found offered different UI for each action ( selecting file / folders) which is not visually suitable for the user.

The Solution

I had to write my own library to select those files and folders. This is its Git repo "MyFileChooser" where you can see the code and download the jar file.

How to use

How to set up the library in your project download the jar file
Import the jar file into your project 


  1. Right click on your project.
  2. Select properties
  3. Select "Java Build Path" then "Libraries" tab and then choose "Add External JARs"
  4. Choose the jar file that you downloaded.
  5. Add the following lines into you manifest file
  6. <activity
        android:name="com.john_aziz57.myfilechooser.MainActivity"
        android:exported="true" >
    </activity>
  7. Since you will probably will read or write files don't forget to add the appropriate permissions
  8. The place where you want to start the activity add the following code to select folders
    Intent i = new Intent(YourCurrentActivity,
            com.john_aziz57.myfilechooser.MainActivity.class);
    i.putExtra(MainPresenter.TAG, MainPresenter.SELECT_FOLDER);
    startActivityForResult(i, FOLDER_SELECT_CODE);
to select files
    Intent i = new Intent(YourCurrentActivity,
            com.john_aziz57.myfilechooser.MainActivity.class);
    i.putExtra(MainPresenter.TAG, MainPresenter.SELECT_FILE);
    startActivityForResult(i, FILE_SELECT_CODE);
in "onActivityResult" add the following code
protected void onActivityResult( int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case FILE_SELECT_CODE:
        if (resultCode == RESULT_OK) {
            // Get the Uri of the selected file
            Uri uri = data.getData();
            // Get the path
            String path = uri.getPath();
                            .......

Screen Shots

here are screen shots inside my Absence program. first one for selecting files and the second one is for selecting folders

Final Notes

I don't expect that this project will meet up with all your needs, feel free to download the code and adjust it as much as you want or comment below for suggested enhancements