4

入力ストリームで見つかった画像の高さと幅を取得する必要があります。これが私がしたことです:

private Boolean testSize(InputStream inputStream){
        BitmapFactory.Options Bitmp_Options = new BitmapFactory.Options();
        Bitmp_Options.inJustDecodeBounds = true;
        BitmapFactory.decodeResourceStream(getResources(), new TypedValue(), inputStream, new Rect(), Bitmp_Options);
        int currentImageHeight = Bitmp_Options.outHeight;
        int currentImageWidth = Bitmp_Options.outWidth;
        Bitmp_Options.inJustDecodeBounds = false;
    if(currentImageHeight < 200 || currentImageWidth < 200){
        Object obj = map.remove(pageCounter);
        Log.i("Page recycled", obj.toString());
        return true;
    }
    return false;}

ポイントの問題にスキップします:

以下の2番目のメソッドで計算した後、強制的にfalseにしたとしても、BitmapFactory.Optionsが変更されます。

private Bitmap getBitmap(InputStream InpStream){
    Bitmap originalBitmap = BitmapFactory.decodeStream(InpStream);//Null.
    return originalBitmap;
}

私の質問に、入力ストリームから画像のサイズと幅を取得する別の方法はありますか? これについて本当に助けが必要です。どんな助けでも大歓迎です。

                ZipInputStream zip = null;
                zip = new ZipInputStream(new FileInputStream(getFileLocation()));
                for(ZipEntry zip_e = zip.getNextEntry(); zip_e != null ; zip_e = zip.getNextEntry()){
                    if(zip_e.isDirectory()) {
                        continue;
                    }
                    String file_zip = zip_e.getName();
                    String comparison = map.get(pageCounter).getHref();
                    if(file_zip.endsWith(comparison)){
                        SpannableString Spanable_String = new SpannableString("abc");
                        if(testSize(zip)){
                            map.remove(pageCounter);
                            return false;
                        }
                        Bitmap bitmap = getBitmap(zip);
                        if(bitmap == null){
                            map.remove(pageCounter);
                            return false;
                        }
                        image_page.put(zip_e.getName(), zip);
                        Drawable drawable_image = new FastBitmapDrawable(bitmap);
                        drawable_image.setBounds(0,0,drawable_image.getIntrinsicWidth(), drawable_image.getIntrinsicHeight());
                        ImageSpan imageSpan = new ImageSpan(drawable_image, ImageSpan.ALIGN_BASELINE);
                        Spanable_String.setSpan(imageSpan, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        tv.setText(Spanable_String);
                        return false;
                    }
                }   
4

1 に答える 1

10

主な問題はBitmapFactory.Optionsではなく にありInputStreamます。ストリームはシーケンシャルであるため、ストリームを読み取って画像のサイズのみをデコードすると、ストリーム内のポインターが移動します。次に、ビットマップを実際にデコードするために再度呼び出すとdecode、ストリーム ポインターがビットマップの先頭になくなりnull、部分ストリームをデコードできないために取得されます。

ストリームをリセットする必要があります。そのストリームが何であるかに応じて、使用できる場合があり.markます.reset。基本的に、次のようにします。

private Boolean testSize(InputStream inputStream) {
    BitmapFactory.Options Bitmp_Options = new BitmapFactory.Options();
    Bitmp_Options.inJustDecodeBounds = true;

    inputStream.mark(inputSteram.available());

    BitmapFactory.decodeResourceStream(getResources(), new TypedValue(), inputStream, new Rect(), Bitmp_Options);

    inputSteram.reset();

    int currentImageHeight = Bitmp_Options.outHeight;
    int currentImageWidth = Bitmp_Options.outWidth;
    Bitmp_Options.inJustDecodeBounds = false;

    if(currentImageHeight < 200 || currentImageWidth < 200){
        Object obj = map.remove(pageCounter);
        Log.i("Page recycled", obj.toString());
        return true;
    }

    return false;
}

InputStream呼び出すことで、これをサポートしているかどうかを確認できますmarkSupported。それが返された場合trueは、上記の手法を使用できます。が返された場合false、上記の方法は機能せず、完全なビットマップをデコードする前に、ストリームを閉じて再度開く必要があります。

于 2012-06-26T19:16:39.647 に答える