Sunday, December 29, 2013

OutOfMemoryError in Android development

If you are using Images in your Android app, sometimes you may face this run-time issue in Android development. This happens only in some of the Android devices. This error does not show up in the Android emulator. So until you test on some devices you can't find this error. The reason for this error is that there are security limitations in the Android OS. If restricts the memory of RAM to 16 MB for a single app. So even if you have 2 GB ram on your Android device, a single app can consume only up-to 16MB. That is the normal procedure.

Once I had to used twelve 1800*1700 pixel images some other images. So lets do a simple calculation.

1800*1700*3    = 9180000 bits
9180000 / 8       = 1147500 bytes
1147500 / 1024 = 1120.60 kB   nearly equals to 1MB.

So each image costs nearly 1 MB in the RAM.
I had 12 of these images. Total of 12 MB.

Also I had some more images and my total memory of the app exceeds 16MB limit. So in some devices I got OutOfMemoryError in Bitmap factory.

The simple solution I followed is, I reduced the resolution of the images. Because 1800*1700 is way too much for an Android app. After reducing the resolution the error is fixed.

Please refer this developer document for more details on this issue.
https://developer.android.com/training/displaying-bitmaps/manage-memory.html

Friday, November 29, 2013

GitHub for Windows Install Error

GitHub for Windows is a great tool for Windows users like me.;-) To Install GitHub for Windows you need dotNet version 4.0 or above.

Even having dotNet 4.0 you might face an issue while installing GitHub for Windows. I also had to face following common issue.

"Application cannot be started. Contact the application vendor."


To solve that issue go to run in Windows start menu and then go to following link.

C:\Users\[Your User Name]\AppData\Local\Apps

Then delete the 2.0 folder. It is ok that some files cannot be deleted from that folder.

Then try to re-install Github. Then you can proceed without an issue.

Friday, November 1, 2013

Using C++ classes

If you did your first OOP related coding using Java or C# you may find bit uncomfortable when you trying to use classes in C++. Because syntax and structure has bit difference between them. So lets see how to write a class in C++. This is a basic tutorial as there is no much explained tutorial in the web.

Unlike Java or C#, you have to define your class in the header file of C++ project. Now go to the header file of your project and add following class definition.

class Car
{
       private:
                int noOfTyres;

       public:
              Car();
               void drive();
};

As you can understand I'm going to discuss a simple example here. In the private: section you have to define private variables / methods of the particular class. In the public: section you can define public methods/variables of the class. The methods should not have its body with the header file. It will contain in the cpp file. Also if you are going to include a constructor other than the default constructor, it should declared at the public section.

Now go to the cpp file and include the header file using the #include tag. Now you have to add method body. Just check the following code.

You can define the constructor of the Car class,

Car::Car()
{
      // constructing code goes here
}

Explanation:
The first "Car" term and the double colons (::) describes that the method is belongs to the "Car" class. Next Car() term describes the Car constructor.

Now lets define the drive() method,

void Car::drive()
{
       // code goes here
}

Explanation,
The first "void" term describes the return type of the method. It should be same as what you gave at the header file. From the next phrases of the statement, it describes that the drive() method is belongs to the Car class.(Car::drive()).

So I hope you understood.

Now think you wanted to add a variable of another class type into the Car class. As an example "Type" class belongs to the "Car" class. Then your header file should be shown as below.

class Tyre
{
        private:
               // private things goes here
        public:
              // public things goes here
};

class Car
{
         private:
               Tyre tyres;
         public:
               void drive();  
};

The most important thing is that you have to declare the "Tyre" class before declaring "Car" class. It is a good practice to follow that the two class should keep in two header files.

I hope you got some basic idea of using Classes in C++.


Tuesday, October 29, 2013

Integrate SQLite with Visual C++

MySQL, SQL Server and Oracle are the generally used database systems. But with the arrival of embedded and mobile devices, SQLite has become very useful. When you are doing a project using C or C++, SQLite will be the most suitable option according to the scale of your project. SQLite is a light weight database system. When I tried to integrate SQLite into my Visual C++ project, I realize that there is no proper documentation that make the developer's life easier. So I am going to explain my experience.

First download SQLite from the developer's web site. You have to download 
  • sqlite-shell-win32-x86-3080100.zip  
  • sqlite-dll-win32-x86-3080100.zip from the Precompiled Binaries for Windows section. 
  • And sqlite-amalgamation-3080100.zip file from the same site.


Then extract all the zip files to one folder. Lets assume that folder is "C:\SQLite". 

Now add the path of that folder into the PATH variable of your computer.(Same procedure as you do it in Java jdk.)

Now open the Visual Studio Command Prompt which I mentioned in this post. Then type below command and press enter. This will create sqlite3 library file.
lib /DEF:"C:\SQLite\sqlite3.def" /OUT:"C:\SQLite\sqlite3.lib"

Now after that open your Visual C++ project using Visual Studio IDE. Then write click your project and goto Properties.

Then following window will appear.


Now go to the C/C++ section from the left tab. When selecting that, the right tab will change. Now add the path of your SQLite folder into the "Additional Include Directories".

Then go to the General section of the Linker from the left tab.(Linker -> General). Now again add the same path to the "Additional Library Directories".

Finally go to the Input sub section of the Linker (Linker -> Input) and add "sqlite3.lib" in to the Additional Dependencies. Then press OK.

Now you are done and you can do your coding.

First include the sqlite3.h header file using #include <sqlite3.h> code.

Then do your coding. Below code is extracted from this tutorial. It just opens and connects to a new database.

#include <stdio.h>
#include <sqlite3.h>

int main(int args, char* argv[]){
    sqlite *db;
    char *zErrMsg = 0;
    int rc;  
    rc = sqlite3_open("database_1.db", &db);
    if(rc){
      printf("Can't open databse: %s\n", sqlite3_errmsg(db));
      exit(0);
    }else{
      printf("Opened database successfully\n");
    }
    sqlite3_close(db);
}

Happy Coding! :)

Monday, October 28, 2013

How to read Research Papers efficiently

Reading a Research paper is a really painful thing for most of the people. Sometimes we have to read few research papers within few hours/days. But some people take a whole day to read a single research paper. So lets see if there is any method do read a research paper quickly.


Most research papers contains two columns. Because that column width is comfortable to the human eye. Majority of the people read paper by reading words one by one. That is the most time consuming thing. So try to read a chunk of three words at once. This will be difficult at your first run. So keep practice. Later you'll save a lot of time when reading research paper.

The second method is avoiding the movement of your eye. You can concentrate your eye on the center of a line. Any avoid the leftmost and rightmost words. Read only middle words. That won't affect the meaning of the sentence. You can get the meaning of that sentence without reading the leftmost and rightmost words and it will save some time.

So aggregating these two methods, I have posted a photo below. You can understand using it.

    
You will have to practice this methods to get the advantage of it. With this method you will not grab the 100% of the contents of the research paper. But this method will give you a clear idea of the research paper and its relatedness to your research quickly. Good luck with your readings.!

Thursday, October 24, 2013

Build your solution using Visual Studio Command Prompt

There are some solutions which generated automatically but with a large content. Also there are some buggy Visual Studio versions which are lagging and becomes non-responding while opening and building a comparatively larger solutions.

When I was building OpenCV and QT for my University Final year project, I faced same situation. (I will post a separate article about this OpenCV building process.) My Visual Studio version seems buggy and I didn't have enough time to re-install it. When I tried to open the OpenCV solution using Visual Studio IDE, it becomes non-responding. So I tried the Visual Studio Command Prompt.

Visual Studio Command Prompt

You can open Visual Studio Command Prompt using Visual Studio Tools in the Visual Studio in Start Menu. Then you have to execute just one command. It is simple.

MSBuild D:\Project\YourSolution.sln /p:Configuration=Release 

Replace the "D:\Project\YourSolution.sln" with the location of your solution. Execution will take some time according to the scale of your project. After building using above command, replace "Release" with "Debug" if you want to build your solution for the debug also.

Hope my post will helpful! :)

Thursday, October 17, 2013

Bypass read only permission in ubuntu recovery mode



While I was doing some experiments, I changed the "fstab" file in "etc" folder in ubuntu file system. That file is normally read only and it is related to the mounting process of partitions of the disks. I changed the read only status of a partition to read+write permission status. Consider that when you change that file DON'T restart computer without re-changing that file. But unfortunately I forgot the thing that I changed that file and after some other work, I restarted my computer.:(

So I couldn't boot my Ubuntu because of the misbehavior of the permissions of the partitions. So the simplest solution which comes in to my mind is to change the fstab file again to its original status. So I tried the recover mode. But later I figured that I can't change that file even in the root user because of the read only status of that file. Previously I changed that permission with some GUI method. So I couldn't use it with the terminal.

So I worked around with the recovery mode and found that we can edit the GRUB commands. For that you have to press "e" at the grub menu. Then it will display the grub commands. At that command list you'll be able to find "ro" in lower command. Change that to "rw". Then press F10 to boot using this changes. Then it will load the recover terminal. Now you can edit any read only file using vi editor. If you are not familiar with vi command refer some online tutorials.:)