【问题标题】:set background image from sqlite database从 sqlite 数据库设置背景图像
【发布时间】:2012-04-28 15:35:19
【问题描述】:

在 Android 中,我想通过从数据库中检索图像来设置背景图像。我已经创建了数据库并将图像作为 BLOB 放入。数据库在我的资产文件夹中,应用程序可以成功访问和查询数据库(但这可能是因为我还没有访问图像)。但现在我不知道从这里去哪里。我想我已经完成了类似的代码:

bgview = (View) findViewById(R.id.bg_display);
bgview.setBackgroundResource(bgimage);

但我不知道如何启动它.....

【问题讨论】:

    标签: android sqlite


    【解决方案1】:

    只在数据库中存储图像的路径而不是图像,您可以检索路径并将其设置为背景。使用它您可以摆脱转换为字节。

    【讨论】:

      【解决方案2】:

      你可以创建

      InputStream is = new ByteArrayInputStream(TheBytesOfTheBlobYouGotFromDB);
      

      保存到文件中

      试试{ OutputStream out = new FileOutputStream(new File(FileName));

      int read = 0;
      byte[] bytes = new byte[1024];
      
      while ((read = is.read(bytes)) != -1) { out.write(bytes, 0, read); }
      
      is.close();
      out.flush();
      out.close();
      } catch (IOException e) {}
      

      然后使用解决方案将文件设置为背景: place a bitmap as background of a view

      Matrix Mat = new Matrix();
      
      /// FileName is the file where you saved the 'is'
      Bitmap Source = BitmapFactory.decodeFile(FileName);
      Bitmap Destination = Bitmap.createScaledBitmap( Source, DisplayWidth, DisplayHeight, true );
      
      Source = Bitmap.createBitmap( Destination, 0, 0, Destination.getWidth(), Destination.getHeight(), Mat, true );
      
      /// Use the 'Source' here
      bgview.setBackgroundResource(Source);
      

      【讨论】:

        猜你喜欢
        • 2018-06-05
        • 2020-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-29
        • 2018-06-30
        相关资源
        最近更新 更多