【问题标题】:Bitmap Too Large to be uploaded in a Texture (Differences between Emulator and Actual Device)位图太大,无法在纹理中上传(模拟器和实际设备之间的差异)
【发布时间】:2013-10-26 14:07:01
【问题描述】:

我创建了这个演示,它从图库中检索图像并将其显示在 ImageView 中(重新采样图像本身之后)https://github.com/svappdroid/BitmapDemo

在 Galaxy Nexus 上,我无法打开尺寸为 4307x2894 的图像,logcat 显示“位图太大,无法在纹理中上传”;奇怪的是,模拟 Galaxy Nexus 我可以打开相同的图像;为什么会出现这种行为以及如何在实际设备上解决?

活动代码为:


    package com.example.bitmapdemo;

    import java.io.FileNotFoundException;

    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.net.Uri;
    import android.os.Bundle;
    import android.util.DisplayMetrics;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.Toast;

    public class MainActivity extends Activity {
        public ImageView canvasImageView;
        public Button selectImageButton;
        public LinearLayout ll;
        public Bitmap image;

        //REQUEST CODE
        public static final int SELECT_IMAGE_REQ_CODE = 1;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ll = (LinearLayout) this.findViewById(R.id.LinearLayout1);
            canvasImageView = (ImageView) this.findViewById(R.id.canvasImageView);
            selectImageButton = (Button) this.findViewById(R.id.selectImageButton);

            selectImageButton.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v)
                {
                    Intent selectImageIntent = new Intent(Intent.ACTION_GET_CONTENT);
                    selectImageIntent.setType("image/*");
                    startActivityForResult(selectImageIntent, SELECT_IMAGE_REQ_CODE);
                }
            });
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if(requestCode == SELECT_IMAGE_REQ_CODE)
            {
                switch(resultCode)
                {
                    case(RESULT_OK):
                        Uri imageUri = data.getData();

                        try
                        {
                            int inSampleSize = calculateInSampleSize(imageUri);
                            image = resampleImage(imageUri, inSampleSize);
                            canvasImageView.setImageBitmap(image);

                        }
                        catch(FileNotFoundException e){}
                }
            }
        }

        public int calculateInSampleSize(Uri imageUri) throws FileNotFoundException
        {
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri), null, opts);
            int imageWidth = opts.outWidth;
            int imageHeight = opts.outHeight;
            int inSampleSize = 4;

            //Get display dimension in order to calculate image ratio
            DisplayMetrics displayMetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

            int reqWidth = displayMetrics.widthPixels;
            int reqHeight = displayMetrics.heightPixels;

            if(imageHeight > reqHeight || imageWidth > reqWidth)
            {
                final int heightRatio = Math.round((float)imageHeight / (float) reqHeight);
                final int widthRatio = Math.round((float) imageWidth / (float) reqWidth);

                inSampleSize = heightRatio 

这是布局的 xml 代码:



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/canvasImageView"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/selectImageButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/select_image_button" />

</LinearLayout>

【问题讨论】:

    标签: android bitmap


    【解决方案1】:

    您需要调整要在视图上显示的位图大小。

    Bitmap b = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
    profileImage.setImageBitmap(Bitmap.createScaledBitmap(sourceBitmap, width, height, false));
    

    有一个很好的文档可用于在 adnroid 中处理位图。请参考:http://developer.android.com/training/displaying-bitmaps/index.html

    在 SO 上,您也可以找到许多解释。请参阅下文,这将有所帮助:https://stackoverflow.com/a/10703256/2035885 https://stackoverflow.com/a/4837803/2035885

    【讨论】:

    • 我已经将图像大小调整为代码,但它适用于模拟器而不是实际设备,我无法解释自己的这种行为
    【解决方案2】:

    试试这个代码

    public static Bitmap getImageFromFile(String filePath) {
    
            try {
                File file = new File(filePath);
                if (!file.exists()) {
                    return null;
                }
                // decode image size
                BitmapFactory.Options bitmapTempOption = new BitmapFactory.Options();
                bitmapTempOption.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(new FileInputStream(file), null, bitmapTempOption);
    
                // Find the correct scale value. It should be the power of 2.
                final int REQUIRED_SIZE = 200;
                int width_tmp = bitmapTempOption.outWidth, height_tmp = bitmapTempOption.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 *= 2;
                }
    
                // decode with inSampleSize
                BitmapFactory.Options bitmapOption = new BitmapFactory.Options();
                bitmapOption.inSampleSize = scale;
    
                try {
                    return BitmapFactory.decodeStream(new FileInputStream(file), null, bitmapOption);
                } catch (OutOfMemoryError e) {
                    DebugHelper.printError(e);
                } catch (Error e) {
                    DebugHelper.printError(e);
                }
            } catch (Exception exception) {
                DebugHelper.printException(exception);
            }
            return null;
        }
    

    您也可以使用此代码缩小图像

    ImageView iv  = (ImageView)waypointListView.findViewById(R.id.waypoint_picker_photo);
    Bitmap d = new BitmapDrawable(ctx.getResources() , w.photo.getAbsolutePath()).getBitmap();
    int nh = (int) ( d.getHeight() * (512.0 / d.getWidth()) );
    Bitmap scaled = Bitmap.createScaledBitmap(d, 512, nh, true);
    iv.setImageBitmap(scaled);
    

    【讨论】:

    • 此代码返回与我相同的结果;这是一个非常奇怪的行为,因为在实际的星系 Nexus 上,Logcat 显示“位图太大而无法上传到纹理中(最大 2048x2048)”,但它适用于模拟的 Galaxy Nexus(而且我在两个设备上都使用相同的图像)
    • 好的,您在 下的 AndroidMenifest.xml 中尝试过吗
    • 它只有在我禁用硬件加速时才有效,但我不确定这是一个好习惯
    • 最终 int REQUIRED_SIZE = 70;最后一次试试这个
    • 我的 inSampleSize 值很大(即 16),但仍然有警告
    猜你喜欢
    • 2015-10-18
    • 1970-01-01
    • 2012-05-03
    • 2015-04-05
    • 1970-01-01
    • 2011-03-04
    • 2020-02-07
    • 2013-09-09
    相关资源
    最近更新 更多