如何从sqlite的单元格中添加Blob中的图像?

时间:2019-01-08 14:44:49

标签: excel sqlite blob jxl

我想使用包含字符串数据和图像的数据库将它们转换为Excel。然后,我在sqlite中存储了一系列数据字符串和一个图像(Blob),但是我不明白如何在excel单元格中显示图像。数据库已正确加载,但对我来说很难重新转换Blob(我将其存储为字节数组)并显示它。我正在使用JXL库。希望您能帮助我,谢谢! 这是我的代码:

private void openActivity() {
    //add your further process after giving permission or to download images from remote server.
    DBHelper dbHelper = new DBHelper ( this );
    dbHelper.insertData ();

    final Cursor cursor = dbHelper.getuser ();

    File sd = Environment.getExternalStorageDirectory ();
    String csvFile = "LectorDeEstados.xls";

    File directory = new File ( sd.getAbsolutePath () );
    //create directory if not exist
    if (!directory.isDirectory ()) {
        directory.mkdirs ();
    }
    try {

        //file path
        File file = new File ( directory, csvFile );
        WorkbookSettings wbSettings = new WorkbookSettings ();
        wbSettings.setLocale ( new Locale ( "en", "EN" ) );
        WritableWorkbook workbook;
        workbook = Workbook.createWorkbook ( file, wbSettings );

        WritableSheet sheet = null;


        sheet = workbook.createSheet ( "Lectura de estados - Cooperativa", 0 );



            sheet.addCell ( new Label ( 0, 0, "ID" ) );
            sheet.addCell ( new Label ( 1, 0, "Nombre" ) );
            sheet.addCell ( new Label ( 2, 0, "Domicilio" ) );
            sheet.addCell ( new Label ( 3, 0, "Número de Medidor" ) );
            sheet.addCell ( new Label ( 4, 0, "Ultima Lectura" ) );
            sheet.addCell ( new Label ( 5, 0, "Foto" ) );

            if (cursor.moveToFirst ()) {
                do {
                    String title = cursor.getString ( cursor.getColumnIndex ( "id" ) );
                    String email = cursor.getString ( cursor.getColumnIndex ( "name" ) );
                    String mobile = cursor.getString ( cursor.getColumnIndex ( "address" ) );
                    String mobile2 = cursor.getString ( cursor.getColumnIndex ( "medidor" ) );
                    String mobile1 = cursor.getString ( cursor.getColumnIndex ( "nueva" ) );
                  //  byte[] mobile3 = cursor.getBlob ( cursor.getColumnIndex ( "foto" ) );
                    byte[] mobile3 = cursor.getBlob(cursor.getColumnIndex ("foto"));



                    int i = cursor.getPosition () + 1;

                    sheet.addCell ( new Label ( 0, i, title ) );
                    sheet.addCell ( new Label ( 1, i, email ) );
                    sheet.addCell ( new Label ( 2, i, mobile ) );
                    sheet.addCell ( new Label ( 3, i, mobile2 ) );
                    sheet.addCell ( new Label ( 4, i, mobile1 ) );


                    sheet.addCell ( new Label ( 5, i, mobile3 )  );

                } while (cursor.moveToNext ());
            }

            //closing cursor
            cursor.close ();
            workbook.write ();
            workbook.close ();
            Toast.makeText ( getApplication (), " EXCEL ha sido Exportado a la Memoria Interna del Dispositivo ", Toast.LENGTH_LONG ).show ();


        } catch(Exception e){
            e.printStackTrace ();
        }

    }

0 个答案:

没有答案