为什么会出现java.io.IOException:标记已失效?

| 我正在尝试从URL下载图像,然后对其进行解码。 问题是我不知道它们有多大,如果我立即对其进行解码,则应用程序会崩溃并显示太大的图像。 我正在执行以下操作,并且它适用于大多数图像,但是对于其中一些图像,它将引发“ 0”异常。 这与大小无关,因为它发生在75KB或120KB的图像上,而不发生在20MB或45KB的图像上。 同样,格式也不重要,因为它可以在jpg或png图像中发生。
pis
是an2ѭ。
    Options opts = new BitmapFactory.Options();
    BufferedInputStream bis = new BufferedInputStream(pis);
    bis.mark(1024 * 1024);
    opts.inJustDecodeBounds = true;
    Bitmap bmImg=BitmapFactory.decodeStream(bis,null,opts);

    Log.e(\"optwidth\",opts.outWidth+\"\");
    try {
        bis.reset();
        opts.inJustDecodeBounds = false;
        int ratio = opts.outWidth/800; 
        Log.e(\"ratio\",String.valueOf(ratio));
        if (opts.outWidth>=800)opts.inSampleSize = ratio;

        return BitmapFactory.decodeStream(bis,null,opts);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
    
已邀请:
        我认为您想解码大图像。我通过选择图库图像来做到这一点。
File photos= new File(\"imageFilePath that you select\");
Bitmap b = decodeFile(photos);
\“ decodeFile(photos)\”函数用于解码大图像。我认为您需要获取图片.png或.jpg格式。
 private Bitmap decodeFile(File f){
        try {
            //decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);

            //Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE=70;
            int width_tmp=o.outWidth, height_tmp=o.outHeight;
            int scale=1;
            while(true){
                if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                    break;
                width_tmp/=2;
                height_tmp/=2;
                scale++;
            }

            //decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {}
        return null;
    }
您可以使用imageView显示它。
ImageView img = (ImageView)findViewById(R.id.sdcardimage);
img.setImageBitmap(b);
    

要回复问题请先登录注册