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 6560107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:25:02+00:00 2026-05-25T13:25:02+00:00

I want to store an image(from url) into a sqlite database. For that I

  • 0

I want to store an image(from url) into a sqlite database.

For that I use:

db = new DataBase(getApplicationContext());
URL url = new URL("http://sree.cc/wp-content/uploads/schogini_team.png");
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is,128);
ByteArrayBuffer barb= new ByteArrayBuffer(128);

int current = 0;
while ((current = bis.read()) != -1) {
    barb.append((byte) current);
}

ContentValues filedata= new ContentValues();

filedata.put(DataBase.IMG_SRC,barb.toByteArray());

db.insert(DataBase.Table_Img, null, filedata);

In the Insert():

public void insert(String tableImg, Object object,
        ContentValues dataToInsert) {
    // TODO Auto-generated method stub
    String sql = "INSERT INTO "+tableImg+" ("+ID+","+IMG_SRC+") " +
            "VALUES ('"+1+"','"+dataToInsert+"')";
    db.execSQL(sql);
}

For the retrieval of image:

Cursor cursor = db.selectDataToShow(DataBase.Table_Img, DataBase.IMG_SRC);

byte[] imageByteArray=cursor.getBlob(cursor.getColumnIndex(DataBase.IMG_SRC));      
cursor.close();

ByteArrayInputStream imageStream = new ByteArrayInputStream(imageByteArray);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);

System.out.println(">>>>>>>>>>>>>>>>>>>>>> "+theImage);

So here I got null.

And in my database the value of image stored as: Image=[B@43e5ac48]

  • 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-05-25T13:25:03+00:00Added an answer on May 25, 2026 at 1:25 pm

    Here the code i used for my app

    This code will take a image from url and convert is to a byte array

    byte[] logoImage = getLogoImage(IMAGEURL);
    
    private byte[] getLogoImage(String url){
         try {
                 URL imageUrl = new URL(url);
                 URLConnection ucon = imageUrl.openConnection();
    
                 InputStream is = ucon.getInputStream();
                 BufferedInputStream bis = new BufferedInputStream(is);
    
                 ByteArrayBuffer baf = new ByteArrayBuffer(500);
                 int current = 0;
                 while ((current = bis.read()) != -1) {
                      baf.append((byte) current);
                 }
    
                 return baf.toByteArray();
         } catch (Exception e) {
              Log.d("ImageManager", "Error: " + e.toString());
         }
         return null;
    }
    

    To save the image to db i used this code.

     public void insertUser(){
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            String delSql = "DELETE FROM ACCOUNTS";
            SQLiteStatement delStmt = db.compileStatement(delSql);
            delStmt.execute();
    
            String sql = "INSERT INTO ACCOUNTS (account_id,account_name,account_image) VALUES(?,?,?)";
            SQLiteStatement insertStmt = db.compileStatement(sql);
            insertStmt.clearBindings();
            insertStmt.bindString(1, Integer.toString(this.accId));
            insertStmt.bindString(2,this.accName);
            insertStmt.bindBlob(3, this.accImage);
            insertStmt.executeInsert();
            db.close();
    }
    

    To retrieve the image back this is code i used.

    public Account getCurrentAccount() {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String sql = "SELECT * FROM ACCOUNTS";
        Cursor cursor = db.rawQuery(sql, new String[] {});
    
        if(cursor.moveToFirst()){
            this.accId  = cursor.getInt(0);
            this.accName = cursor.getString(1);
            this.accImage = cursor.getBlob(2);
        }
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
        db.close();
        if(cursor.getCount() == 0){
            return null;
        } else {
            return this;
        }
    }
    

    Finally to load this image to a imageview

    logoImage.setImageBitmap(BitmapFactory.decodeByteArray( currentAccount.accImage, 
            0,currentAccount.accImage.length));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to store a large result set from database in memory. Every record
I want to save images fetched from a url into the NSTempDirectory(). If I
I created a separate project and activity that downloads an image from a URL,
I want to access an image stored in Blackberry, say at location store/home/user/image.png .
I want to store the current URL in a session variable to reference the
I want to store a large number of sound files in a database, but
I want to store the data returned by $_SERVER[REMOTE_ADDR] in PHP into a DB
I want to store a URL prefix in an Windows environment variable. The ampersands
Hope you all will be fine. Actually i want to upload image files from
I used the following code to retrieve an image from a url and display

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.