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

时间:2015-01-30 14:00:46

标签: java android database sqlite imageview

我创建了一个表单,可以将信息保存到我的数据库中,包括图像。我已经将图像插入到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中的图像

高度赞赏以评论和清晰度解释的任何帮助

1 个答案:

答案 0 :(得分:1)

您应该只保存数据库中图像的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