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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:12:16+00:00 2026-05-27T05:12:16+00:00

I am struggling with saving image on sqlite. I am trying to save image

  • 0

I am struggling with saving image on sqlite.
I am trying to save image as blob on sqlite.
I don’t know how to declare imageView to byte[] because I am using insert method on dbAdapter.
Is it good way to save the image into database? some people said that saving file path url into database is better. If it is better, please give me some your hands.
I am really getting tough for that.
Many thanks.

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);
     if (resultCode != RESULT_OK) return;
       switch (requestCode)
       {
        case PICK_FROM_CAMERA:
        Bundle extras = data.getExtras();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Bitmap selectedImage = (Bitmap) extras.get("data");
        selectedImage = Bitmap.createScaledBitmap(selectedImage, 200, 250, false);
        selectedImage.compress(CompressFormat.PNG, 0, bos);
        mImageView.setImageBitmap(selectedImage);
        break;

        case PICK_FROM_GALLERY:
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);
            mImageView.setImageURI(selectedImageUri);
        break;
       }
}

protected void onSaveInstanceState(Bundle outState)
{
    super.onSaveInstanceState(outState);
    saveState();
}

private void saveState() 
{
    String name = (String) nameEdit.getText().toString();
    String category = (String) categoryEdit.getText().toString();
    String expired_date = (String) expired_Date_Btn.getText().toString();
    //byte[] image = (byte[]) mImageView... I HAVE TO DO SOME CODING HERE.. 
    if(mRowId == null)
    {
        long id = mDbHelper.insertItem(category, name, expired_date, image);

        if(id>0)
        {
            mRowId = id;
        }           
    }
    else 
    {
        mDbHelper.updateItem(mRowId, category, name, expired_date, image);
    }   
}

DbAdapter class

    public long insertItem(String category, String name, String expired_date, byte[] image)
{
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_CATEGORY, category);
    initialValues.put(KEY_NAME, name);
    initialValues.put(KEY_EXPIRED_DATE, expired_date);
    initialValues.put(KEY_IMAGE, image);
    return db.insert(DATABASE_TABLE, null, initialValues);
}
  • 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-27T05:12:17+00:00Added an answer on May 27, 2026 at 5:12 am

    Try doing this:

    ConvertDrawableToByteArray(mImageView.getDrawable()
    

    With ConvertDrawabelToByteArray defined like this:

    public static byte[] ConvertDrawableToByteArray(Drawable drawableResource) {
    
        Bitmap imageBitmap = ((BitmapDrawable) drawableResource).getBitmap();
        ByteArrayOutputStream imageByteStream = new ByteArrayOutputStream();
        imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, imageByteStream);
        byte[] imageByteData = imageByteStream.toByteArray();
        return imageByteData;
    }
    

    Though I think you should be able to get the bytes out of the Bitmap as well. All that said, storing the filepath in sql lite would probably be more efficient, however, if the user deletes the image you’d have to handle that in your application.

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

Sidebar

Related Questions

I've been struggling on an issue for days and I don't know what to
I know that Android provides a mechanism to allow developers to save and restore
I am trying to understand LINQ and become confident at using it. What I
Still struggling very much with how to accomplish saving div+contents/layout to a database and
I'm really struggling with ABAddressBookGetPersonWithRecordID at the moment. I am saving an ID, and
i'm struggling a bit with repositories. I'm using C# and NHibernate. The question i
I've started using the dashboard plugin which is demoed here - http://www.gxdeveloperweb.com/dashboardplugin/demo/dashboard.html I'm struggling
Struggling with styling the mouse over for a button ... I have managed to
Still struggling to understand what best practices are with respect to macros. I'm attempting
Iam struggling with NHibernate and its lazyload. I have a structure which I simplified

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.