1

SDカードから選択した画像のサイズをサムネイルサイズに変更しようとしていますが、サイズを変更すると、デフォルトの画像128/128のサイズに合わせてビットマップがnullになります

InputStream input=null;
            try {
                input = getActivity().getContentResolver().openInputStream(Uri.parse(cursor.getString(3)));
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(input, null, options);
                int height = options.outHeight; //1030
                int width = options.outWidth; //1033
                BitmapFactory.decodeResource(getResources(), R.drawable.ic_contact_picture, options);

                int imageHeight = options.outHeight; //128
                int imageWidth = options.outWidth; //128


                if(imageWidth > imageHeight){
                    options.inSampleSize = Math.round((float)height/(float)imageHeight);
                }else{
                    options.inSampleSize = Math.round((float)width/(float)imageWidth);
                }
                options.inJustDecodeBounds = false;
                Bitmap bm = BitmapFactory.decodeStream(input,null, options); //null here
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                image.setImageBitmap(bm);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

画像の高さと幅の両方を取得できますが、サイズを変更すると、nullビットマップが返されます。

私はリソースファイルを使ってこのようにしたがUri、SDカードからの画像を使ったことがないので、何が間違っているのですか?

4

1 に答える 1

0

同じ入力ストリームを2回使用することはできないため、2回目に新しいInputStreamを作成する必要があります。詳細については、こちらをご覧ください。

于 2012-07-31T00:19:47.880 に答える