black.empiricist
black.empiricist

Reputation: 47

Display taken image from camera in image view and add it to database - Android Studio

I created a form that will save information to my database including an image. I have inserted the image in a Imageview to view it after taking the picture via camera. How do I insert the image retrived by the imageview to my SQL Query?

This is my onCreate Function:

 String FirstName, LastName, EmailAddress;
            Integer StudentID, ContactNumber;
            SQLiteDatabase db;
            ImageView viewImage, StudPic;

            @Override
            protected void onCreate(Bundle savedInstanceState)
            {

                super.onCreate(savedInstanceState);
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
                setContentView(R.layout.addstudentsform);
                db=openOrCreateDatabase("ClassManager",MODE_WORLD_READABLE, null);

    db.execSQL("CREATE TABLE IF NOT EXISTS MasterStudents (StudPic BLOB, StudentID INTEGER NOT NULL UNIQUE, FirstName VARCHAR," +
               "LastName VARCHAR, ContactNumber INTEGER, EmailAddress VARCHAR);");

                viewImage = (ImageView)findViewById(R.id.CamPicture);
                viewImage.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        File f = new File(android.os.Environment.getExternalStorageDirectory(), "StudPic.jpg");
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                        startActivityForResult(intent, 1);
                    }

                });
            }

This is onActivityResult Function that previews the image taken from the camera:

@Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data)
            {
                super.onActivityResult(requestCode,resultCode,data);
                if(resultCode==RESULT_OK)
                {
                    if (requestCode == 1)
                    {
                        File f = new File(Environment.getExternalStorageDirectory().toString());
                        for (File temp : f.listFiles()) {
                            if (temp.getName().equals("StudPic.jpg")) {
                                f = temp;
                                break;
                            }
                        }

                        try
                        {
                            Bitmap bitmap;
                            BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

                            bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions);
                            viewImage.setImageBitmap(bitmap);

                            String path = Environment.getExternalStorageDirectory() + File.separator + "Phoenix" + File.separator + "Default";
                            f.delete();
                            OutputStream outFile = null;
                            File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");

                            try
                            {
                                outFile = new FileOutputStream(file);
                                bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
                                outFile.flush();
                                outFile.close();
                            }
                            catch (FileNotFoundException e)
                            {
                                e.printStackTrace();
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                            catch (Exception e)
                            {
                                e.printStackTrace();
                            }
                        }

                        catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                    }
                }
            }

This is the CreateScreen Function that inserts the text in the database. How do I do it with the Image in the Image view?

   public void CreateScreen (View view)
            {

                EditText  FirstNameText     = (EditText)findViewById(R.id.FirstNameText);
                EditText  StudentIDText     = (EditText)findViewById(R.id.StudentIDText);
                EditText  LastNameText      = (EditText)findViewById(R.id.LastNameText);
                EditText  ContactNumberText = (EditText)findViewById(R.id.ContactNumberText);
                EditText  EmailAddressText  = (EditText)findViewById(R.id.EmailAddressText);

                StudentID     = Integer.parseInt(StudentIDText.getText().toString());
                FirstName     = FirstNameText.getText().toString();
                LastName      = LastNameText.getText().toString();
                ContactNumber = Integer.parseInt(ContactNumberText.getText().toString());
                EmailAddress  = EmailAddressText.getText().toString();

                db.execSQL("INSERT INTO MasterStudents (StudPic, StudentID, FirstName, LastName, ContactNumber, EmailAddress) " +
                           "VALUES ('" + StudentID + "','" + FirstName + "','" + LastName + "','" + ContactNumber + "','" + EmailAddress + "');");

                Toast toast = Toast.makeText(getApplicationContext(), "Student Added", Toast.LENGTH_SHORT);
                toast.show();
                finish();

            }

NOTE: The StudPic field name is the BLOB in the sql query that i want to insert the image from the imageView in the onActivityResult Function

Any help that is explained with //comments and clarity is highly appreciated

Upvotes: 0

Views: 3084

Answers (1)

cylon
cylon

Reputation: 733

You should save only the URI of the image in your database.

You can use this to get your image uri, instead of iterating through all files.

protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    Uri u = intent.getData(); // this gonna give you the pic's uri
    // you should convert this to a path (see the link below the code section)

}

Then you can retrive your image whenever you want.

Bitmap myBitmap = BitmapFactory.decodeFile(path);

ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);

myImage.setImageBitmap(myBitmap);

Here is how you can convert your uri to path: Get Path of image from ACTION_IMAGE_CAPTURE Intent

Upvotes: 1

Related Questions