Android读写临时文件
您好,我创建了这个方法来执行以下操作:
- 它需要一个位图
- 创建临时文件
- 压缩位图并将其放入临时文件
- 打开临时文件
- 读取字节 >> 问题出在我的代码中
- 保存sqlite db 中的这些字节
- 从 sqlite db 中检索字节
- 再次将这些字节设置为图像位图
我知道我知道完全没用,但这是我发现做我想要实现的目标的唯一方法,即:
- 压缩位图
- 将其保存到 sqlite 数据库,
所以这里是代码。程序在 in.read(bb) 处崩溃:
private void updateBitmapData() {
File file = null;
FileOutputStream stream = null;
try {
file = File.createTempFile("image", null, getBaseContext().getFilesDir());
stream = new FileOutputStream(file);
System.out.println(thumbnail.getRowBytes());
thumbnail.compress(Bitmap.CompressFormat.JPEG, 50, stream);
stream.close();
FileInputStream in=new FileInputStream(file);
byte[] bb = null;
in.read(bb); //crash
in.close();
System.out.println("despues compression"+bb.length);
DBAdapter db=new DBAdapter(getApplicationContext());
db.insertData(bb);
Cursor c=db.getLastImage();
if(c.moveToFirst()){
bb=c.getBlob(0);
}
c.close();
db.close();
im.setImageBitmap(BitmapFactory.decodeByteArray(bb, 0, bb.length));
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.e("NuevaIncidencia", e.getLocalizedMessage());
im.setImageBitmap(thumbnail);
}catch (IOException e1) {
Log.e("NuevaIncidencia", e1.getLocalizedMessage());
e1.printStackTrace();
im.setImageBitmap(thumbnail);
} catch (Exception e2){
e2.printStackTrace();
//Log.e("NuevaIncidencia", e2.getLocalizedMessage());
}
}
我该如何解决这个问题?谢谢
Hi I have created this method to do the folowing:
-it takes a bitmap
-creates temp file
-compress bitmap and put it in temp file
-open temp file
-read bytes >>>problem is here in my code
-save these bytes in sqlite db
-retrieve bytes from sqlite db
-set those bytes as image bitmap again
I know I know totally useless but this is the only way I have found to do what I want to achieve which is:
-compress a bitmap
-save it to sqlite database
so here is the code. the program just crashes at in.read(bb):
private void updateBitmapData() {
File file = null;
FileOutputStream stream = null;
try {
file = File.createTempFile("image", null, getBaseContext().getFilesDir());
stream = new FileOutputStream(file);
System.out.println(thumbnail.getRowBytes());
thumbnail.compress(Bitmap.CompressFormat.JPEG, 50, stream);
stream.close();
FileInputStream in=new FileInputStream(file);
byte[] bb = null;
in.read(bb); //crash
in.close();
System.out.println("despues compression"+bb.length);
DBAdapter db=new DBAdapter(getApplicationContext());
db.insertData(bb);
Cursor c=db.getLastImage();
if(c.moveToFirst()){
bb=c.getBlob(0);
}
c.close();
db.close();
im.setImageBitmap(BitmapFactory.decodeByteArray(bb, 0, bb.length));
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.e("NuevaIncidencia", e.getLocalizedMessage());
im.setImageBitmap(thumbnail);
}catch (IOException e1) {
Log.e("NuevaIncidencia", e1.getLocalizedMessage());
e1.printStackTrace();
im.setImageBitmap(thumbnail);
} catch (Exception e2){
e2.printStackTrace();
//Log.e("NuevaIncidencia", e2.getLocalizedMessage());
}
}
How could I solve this problem? thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜你遇到了 NullPointerException,这似乎很正常,因为当你调用 read 时你的缓冲区为空。
您必须实例化具有预期大小的缓冲区。
尝试用这种方式初始化你的字节数组:
byte[] bb = new byte[file.length()];
希望它对
Jokahero有帮助
I guess you got a NullPointerException, and it seems to be normal because your buffer is null when you call read.
You have to instantiate the buffer with the expected size.
Try initializing your byte array this way:
byte[] bb = new byte[file.length()];
Hope it helps
Jokahero