How to store image in SQLite database

0 votes

I want to upload an image from a gallery into my application and store it in a SQLite database. What is the best way to store a bitmap in a database? I'm stringifying a bitmap and storing it in the database. I cannot give that string to ImageView after getting it from the database because it is a string.

Imageupload12 .java:

     public class Imageupload12 extends Activity {
      Button buttonLoadImage;
      ImageView targetImage;
      int i = 0;
      Database database = new Database(this);
      String i1;
      String img;
      @Override
      public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main5);
       buttonLoadImage = (Button) findViewById(R.id.loadimage);
       targetImage = (ImageView) findViewById(R.id.targetimage);
    
    
       Bundle b = getIntent().getExtras();
       if (b != null) {
        img = b.getString("image");
        targetImage2.setImageURI("image");
        //i am getting error as i cant assign string to imageview.
    
       }
    
       buttonLoadImage.setOnClickListener(new Button.OnClickListener() {
    
        public void onClick(View arg0) {
         // TODO Auto-generated method stub
         Intent intent = new Intent(Intent.ACTION_PICK,
          android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
         Log.i("photo", "" + intent);
         startActivityForResult(intent, i);
         i = i + 1;
        }
       });
    
      }
    
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
       // TODO Auto-generated method stub
       super.onActivityResult(requestCode, resultCode, data);
       switch (requestCode) {
    
        case 0:
         if (resultCode == RESULT_OK) {
          Uri targetUri = data.getData();
          //             textTargetUri.setText(targetUri.toString());
          Bitmap bitmap;
          try {
           bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
           targetImage.setImageBitmap(bitmap);
    
           i1 = bitmap.toString();
           Log.i("firstimage........", "" + i1);
           targetImage.setVisibility(0);
    
           SQLiteDatabase db = database.getWritableDatabase();
           db.execSQL("INSERT INTO UPLOAD VALUES('" + i1 + "');");
    
          } catch (FileNotFoundException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
          }
         }
         break;
    
    
    
       }
    
      }
     }

Image.class:

    public class Image extends Activity {
     Database database = new Database(this);
     static EfficientAdapter adapter, adapter1;
     static ListView lv1;
    
     static SQLiteDatabase db;
     static EfficientAdapter adp;
     static Cursor c1;
    
     static Vector < String > IMAGE = new Vector < String > ();
    
     @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
    
      db = database.getReadableDatabase();
      c1 = db.rawQuery("select * from UPLOAD;", null);
    
      if (c1.moveToFirst()) {
    
       do {
        IMAGE.add(c1.getString(0).toString());
    
       } while (c1.moveToNext());
    
       c1.close();
      }
    
      lv1 = (ListView) findViewById(R.id.List);
    
      adapter = new EfficientAdapter(this);
    
    
      lv1.setAdapter(adapter);
    
      ImageView add = (ImageView) findViewById(R.id.imv1a);
    
    
    
      add.setOnClickListener(new OnClickListener() {
    
       @Override
       public void onClick(View v) {
        // TODO Auto-generated method stub
        IMAGE.clear();
    
        Intent i = new Intent(Image.this, Imageupload12.class);
        startActivity(i);
    
    
       }
      });
    
    
     }
    
    
    
     private static class EfficientAdapter extends BaseAdapter {
    
    
      //        protected  final Context Context = null;
      protected LayoutInflater mLayoutInflater;
      AlertDialog.Builder aBuilder;
      public EfficientAdapter(Context context) {
       // TODO Auto-generated constructor stub
       mLayoutInflater = LayoutInflater.from(context);
      }
    
      @Override
      public int getCount() {
       // TODO Auto-generated method stub
    
       return IMAGE.size();
      }
    
      @Override
      public Object getItem(int position) {
       // TODO Auto-generated method stub
       return position;
      }
    
      @Override
      public long getItemId(int position) {
       // TODO Auto-generated method stub
       return position;
      }
    
      @Override
      public View getView(final int position, View convertView, ViewGroup parent) {
       // TODO Auto-generated method stub
    
       final ViewHolder mVHolder;
       if (convertView == null) {
        convertView = mLayoutInflater.inflate(R.layout.pjtlistdetails, parent, false);
    
        mVHolder = new ViewHolder();
    
        mVHolder.t1 = (TextView) convertView.findViewById(R.id.pjtdetails);
        mVHolder.time = (TextView) convertView.findViewById(R.id.name);
    
    
        mVHolder.imv = (ImageButton) convertView.findViewById(R.id.editic);
        mVHolder.imvd = (ImageView) convertView.findViewById(R.id.delete);
        mVHolder.imvf = (ImageView) convertView.findViewById(R.id.fwd);
    
    
    
    
    
        mVHolder.imv.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
    
    
    
          String img = IMAGE.elementAt(position);
          Log.i("image...", "" + img);
    
          Context ctx = v.getContext();
          Intent myIntent = new Intent();
          ctx = v.getContext();
          myIntent.setClass(ctx, Imageupload12.class);
          myIntent.putExtra("image", img);
    
          ctx.startActivity(myIntent);
    
          IMAGE.clear();
    
         }
        });
        static class ViewHolder {
    
         ImageButton imv;
         ImageView imvd, imvf;
        }
       }
      }
     }
    }
Aug 22, 2022 in Database by Kithuzzz
• 38,000 points
3,028 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Database

0 votes
1 answer

Sir please tell me how to import image in database?

Hello soumadip, (Assuming you are using mysql database) You ...READ MORE

answered Apr 1, 2020 in Database by Niroj
• 82,840 points
1,010 views
0 votes
2 answers
0 votes
1 answer

How to load data of .csv file in MySQL Database Table?

At first, put the dataset in the ...READ MORE

answered Jul 5, 2019 in Database by Reshma
1,424 views
0 votes
2 answers

How to select the nth row in a SQL database table?

SELECT * FROM ( SELECT ID, NAME, ROW_NUMBER() ...READ MORE

answered Apr 23, 2020 in Database by anand
• 140 points
26,293 views
0 votes
1 answer

How to rename a column in a database table using SQL?

For SQL Server, use sp_rename USE AdventureWorks; GO EXEC sp_rename 'Customers.CustomerTerritory.TerritoryID', ...READ MORE

answered Feb 9, 2022 in Database by Neha
• 9,020 points
762 views
0 votes
0 answers

How to change column datatype in SQL database without losing data?

I have a SQL Server database, and ...READ MORE

Feb 25, 2022 in Database by Vaani
• 7,070 points
713 views
0 votes
0 answers

How to retrieve data from sqlite database in android and display it in TextView

I'm studying Android. I'm having trouble solving ...READ MORE

Aug 11, 2022 in Database by Kithuzzz
• 38,000 points
4,763 views
0 votes
0 answers
0 votes
0 answers

SQLite in Android How to update a specific row

For a time now, I've been attempting ...READ MORE

Aug 11, 2022 in Database by Kithuzzz
• 38,000 points
1,701 views
0 votes
0 answers

Displaying ALL data from sqlite database into listview in tabbed activity

I've been struggling with this problem for ...READ MORE

Aug 20, 2022 in Database by Kithuzzz
• 38,000 points
3,133 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP