web-dev-qa-db-ja.com

ビットマップが大きすぎてテクスチャにアップロードできないようにするandroid

元の画像をギャラリー形式で全画面表示する必要があります。簡単に言えば、それは完璧に機能し、その画像を元のソースでフルスクリーンで表示しようとすると、表示できなくなります。ほとんどの場合、画像の解像度が2000を超えると、エラーが表示されますビットマップが大きすぎて、Androidのテクスチャにアップロードできません

これを防止したいのですが、Googleで検索していますが、これに関する回答がありません。

15
Pratik

私は同じ問題に遭遇し、この問題のワンライナーソリューションを思いついた here

Picasso.with(context).load(new File(path/to/File)).fit().centerCrop().into(imageView);
17
Phileo99

画像が1Mピクセルより大きいかどうかを確認するif else関数を作成したところ、サンプルコードは次のようになります。

public void onActivityResult(int requestCode, int resultCode, Intent data) {

   if (resultCode == RESULT_OK) {

     if (requestCode == SELECT_PICTURE) {

        Uri selectedImageUri = data.getData();
        selectedImagePath = getPath(selectedImageUri);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;

        Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath);
        int height = bitmap.getHeight(), width = bitmap.getWidth();

        if (height > 1280 && width > 960){
            Bitmap imgbitmap = BitmapFactory.decodeFile(selectedImagePath, options);
            imageView.setImageBitmap(imgbitmap);

            System.out.println("Need to resize");

        }else {
            imageView.setImageBitmap(bitmap);
            System.out.println("WORKS");
        }
8
Gilbert92

Googleはその方法をトレーニングしました。サンプルをダウンロード ビットマップを効率的に表示

ImageResizerクラスを見てください。 ImageResizer.decodeSampledBitmapFrom *は、このメソッドを使用して縮小画像を取得します。

4
Minas

これは、サイズ3120x4196の解像度の画像を4096x4096の解像度の画像ビューに合わせるという私の問題を修正するために使用したコードです。ここで、ImageViewIdはメインレイアウトで作成された画像ビューのIDで、ImageFileLocationはサイズ変更される画像のパスです。

        ImageView imageView=(ImageView)findViewById(R.id.ImageViewId);          
        Bitmap d=BitmapFactory.decodeFile(ImageFileLcation);
        int newHeight = (int) ( d.getHeight() * (512.0 / d.getWidth()) );
        Bitmap putImage = Bitmap.createScaledBitmap(d, 512, newHeight, true);
        imageView.setImageBitmap(putImage);
3
Kartik Nigam

画像全体をロードする必要はありません。画像が大きすぎて、おそらくお使いの携帯電話は完全なビットマップピクセルを表示できません。まず、デバイスの画面サイズに合わせてスケーリングする必要があります。これは私が見つけた最良の方法であり、かなりうまくいきます: Android:大きなビットマップファイルをスケーリングされた出力ファイルにサイズ変更する

1
shem

外部ライブラリを使用せずにこれを行う方法を見つけました:

_if (bitmap.getHeight() > GL10.GL_MAX_TEXTURE_SIZE) {

    // this is the case when the bitmap fails to load
    float aspect_ratio = ((float)bitmap.getHeight())/((float)bitmap.getWidth());
    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                    (int) ((GL10.GL_MAX_TEXTURE_SIZE*0.9)*aspect_ratio),
                    (int) (GL10.GL_MAX_TEXTURE_SIZE*0.9));
    imageView.setImageBitmap(scaledBitmap);
}
else{

    // for bitmaps with dimensions that lie within the limits, load the image normally
    if (Build.VERSION.SDK_INT >= 16) {
        BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap);
        imageView.setBackground(ob);
    } else {
        imageView.setImageBitmap(bitmap);
    }
}
_

基本的に、画像の最大サイズはシステムによって課される制限です。上記のアプローチは、この制限を超えるビットマップを正しくサイズ変更します。ただし、画像全体の一部のみが読み込まれます。表示される領域を変更するには、createBitmap()メソッドのxおよびyパラメーターを変更します。

このアプローチは、プロのカメラで撮った写真を含む、あらゆるサイズのビットマップを処理します。

参照:

Android Universal Image Loader

1
Y.S