Android 从SD卡读取位图时出现StreamCorruptedException

Android 从SD卡读取位图时出现StreamCorruptedException,android,sd-card,Android,Sd Card,我有一个返回jpg的web服务。我将这个jpg读入一个字节[],转换成位图,并将其保存到SD卡。下次用户来此活动时,它将在点击web服务之前搜索SD卡以查看图像是否存在 但是,如果文件存在,检查SD卡的代码将返回StreamCorruptedException 以下是我写入SD卡的代码: if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { String root = Environm

我有一个返回jpg的web服务。我将这个jpg读入一个字节[],转换成位图,并将其保存到SD卡。下次用户来此活动时,它将在点击web服务之前搜索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卡上现有图像的代码:

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) {
    }
}

异常发生在新建ObjectInputStream(fis)期间。

您不能像这样任意读取Object()。需要使用writeObject()才能让readObject()检测有效的序列化对象。

BitmapFactory.decodeStream(InputStream是,Rect outPadding,BitmapFactory.Options选项)或其中一个decodeFile()API如何?