我有一个Web服务,返回一个JPG。我将这个jpg读入一个byte [],转换成一个Bitmap,并保存到SD卡。用户下次参加此活动时,它将搜索SD卡,以查看在访问Web服务之前图像是否存在。StreamCorruptedException当从SD卡读取位图
但是,如果文件存在,则检查SD卡的代码将返回StreamCorruptedException。
这里是我的代码写入到SD卡:
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String root = Environment.getExternalStorageDirectory().toString();
new File(root + "/images").mkdirs();
try {
File file = new File(root + "/images", Integer.toString(intImageId) + "m.jpg");
FileOutputStream os = new FileOutputStream(file);
Bitmap theImageFromByteArray = BitmapFactory.decodeByteArray(image, 0, image.length);
theImageFromByteArray.compress(Bitmap.CompressFormat.JPEG, 80, os);
os.flush();
os.close();
}
catch (FileNotFoundException e) {
}
catch (IOException e) {
}
}
这里是我的代码,检查现有的图像SD卡:在新的ObjectInputStream
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
|| Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
try {
File file = new File(Environment.getExternalStorageDirectory() + "/images", Integer.toString(mImageId) + "m.jpg");
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
mImage = (Bitmap)ois.readObject();
ois.close();
}
catch (Exception e) {
}
}
异常情况( fis)
那么我该如何根据自己写的文件来读取文件呢? – Andrew
什么是BitmapFactory.decodeStream(InputStream是,Rect outPadding,BitmapFactory.Options opts)还是decodeFile()API之一呢? [DOC](http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeFile%28java.lang.String%29) – ribram