【问题标题】:Capture, save and retrieve images/pictures using Sqlite database使用 Sqlite 数据库捕获、保存和检索图像/图片
【发布时间】:2012-11-15 10:07:20
【问题描述】:

在我的 Windows 8 应用程序(C#)中,我想捕获图像(我完成了),然后将其保存到 Sqlite 数据库。由于 Sqlite 不支持 BipmapImage、Uri 类型等(我试过但给出了不支持存储的例外情况..)我该怎么做?

我只想将使用相机捕获的图像保存在本地数据库中,然后检索这些图像(设置为绑定)。请建议我实现这一目标的其他选择。

我还尝试将 Uri 转换为字符串,然后将此字符串保存到 SQLite db 中,然后再次将字符串转换为 Uri,然后制作位图图像,但我无法做到这一点(这是正确的方法吗?)。

如果您可以与我分享任何样品,请提供。我花了很多时间,但不知道我做错了什么!

谢谢扎克

【问题讨论】:

    标签: c# sqlite windows-8 microsoft-metro


    【解决方案1】:

    虽然我不喜欢将图像保存在数据库中。

    但如果您想将图像保存在数据库中,那么一种方法是将图像转换为base64 字符串,然后将字符串保存在SQLite 数据库中。

    public string ConvertToString(Image image)
    {
        // First Convert image to byte array.
        byte[] byteArray = new byte[0];
        using (MemoryStream stream = new MemoryStream())
        {
            image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
            stream.Close();
    
            byteArray = stream.ToArray();
        }
    
        // Convert byte[] to Base64 String
        string base64String = Convert.ToBase64String(byteArray);
    
        return base64String;  
    }
    

    【讨论】:

      【解决方案2】:

      您可以通过这种方式将相机捕获的图像保存到SQLite数据库中..

      公共类 CamActivity 扩展 Activity {

      byte[] byteArray;
      private static final int CAMERA_REQUEST = 1888;
      protected static final int TAKE_PHOTO_CODE = 0;
      public ImageView imageView;
      private DBAdapter db;
      byte [] imgbyte;
      EditText txtView ;
      String name;
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          db=new DBAdapter(CamActivity.this);
          db.open();
          this.imageView = (ImageView)this.findViewById(R.id.imageView1);          
        txtView=(EditText)findViewById(R.id.editText1);
          Button B = (Button) this.findViewById(R.id.camera); 
          B.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                 // cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,getImageUri());
                  startActivityForResult(cameraIntent,CAMERA_REQUEST ); 
              }
          }); 
      
          Button save = (Button)findViewById(R.id.saving);
          save.setOnClickListener(new View.OnClickListener() {            
              public void onClick(View v) {
      
      
      
      
                  name=txtView.getText().toString();
      
      
                  try
                  {
      

      db.insertImageDetails(byteArray,name);

                  }              
      
                  catch (Exception e) {
                      e.printStackTrace();
                  }
                   //mySQLiteAdapter.close();
      
                  Toast.makeText(getApplicationContext(), "processing", Toast.LENGTH_SHORT).show();
                  Toast.makeText(getApplicationContext(), "image saved", Toast.LENGTH_SHORT).show();
      
                  }});
      
          Button G = (Button) this.findViewById(R.id.get);
          G.setOnClickListener(new View.OnClickListener() {           
              public void onClick(View v)
      
              {
      
                  Intent intent= new Intent(CamActivity.this,SecondActivity.class);
                  startActivity(intent);
      
      }
              });
          }
      

      protected void onActivityResult(int requestCode, int resultCode, Intent data) {

          //final byte[] byteArray;
          if (requestCode == CAMERA_REQUEST) {  
              Bitmap photo = (Bitmap) data.getExtras().get("data");   
             //imageView.setImageBitmap(photo);    
             ByteArrayOutputStream stream = new ByteArrayOutputStream();
             photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
               byteArray = stream.toByteArray();
             System.out.println(byteArray);
             Toast.makeText(getApplicationContext(), byteArray.toString(), Toast.LENGTH_SHORT).show();
      
          }
      
      }
      
      
      
          }
      

      【讨论】:

      • 这么多代码只是为了做一件简单的事情,你不觉得吗?
      猜你喜欢
      • 2011-09-24
      • 2015-03-16
      • 2012-11-30
      • 2016-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-22
      相关资源
      最近更新 更多