2011-06-18 9 views
5

私は、URLからimagsをダウンロードし、それらを解読しようとしています。 問題は、どれくらいの大きさなのか分からず、すぐにデコードすれば、大きすぎる画像でアプリがクラッシュすることです。なぜ私はj​​ava.io.IOExceptionを取得していますか:マークが無効になっていますか?

私は以下のことをしています。ほとんどの画像で動作しますが、一部ではjava.io.IOException: Mark has been invalidated例外をスローします。 サイズの問題ではありません。サイズが75KBまたは120KBのイメージで、20MBまたは45KBのイメージでは発生しないからです。 また、フォーマットはjpgまたはpngイメージのいずれかで発生する可能性があるため、重要ではありません。

pisは、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; 
    } 

答えて

6

大きな画像をデコードしたいと思います。私はギャラリー画像を選んでそれをしました。

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

"decodeFile(photos)"関数は、大きな画像をデコードするために使用されます。私はあなたがimage.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); 
+0

しかし、どのように私はビットマップに変換するための情報が含まれているInputStreamを渡すことができますか?¿ – sergi

+0

この@sergiはそれを行うための最善の方法です。あなたはInputStreamを持っていて、 'decodeStream()'は入力ストリームparamをとります。 – Haphazard

+0

今私はこの方法で作業しているが、それを行う最良の方法を見たいと思った。 – sergi

関連する問題