1

I am trying to create an application where the user uploads a picture and it gets saved in SQLite Database. So far i have successfully managed to upload the image to the application while following a tutorial , however i am not sure what to do to save it in SQLite so that when the activity closes and opens again the image stays there.

My Code:

   public class Getimg2 extends Activity implements View.OnClickListener{



    private static final int RESULT_LOAD_IMAGE =1;
    ImageView ImageUp;
    Button   BtnUp;
    Button Confirm;
    SQLiteDatabase db;

     @Override
      protected void onCreate(Bundle savedInstanceState) {                             
        super.onCreate(savedInstanceState);
        setContentView(R.layout.diary_edit);


       ImageUp = (ImageView) findViewById (R.id.imageUp);
    BtnUp = (Button) findViewById (R.id.btnUp);
    Confirm =(Button) findViewById (R.id.confirm)   ;

        ImageUp.setOnClickListener(this);   
        BtnUp.setOnClickListener(this); 
        Confirm.setOnClickListener(this);
     }

    @Override
            public void onClick(View v){           

        switch(v.getId()) {

        case R.id.imageUp:                     

                Intent galleryIntent = new Intent (Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(galleryIntent,RESULT_LOAD_IMAGE);

            break;

            case R.id.btnUp:

                Bitmap image =((BitmapDrawable)ImageUp.getDrawable()).getBitmap();  

                break;

            case R.id.confirm:

                //save image to sql?

                Cursor c =dba.rawQuery("select  *  from tb", null);
    if (c.moveToNext())

                break;
            }


            }
   @Override
            protected void onActivityResult(int  requestCode , int resultCode, Intent data){
                super.onActivityResult(requestCode, resultCode, data);
                if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data !=null){
                    Uri selectedImage =data.getData();
                    ImageUp.setImageURI(selectedImage);

                }

          }

           private class UploadImage extends AsyncTask<Void,Void ,Void >{     

                Bitmap image;
                String name;


                public UploadImage(Bitmap image,String name){                   

                    this.image = image;
                    this.name = name;

                }

            @Override
                protected Void doInBackground(Void... params) {               
                    // TODO Auto-generated method stub

                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
                    String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(),Base64.DEFAULT);
  }}}
nanay
  • 301
  • 1
  • 3
  • 9

1 Answers1

1

Storing such large items in the database is not the right approach. Store the file in the app's private file system and then store the path to the file in the database.

Francesc
  • 25,014
  • 10
  • 66
  • 84