【问题标题】:Android - What's the best way to store image to SQLite?Android - 将图像存储到 SQLite 的最佳方式是什么?
【发布时间】:2012-11-17 18:57:22
【问题描述】:

我正在开发一个离线应用程序,但我没有从用户的图库中获取任何图片。 但我想向我手动插入的用户显示图像。

我能想到的唯一解决方案是将这些图像放入drawable,然后将其保存到 sqlite 数据库(我不确定如何)。这个可以吗?

谢谢。

【问题讨论】:

  • 您不能(轻松)将文件添加到可绘制文件夹。您必须将它们保存到您可以创建的 SD 卡中的应用程序文件夹中。您可能还想寻找内容提供商以增加功能。 developer.android.com/guide/topics/providers/…
  • 我明白了。如果应用是在线应用怎么办?
  • 如果你将图像下载到设备上,也是一样的。如果您只是显示一个页面,那么您可能需要查看 WebView。

标签: android image sqlite


【解决方案1】:

如果您愿意将图像存储在可绘制文件夹中,您可以在数据库中存储对它们的引用。只需存储一个类似com.example.yourapp:drawable/imagefilename 的字符串并使用它来显示图像,其中yourString 包含来自数据库的字符串..

int imageResource = this.getResources().getIdentifier(yourString, null, null);
yourimageview.setImageResource(imageResource);

【讨论】:

    【解决方案2】:

    您可以为此使用插入 blob

        public void insertImg(int id , Bitmap img ) {   
    
    
        byte[] data = getBitmapAsByteArray(img); // this is a function
    
        insertStatement_logo.bindLong(1, id);       
        insertStatement_logo.bindBlob(2, data);
    
        insertStatement_logo.executeInsert();
        insertStatement_logo.clearBindings() ;
    
    }
    
     public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.PNG, 0, outputStream);       
        return outputStream.toByteArray();
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-09
      • 2018-03-03
      • 2013-05-23
      • 1970-01-01
      • 1970-01-01
      • 2013-02-04
      • 2019-02-13
      • 2019-09-21
      • 2014-10-21
      相关资源
      最近更新 更多