sqlite 存储图片java_使用Eclipse SWT Image,JAVA和SQLite来插入,存储和...

我正在编写一个基本的Java应用程序,允许用户将有关个人的详细信息插入到SQLite数据库中.我正在使用Eclipse SWT作为GUI.

Eclipse SWT定义了一个用于在GUI中显示Images的Image(org.eclipse.swt.graphics.Image).

我试图允许用户浏览文件系统,选择一个图像,然后将该图像插入数据库.我还希望能够从数据库中检索该图像并将其显示在GUI中.

一切都很简单,但对于我的生活,我无法让它工作!我也搜索了很多,似乎无法找到解决方案.

我正在使用Eclipse IDE for Java Developers(3.6),sqlite-jdbc-3.7.2.jar和JDK 1.6.0_22.

import org.eclipse.swt.*;

import org.eclipse.swt.events.*;

import org.eclipse.swt.layout.*;

import org.eclipse.swt.widgets.*;

import org.eclipse.swt.graphics.*;

import java.sql.*;

/***********************************************************************/

/*** Tests reading and writing SWT Images from an SQLite Database ***/

/***********************************************************************/

public class ImageTest {

Shell shell;

//Variables to store the current values when editing

private Canvas personPhoto;

private Image personImage;

private int personID = 1;

private double photoWidth = 100;

private double photoHeight = 100;

//Database connection and statement variables

private static Connection connection = null;

private static Statement statement = null;

private static PreparedStatement ps = null;

private static ResultSet rs = null;

public ImageTest(Shell parent, Connection passedConnection) {

shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);

shell.setLayout(new GridLayout());

connection = passedConnection;

}

private void createControlButtons() {

Composite composite = new Composite(shell, SWT.NONE);

composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));

GridLayout layout = new GridLayout();

layout.numColumns = 2;

composite.setLayout(layout);

Button okButton = new Button(composite, SWT.PUSH);

okButton.setText("OK");

okButton.addSelectionListener(new SelectionAdapter() {

public void widgetSelected(SelectionEvent e) {

if(personID > 0){

try {

PreparedStatement ps = connection.prepareStatement("UPDATE person SET photo = ? " +

"WHERE person_id = ?");

ps.setBytes(1, personImage.getImageData().data);

ps.setInt(2, personID);

ps.executeUpdate();

ps.close();

} catch (SQLException err) {

err.printStackTrace();

}

} else {

try {

PreparedStatement ps = connection.prepareStatement("INSERT INTO person (photo) VALUES (?)");

ps.setBytes(1, personImage.getImageData().data);

ps.executeUpdate();

ps.close();

} catch (SQLException err) {

err.printStackTrace();

}

}

shell.close();

}

});

Button cancelButton = new Button(composite, SWT.PUSH);

cancelButton.setText("Cancel");

cancelButton.addSelectionListener(new SelectionAdapter() {

public void widgetSelected(SelectionEvent e) {

shell.close();

}

});

shell.setDefaultButton(okButton);

}

private void createTextWidgets(final Display display) {

GridLayout gridLayout = new GridLayout();

gridLayout.numColumns = 2;

shell.setLayout(gridLayout);

new Label(shell, SWT.NONE).setText("Photo:");

personPhoto = new Canvas(shell, SWT.BORDER);

GridData gridData = new GridData(GridData.FILL, GridData.FILL, true, true);

gridData.widthHint = (int)photoWidth;

gridData.heightHint = (int)photoHeight;

gridData.verticalSpan = 5;

gridData.horizontalSpan = 2;

personPhoto.setLayoutData(gridData);

personPhoto.redraw();

personPhoto.addPaintListener(new PaintListener() {

public void paintControl(final PaintEvent event) {

if (personImage != null) {

event.gc.drawImage(personImage, 0, 0);

}

}

});

//Skip a Column

new Label(shell, SWT.NONE);

Button browse = new Button(shell, SWT.PUSH);

browse.setText("Browse...");

gridData = new GridData(GridData.FILL, GridData.CENTER, true, false);

gridData.horizontalIndent = 5;

browse.setLayoutData(gridData);

browse.addSelectionListener(new SelectionAdapter() {

public void widgetSelected(SelectionEvent event) {

String fileName = new FileDialog(shell).open();

if (fileName != null) {

personImage = new Image(display, fileName);

personPhoto.redraw();

}

}

});

Button delete = new Button(shell, SWT.PUSH);

delete.setText("Delete");

gridData = new GridData(GridData.FILL, GridData.BEGINNING, true, false);

gridData.horizontalIndent = 5;

delete.setLayoutData(gridData);

delete.addSelectionListener(new SelectionAdapter() {

public void widgetSelected(SelectionEvent event) {

if (personImage != null) {

personImage.dispose();

personImage = null;

personPhoto.redraw();

}

}

});

//Skip a Column

new Label(shell, SWT.NONE);

//Skip two Rows

new Label(shell, SWT.NONE);

new Label(shell, SWT.NONE);

new Label(shell, SWT.NONE);

new Label(shell, SWT.NONE);

}

public void open() {

Display display = shell.getDisplay();

//To avoid null pointer exceptions

personImage = new Image(display,"user.png");

try{

PreparedStatement ps = connection.prepareStatement("SELECT photo FROM person WHERE person_id = ?");

ps.setInt(1, personID);

rs = ps.executeQuery();

while (rs.next()) {

//dispose of the current image

personImage.dispose();

personImage = new Image(display, (int) photoWidth, (int) photoHeight);

//Retrieve the photo for this person

personImage.getImageData().data = rs.getBytes("photo");

}

ps.close();

rs.close();

} catch (SQLException e) {

e.printStackTrace();

}

createTextWidgets(display);

createControlButtons();

shell.pack();

shell.open();

while(!shell.isDisposed()){

if(!display.readAndDispatch())

display.sleep();

}

}

}

我现在已经简化了代码,但我仍然无法正确地从数据库中提取字节数组并将其显示为SWT图像.有人有什么想法吗?任何帮助将非常感激!

吉文

/* Imports */

import org.eclipse.swt.*;

import org.eclipse.swt.events.*;

import org.eclipse.swt.layout.*;

import org.eclipse.swt.widgets.*;

import org.eclipse.swt.graphics.*;

import java.sql.*;

/***********************************************************************/

/*** Tests reading and writing SWT Images from an SQLite Database ***/

/***********************************************************************/

public class ImageTest {

Shell shell;

//Variables to store the current values when editing

private Canvas personPhoto;

private Image personImage;

private int personID = 1;

private double photoWidth = 100;

private double photoHeight = 100;

//Database connection and statement variables

private static Connection connection = null;

private static Statement statement = null;

private static PreparedStatement ps = null;

private static ResultSet rs = null;

public ImageTest(Shell parent, Connection passedConnection) {

shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);

shell.setLayout(new GridLayout());

connection = passedConnection;

}

private void createControlButtons() {

Composite composite = new Composite(shell, SWT.NONE);

composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));

GridLayout layout = new GridLayout();

layout.numColumns = 2;

composite.setLayout(layout);

Button okButton = new Button(composite, SWT.PUSH);

okButton.setText("OK");

okButton.addSelectionListener(new SelectionAdapter() {

public void widgetSelected(SelectionEvent e) {

if(personID > 0){

try {

PreparedStatement ps = connection.prepareStatement("UPDATE person SET photo = ? " +

"WHERE person_id = ?");

ps.setBytes(1, personImage.getImageData().data);

ps.setInt(2, personID);

ps.executeUpdate();

ps.close();

} catch (SQLException err) {

err.printStackTrace();

}

} else {

try {

PreparedStatement ps = connection.prepareStatement("INSERT INTO person (photo) VALUES (?)");

ps.setBytes(1, personImage.getImageData().data);

ps.executeUpdate();

ps.close();

} catch (SQLException err) {

err.printStackTrace();

}

}

shell.close();

}

});

Button cancelButton = new Button(composite, SWT.PUSH);

cancelButton.setText("Cancel");

cancelButton.addSelectionListener(new SelectionAdapter() {

public void widgetSelected(SelectionEvent e) {

shell.close();

}

});

shell.setDefaultButton(okButton);

}

private void createTextWidgets(final Display display) {

GridLayout gridLayout = new GridLayout();

gridLayout.numColumns = 2;

shell.setLayout(gridLayout);

new Label(shell, SWT.NONE).setText("Photo:");

personPhoto = new Canvas(shell, SWT.BORDER);

GridData gridData = new GridData(GridData.FILL, GridData.FILL, true, true);

gridData.widthHint = (int)photoWidth;

gridData.heightHint = (int)photoHeight;

gridData.verticalSpan = 5;

gridData.horizontalSpan = 2;

personPhoto.setLayoutData(gridData);

personPhoto.redraw();

personPhoto.addPaintListener(new PaintListener() {

public void paintControl(final PaintEvent event) {

if (personImage != null) {

event.gc.drawImage(personImage, 0, 0);

}

}

});

//Skip a Column

new Label(shell, SWT.NONE);

Button browse = new Button(shell, SWT.PUSH);

browse.setText("Browse...");

gridData = new GridData(GridData.FILL, GridData.CENTER, true, false);

gridData.horizontalIndent = 5;

browse.setLayoutData(gridData);

browse.addSelectionListener(new SelectionAdapter() {

public void widgetSelected(SelectionEvent event) {

String fileName = new FileDialog(shell).open();

if (fileName != null) {

personImage = new Image(display, fileName);

personPhoto.redraw();

}

}

});

Button delete = new Button(shell, SWT.PUSH);

delete.setText("Delete");

gridData = new GridData(GridData.FILL, GridData.BEGINNING, true, false);

gridData.horizontalIndent = 5;

delete.setLayoutData(gridData);

delete.addSelectionListener(new SelectionAdapter() {

public void widgetSelected(SelectionEvent event) {

if (personImage != null) {

personImage.dispose();

personImage = null;

personPhoto.redraw();

}

}

});

//Skip a Column

new Label(shell, SWT.NONE);

//Skip two Rows

new Label(shell, SWT.NONE);

new Label(shell, SWT.NONE);

new Label(shell, SWT.NONE);

new Label(shell, SWT.NONE);

}

public void open() {

Display display = shell.getDisplay();

//To avoid null pointer exceptions

personImage = new Image(display,"user.png");

try{

PreparedStatement ps = connection.prepareStatement("SELECT photo FROM person WHERE person_id = ?");

ps.setInt(1, personID);

rs = ps.executeQuery();

while (rs.next()) {

//dispose of the current image

personImage.dispose();

personImage = new Image(display, (int) photoWidth, (int) photoHeight);

//Retrieve the photo for this person

personImage.getImageData().data = rs.getBytes("photo");

}

ps.close();

rs.close();

} catch (SQLException e) {

e.printStackTrace();

}

createTextWidgets(display);

createControlButtons();

shell.pack();

shell.open();

while(!shell.isDisposed()){

if(!display.readAndDispatch())

display.sleep();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android SQLite可以存储图片,这里提供一种简单的方法。 首先,将图片转换为字节数组,然后将其存储为BLOB类型的数据。以下是一个示例方法,将位图转换为字节数组: ```java public static byte[] getBytesFromBitmap(Bitmap bitmap) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); return stream.toByteArray(); } ``` 接下来,将字节数组存储SQLite数据库中。以下是一个示例方法,将字节数组存储SQLite数据库中: ```java public void insertImage(byte[] imageBytes) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("image", imageBytes); db.insert("images", null, values); db.close(); } ``` 最后,从SQLite数据库中检索图像并将其转换回位图。以下是一个示例方法,从SQLite数据库中检索图像并将其转换回位图: ```java public Bitmap getImage(int id) { SQLiteDatabase db = this.getReadableDatabase(); String[] columns = new String[] { "image" }; String whereClause = "id=?"; String[] whereArgs = new String[] { String.valueOf(id) }; Cursor cursor = db.query("images", columns, whereClause, whereArgs, null, null, null); if (cursor.moveToFirst()) { byte[] imageBytes = cursor.getBlob(0); cursor.close(); db.close(); return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length); } cursor.close(); db.close(); return null; } ``` 这些方法应该能够帮助你在Android SQLite存储和检索图像。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值