在图像视图中显示从相机拍摄的图像并将其添加到数据库 - Android Studio

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

我创建了一个表单,可以将包括图像在内的信息保存到我的数据库中。我已将图像插入 Imageview 以在通过相机拍摄照片后查看它。如何将 imageview 检索到的图像插入到我的 SQL 查询中?

这是我的 onCreate 函数:

 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);
                    }

                });
            }

这是 onActivityResult 函数,预览从相机拍摄的图像:

@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();
                        }
                    }
                }
            }

这是在数据库中插入文本的 CreateScreen 函数。如何在图像视图中使用图像?

   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();

            }

注意:StudPic 字段名称是 sql 查询中的 BLOB,我想在 onActivityResult 函数中插入来自 imageView 的图像

非常感谢使用//评论和清晰解释的任何帮助

您应该只在数据库中保存图像的 URI。

您可以使用它来获取图像 uri,而不是遍历所有文件。

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)

}

然后您可以随时检索您的图像。

Bitmap myBitmap = BitmapFactory.decodeFile(path);

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

myImage.setImageBitmap(myBitmap);

以下是将 uri 转换为路径的方法:Get Path of image from ACTION_IMAGE_CAPTURE Intent