Android 将文档、xls、pdf和图像保存到sqlite数据库
我已经创建了一个应用程序,我想在其中存储word文件、excel文件、图像、ppt等到数据库中。但由于我是新手,我不知道如何存储此类文件。所以请帮助我 数据库类Android 将文档、xls、pdf和图像保存到sqlite数据库,android,android-sqlite,Android,Android Sqlite,我已经创建了一个应用程序,我想在其中存储word文件、excel文件、图像、ppt等到数据库中。但由于我是新手,我不知道如何存储此类文件。所以请帮助我 数据库类 public class DbHandler extends SQLiteOpenHelper { private static final int dbVersion = 1; private static final String dbName = "HSsuraksha"; private static
public class DbHandler extends SQLiteOpenHelper {
private static final int dbVersion = 1;
private static final String dbName = "HSsuraksha";
private static final String tableName = "pocketDocs";
private static final String userId = "userId";
private static final String docId = "docId";
private static final String fileName = "fileName";
private static final String fileExt = "fileExt";
private static final String title = "title";
private static final String createTable = "CREATE TABLE " + tableName + "(" + userId + " Integer Primary Key," + docId + " Integer," + fileName + " Text," + fileExt + " Text," + title + " Text" + ")";
public DbHandler(Context context) {
super(context, dbName, null, dbVersion);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {
}
public void insertData(DbModel dbModel) {
SQLiteDatabase database = getWritableDatabase();
ContentValues contentValues = new ContentValues();
database.beginTransaction();
contentValues.put(userId, dbModel.userId);
contentValues.put(docId, dbModel.docId);
contentValues.put(fileName, dbModel.fileName);
contentValues.put(fileExt, dbModel.fileExtension);
contentValues.put(title, dbModel.title);
if (contentValues != null) {
Long id = database.insert(tableName, null, contentValues);
Log.e("insert values", "" + id);
}
database.endTransaction();
}
public DbModel selectDocs(String id) {
SQLiteDatabase db = this.getReadableDatabase();
DbModel model = new DbModel();
Cursor cursor = db.query(tableName, new String[]{fileName, fileExt, title}, docId + "=?", new String[]{id}, null, null, null, null);
if (cursor.moveToFirst()) {
model.fileName = cursor.getString(2);
model.fileExtension = cursor.getString(3);
model.title = cursor.getString(4);
}
db.close();
return model;
}
}
数据库模型
public class DbModel implements Serializable {
public String userId, docId, fileName, fileExtension, title;
public DbModel() {
}
public DbModel(String userId, String docId, String fileName, String fileExtension, String title) {
this.userId = userId;
this.docId = docId;
this.fileName = fileName;
this.fileExtension = fileExtension;
this.title = title;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getDocId() {
return docId;
}
public void setDocId(String docId) {
this.docId = docId;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileExtension() {
return fileExtension;
}
public void setFileExtension(String fileExtension) {
this.fileExtension = fileExtension;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
从我放置数据的位置初始化
dbHandler = new DbHandler(context);
dbModel = new DbModel("1", "1", "GoogleDocs", ".pdf", "Test");
我将此库用于excel导入和导出,支持高达
ms2007
http://poi.apache.org/download.html
,轻松集成并使用
创建excel文件,其他文档请参考poi库文档
// check if available and not read only
if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
Toast.makeText(context, "No External Storage", Toast.LENGTH_SHORT).show();
Log.w("FileUtils", "Storage not available or read only");
return false;
}
boolean success = false;
//New Workbook
Workbook wb = new HSSFWorkbook();
Cell c = null;
//Cell style for header row
CellStyle cs = wb.createCellStyle();
cs.setFillForegroundColor(HSSFColor.LIME.index);
cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//New Sheet
Sheet sheet1 = null;
sheet1 = wb.createSheet("job");
Row row = sheet1.createRow(0);
c = row.createCell(0);
c.setCellValue("OperatorId : ");
c.setCellStyle(cs);
c = row.createCell(1);
c.setCellValue("OperatorName : " );
c.setCellStyle(cs);
c = row.createCell(2);
c.setCellValue("Blade : " );
c.setCellStyle(cs);
c = row.createCell(3);
c.setCellValue("ShiftType : " );
c.setCellStyle(cs);
c = row.createCell(4);
c.setCellValue("Bom : ");
c.setCellStyle(cs);
c = row.createCell(5);
c.setCellValue("Job Created at : ");
c.setCellStyle(cs);
c = row.createCell(6);
c.setCellValue("Blade Number: " );
c.setCellStyle(cs);
sheet1.setColumnWidth(0, (15 * 500));
sheet1.setColumnWidth(1, (15 * 500));
sheet1.setColumnWidth(2, (15 * 500));
sheet1.setColumnWidth(3, (15 * 500));
sheet1.setColumnWidth(4, (15 * 500));
sheet1.setColumnWidth(5, (15 * 500));
sheet1.setColumnWidth(6, (15 * 500));
Row row2 = sheet1.createRow(1);
c = row2.createCell(0);
c.setCellValue("JobId");
c.setCellStyle(cs);
c = row2.createCell(1);
c.setCellValue("BladeName");
c.setCellStyle(cs);
c = row2.createCell(2);
c.setCellValue("Start Time");
c.setCellStyle(cs);
c = row2.createCell(3);
c.setCellValue("End Time");
c.setCellStyle(cs);
c = row2.createCell(4);
c.setCellValue("Time Taken");
c.setCellStyle(cs);
c = row2.createCell(5);
c.setCellValue("Ply Number");
c.setCellStyle(cs);
SimpleDateFormat sdf = new SimpleDateFormat("HHmm_ddMMyyyy");
String currentDateandTime = sdf.format(new Date());
File file = new File(context.getExternalFilesDir(null), historyID+"History_"+currentDateandTime+".xls");
FileOutputStream os = null;
try {
os = new FileOutputStream(file);
wb.write(os);
Log.w("FileUtils", "Writing file" + file);
success = true;
} catch (IOException e) {
Log.w("FileUtils", "Error writing " + file, e);
} catch (Exception e) {
Log.w("FileUtils", "Failed to save file", e);
} finally {
try {
if (null != os)
os.close();
} catch (Exception ex) {
}
}
return success;
}
使用权限
android.permission.WRITE_EXTERNAL_STORAGE
android.permission.READ_EXTERNAL_STORAGE
// Reading a excel file
File file = new File(context.getExternalFilesDir(null), filename);
FileInputStream myInput = new FileInputStream(file);
// Create a POIFSFileSystem object
POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
// Create a workbook using the File System
HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
// Get the first sheet from workbook
HSSFSheet mySheet = myWorkBook.getSheetAt(0);
/** We now need something to iterate through the cells.**/
Iterator<Row> rowIter = mySheet.rowIterator();
Cell bladeIDcell = mySheet.getRow(0).getCell(0);
Cell bladeNameCell = mySheet.getRow(0).getCell(0);
HSSFSheet mySheet2 = myWorkBook.getSheetAt(1);
Iterator<Row> rowIter2 = mySheet2.rowIterator();
while(rowIter2.hasNext()){
HSSFRow myRow = (HSSFRow) rowIter2.next();
Iterator<Cell> cellIter = myRow.cellIterator();
ArrayList<String> Valuesfromcell = new ArrayList<String>();
while(cellIter.hasNext()){
HSSFCell myCell = (HSSFCell) cellIter.next();
Log.w("FileUtils", "Cell Value: " + myCell.toString());
Valuesfromcell.add(myCell.toString());
//Toast.makeText(context, "cell Value: " + myCell.toString(), Toast.LENGTH_SHORT).show();
}
android.permission.WRITE\u外部存储
android.permission.READ_外部存储
//读取excel文件
File File=新文件(context.getExternalFilesDir(null),文件名);
FileInputStream myInput=新的FileInputStream(文件);
//创建一个POIFSSystem对象
POIFSFileSystem myFileSystem=新的POIFSFileSystem(myInput);
//使用文件系统创建工作簿
HSSFWorkbook myWorkBook=新的HSSFWorkbook(myFileSystem);
//从工作簿中获取第一张工作表
HSSFSheet mySheet=myWorkBook.getSheetAt(0);
/**我们现在需要一些东西来遍历单元格**/
迭代器rowIter=mySheet.rowitter();
单元格bladeIDcell=mySheet.getRow(0).getCell(0);
单元格bladeNameCell=mySheet.getRow(0).getCell(0);
HSSFSheet mySheet2=myWorkBook.getSheetAt(1);
迭代器rowIter2=mySheet2.rowIter2();
while(rowIter2.hasNext()){
HSSFRow myRow=(HSSFRow)rowIter2.next();
迭代器cellIter=myRow.cellIterator();
ArrayList值fromcell=新的ArrayList();
while(cellIter.hasNext()){
HSSFCell myCell=(HSSFCell)cellIter.next();
w(“FileUtils”,“单元格值:”+myCell.toString());
值fromcell.add(myCell.toString());
//Toast.makeText(上下文,“单元格值:+myCell.toString(),Toast.LENGTH_SHORT).show();
}
改为上传到ftp服务器上,但先生,根据我的要求,我想将其存储在数据库中,万一文件中的内容太大……那么不喜欢数据库……这是一个糟糕的做法,我还有一个疑问,你能帮我解决吗???是的,我可以帮你,顺便说一句,我不是先生,你可以叫我adcom,但你的问题需要向我清楚,我不想知道创建excel文件我只想从存储器中获取文件并将其存储在数据库中通过使用此库,您只需读取文件,从文件中获取数据,就可以将数据存储在数据库中。。。。
android.permission.WRITE_EXTERNAL_STORAGE
android.permission.READ_EXTERNAL_STORAGE
// Reading a excel file
File file = new File(context.getExternalFilesDir(null), filename);
FileInputStream myInput = new FileInputStream(file);
// Create a POIFSFileSystem object
POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
// Create a workbook using the File System
HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
// Get the first sheet from workbook
HSSFSheet mySheet = myWorkBook.getSheetAt(0);
/** We now need something to iterate through the cells.**/
Iterator<Row> rowIter = mySheet.rowIterator();
Cell bladeIDcell = mySheet.getRow(0).getCell(0);
Cell bladeNameCell = mySheet.getRow(0).getCell(0);
HSSFSheet mySheet2 = myWorkBook.getSheetAt(1);
Iterator<Row> rowIter2 = mySheet2.rowIterator();
while(rowIter2.hasNext()){
HSSFRow myRow = (HSSFRow) rowIter2.next();
Iterator<Cell> cellIter = myRow.cellIterator();
ArrayList<String> Valuesfromcell = new ArrayList<String>();
while(cellIter.hasNext()){
HSSFCell myCell = (HSSFCell) cellIter.next();
Log.w("FileUtils", "Cell Value: " + myCell.toString());
Valuesfromcell.add(myCell.toString());
//Toast.makeText(context, "cell Value: " + myCell.toString(), Toast.LENGTH_SHORT).show();
}