2012-03-26 116 views
0

图像存储在我的SQLite数据库中,我需要将用户选择的图像附加到电子邮件/ mms应用程序。但是这样的应用程序只能通过URI格式(Intent.EXTRA_STREAM)接受图像,而不是直接接收字节数组/图像本身。存储在SQLite数据库中的图像的URI

所以现在我需要存储在SQLite数据库中的图像的URI。我如何检索它? 或者我需要为我的自定义内容提供者编写它吗?

+0

是不是只是一个字符串的URI?将其存储在表格的另一个字段中。 – 2012-03-29 17:28:25

回答

0

听起来好像是你需要编写自己的内容提供商,如你所提到的 - 没有之一,因此,操作系统没有办法你的数据库与URI

0

此代码为我交往。你可以得到适当的解决方案

Uri selectedImage = data.getData(); 
     ImageView imageView=(ImageView)this.findViewById(R.id.imageView1); 

     getContentResolver().notifyChange(selectedImage, null); 

     ContentResolver cr = getContentResolver(); 
     Bitmap bitmap; 
     try { 


      bitmap = android.provider.MediaStore.Images.Media 
      .getBitmap(cr, selectedImage); 
      imageView.setImageBitmap(bitmap); 

      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
      byte[] b = baos.toByteArray(); 
      String encodedImageString = Base64.encodeToString(b, Base64.DEFAULT); 

      byte[] bytarray = Base64.decode(encodedImageString, Base64.DEFAULT); 
      Bitmap bmimage = BitmapFactory.decodeByteArray(bytarray, 0, 
        bytarray.length); 

      myDb.execSQL("INSERT INTO imgtbl VALUES('"+encodedImageString+"');"); 
      Cursor c= myDb.rawQuery("SELECT * FROM imgtbl", null); 

      c.moveToFirst(); 
      String img=c.getString(0); 
      byte[] byarray = Base64.decode(img, Base64.DEFAULT); 
      Bitmap bmimg = BitmapFactory.decodeByteArray(byarray, 0, 
        byarray.length); 

      ImageView iv=(ImageView)findViewById(R.id.img2); 
      ImageView iv1=(ImageView)findViewById(R.id.img3); 

      iv.setImageBitmap(bmimg); 
      iv1.setImageBitmap(bmimg); 


     } catch (Exception e) { 
      Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT) 
        .show(); 

      e.printStackTrace(); 
     } 
相关问题