Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8331715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:29:53+00:00 2026-06-09T02:29:53+00:00

In my project I need to store an image into a sqlite database and

  • 0

In my project I need to store an image into a sqlite database and also need to retrieve it to show in my android emulator. When I show it directly after decoding the encoded string, which I got from Java class using sockets, the image displays there. But when I store a byte array code of the string into the sqlite database with the datatype blob and then again retrieve it by using the getblob() function it contains a different value and this error occurs:

JAVA.lang.NULLPointerException: Factory returns null.

I need a suggestion to store a bitmap image into a sqlite database and also to retrieve it from the sqlite database.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:29:55+00:00Added an answer on June 9, 2026 at 2:29 am

    Setting Up the database

    public class DatabaseHelper extends SQLiteOpenHelper {
        // Database Version
        private static final int DATABASE_VERSION = 1;
    
        // Database Name
        private static final String DATABASE_NAME = "database_name";
    
        // Table Names
        private static final String DB_TABLE = "table_image";
    
        // column names
        private static final String KEY_NAME = "image_name";
        private static final String KEY_IMAGE = "image_data";
    
        // Table create statement
        private static final String CREATE_TABLE_IMAGE = "CREATE TABLE " + DB_TABLE + "("+ 
                           KEY_NAME + " TEXT," + 
                           KEY_IMAGE + " BLOB);";
    
        public DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
    
            // creating table
            db.execSQL(CREATE_TABLE_IMAGE);
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // on upgrade drop older tables
            db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
    
            // create new table
            onCreate(db);
        }
    }
    

    Insert in the Database:

    public void addEntry( String name, byte[] image) throws SQLiteException{
        SQLiteDatabase database = this.getWritableDatabase();
        ContentValues cv = new  ContentValues();
        cv.put(KEY_NAME,    name);
        cv.put(KEY_IMAGE,   image);
        database.insert( DB_TABLE, null, cv );
    }
    

    Retrieving data:

     byte[] image = cursor.getBlob(1);
    

    Note:

    1. Before inserting into database, you need to convert your Bitmap image into byte array first then apply it using database query.
    2. When retrieving from database, you certainly have a byte array of image, what you need to do is to convert byte array back to original image. So, you have to make use of BitmapFactory to decode.

    Below is an Utility class which I hope could help you:

    public class DbBitmapUtility {
    
        // convert from bitmap to byte array
        public static byte[] getBytes(Bitmap bitmap) {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(CompressFormat.PNG, 0, stream);
            return stream.toByteArray();
        }
    
        // convert from byte array to bitmap
        public static Bitmap getImage(byte[] image) {
            return BitmapFactory.decodeByteArray(image, 0, image.length);
        }
    }
    

    Further reading
    If you are not familiar how to insert and retrieve into a database, go through this tutorial.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to be able to save and load images into an SQLite database
My android app project need to add a new function of saving the click
I am trying to store some pictures into my SQL Server database from a
I need to load a png stored in my asp.net mvc project into my
I need some guidance for a database design around a project management system. My
I am using pymongo to store user comments in my Django project. I need
In my project I need to use third party code, stored in several Git
my project need to read multiple web pages at a time(eg: for a particular
My project need to compare output(only urls) of google and yahoo for a particular
The Junits I have in my project need to load property files from the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.