为什么我会收到java.io.IOException异常:标记已失效? [英] Why am I getting java.io.IOException: Mark has been invalidated?

查看:275
本文介绍了为什么我会收到java.io.IOException异常:标记已失效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下载从URL imags,然后去$ C C他们$。 问题是,我不知道他们有多大,如果我去$ C C $他们马上,应用程序崩溃,过于大的图像。

我做以下和它的工作原理与大多数图像,但其中的一部分,它抛出了 java.io.IOException异常:标记已失效例外。 这不是大小的问题,因为它具有75KB或120KB的图像,而不是用一个20MB或45KB的图像会发生。 同样的格式并不重要,因为它可以发生无论是与一个JPG或PNG图片。

PIS 的InputStream

 选项选择采用=新BitmapFactory.Options();
    的BufferedInputStream双=新的BufferedInputStream(PIS);
    bis.mark(1024 * 1024);
    opts.inJustDe codeBounds = TRUE;
    位图bmImg = BitmapFactory.de codeStream(之二,空,选择采用);

    Log.e(optwidth,opts.outWidth +);
    尝试 {
        bis.reset();
        opts.inJustDe codeBounds = FALSE;
        INT比= opts.outWidth / 800;
        Log.e(比率,将String.valueOf(比));
        如果(opts.outWidth> = 800)opts.inSampleSize =比;

        返回BitmapFactory.de codeStream(之二,空,选择采用);

    }赶上(IOException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
        返回null;
    }
 

解决方案

我想你想脱code大的图像。我选择展厅形象做到了。

 文件照片=新的文件(imageFilePath您选择);
位图B =去codeFILE(照片);
 

德codeFILE(张)功能用于去code大的图像。我想你需要获得图像的.png或.jpg formet。

 私人位图德codeFILE(文件f){
        尝试 {
            //德code图像尺寸
            BitmapFactory.Options O =新BitmapFactory.Options();
            o.inJustDe codeBounds = TRUE;
            BitmapFactory.de codeStream(新的FileInputStream(f)项,空,O);

            //找到正确的比例值。它应该是2的幂。
            最终诠释REQUIRED_SIZE = 70;
            INT width_tmp = o.outWidth,height_tmp = o.outHeight;
            int标= 1;
            而(真){
                如果(width_tmp / 2'; REQUIRED_SIZE || height_tmp / 2'; REQUIRED_SIZE)
                    打破;
                width_tmp / = 2;
                height_tmp / = 2;
                规模++;
            }

            //德code与inSampleSize
            BitmapFactory.Options O2 =新BitmapFactory.Options();
            o2.inSampleSize =规模;
            返回BitmapFactory.de codeStream(新的FileInputStream(f)项,空,O2);
        }赶上(FileNotFoundException异常E){}
        返回null;
    }
 

您可以通过使用ImageView的显示出来。

  ImageView的IMG =(ImageView的)findViewById(R.id.sdcardimage);
img.setImageBitmap(B);
 

I'm trying to download imags from a url and then decode them. The problem is that I don't know how large are they and if I decode them right away, the app crashes with too-big images.

I'm doing the following and it works with most of the images but with some of them, it throws the java.io.IOException: Mark has been invalidated exception. It's not a matter of size because it happens with a 75KB or a 120KB image and not with a 20MB or 45KB image. Also the format is not important as it can happen either with a jpg or png image.

pis is an InputStream.

    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;
    }

解决方案

I think you want to decode large image. I did it by selecting gallery image.

File photos= new File("imageFilePath that you select");
Bitmap b = decodeFile(photos);

"decodeFile(photos)" function is used for decode large image. I think you need to get image .png or .jpg formet.

 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;
    }

you can display it by using imageView.

ImageView img = (ImageView)findViewById(R.id.sdcardimage);
img.setImageBitmap(b);

这篇关于为什么我会收到java.io.IOException异常:标记已失效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆