5 Stimmen

Warum erhalte ich java.io.IOException: Mark wurde für ungültig erklärt?

Ich versuche, Imags von einer URL herunterzuladen und sie dann zu entschlüsseln. Das Problem ist, dass ich nicht weiß, wie groß sie sind und wenn ich sie sofort dekodiere, stürzt die App mit zu großen Bildern ab.

Ich tue das Folgende und es funktioniert mit den meisten Bildern, aber mit einigen von ihnen, wirft es die java.io.IOException: Mark has been invalidated Ausnahme. Es hat nichts mit der Größe zu tun, denn es passiert bei einem 75KB oder 120KB Bild und nicht bei einem 20MB oder 45KB Bild. Auch das Format spielt keine Rolle, da es sowohl bei einem jpg- als auch bei einem png-Bild auftreten kann.

pis ist ein 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;
    }

6voto

rabby Punkte 335

Ich glaube, Sie wollen ein großes Bild entschlüsseln. Ich habe es getan, indem ich das Galeriebild ausgewählt habe.

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

Die Funktion "decodeFile(photos)" wird zur Dekodierung großer Bilder verwendet. Ich denke, Sie müssen Bild .png oder .jpg formet erhalten.

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

können Sie es mit Hilfe von imageView anzeigen.

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

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X