安卓:位图大小超过DialogFragment 32位 [英] Android: Bitmap size exceeds 32 bit in DialogFragment

查看:117
本文介绍了安卓:位图大小超过DialogFragment 32位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是不是<一个副本href="http://stackoverflow.com/questions/9370145/mysterious-stacktrace-in-android-developer-console-bitmap-size-exceeds-32bits">Mysterious堆栈跟踪在Android开发者控制台(位图的大小超过32位)

这个问题没有提供code一行并没有答案要么;此外,我得到这个错误,即使我设置位图的大小32×32,它是唯一的位图,所以它不是内存相关的。

我想做什么

  1. 在获得ImageView的大小,
  2. 创建位图
  3. 和借鉴的东西在画布。

Log.e 输出ImageView的大小,因此位图是:

 宽度:272
身高:136
 

会发生什么

下面code运作良好的Nexus4,Nexus7和Desire HD的(CM10),但在模拟器上运行的应用程序给我下图所示的错误(API 8)。

结果

  1. 我已经试过了一半的大小和32×32,这给了同样的错误。

  2. 我在DialogFragment显示ImageView的(ActionBarSherlock / HoloEveryWhere的API 8),也许这是罪魁祸首?

ImageView的:

 &LT; ImageView的
      机器人:ID =@ + ID / imageView1
      机器人:layout_width =match_parent
      机器人:layout_height =1DP
      机器人:layout_marginBottom =12dp
      机器人:layout_marginTop =12dp/&GT;
 

在我的活动:

  IV =(ImageView的)view.findViewById(R.id.imageView1);
ViewTreeObserver VTO = iv.getViewTreeObserver();
vto.addOn preDrawListener(新ViewTreeObserver.On preDrawListener(){
    @覆盖
    在preDraw公共布尔(){
         如果(!waveFor​​mMeasured){
             如果(iv.getMeasuredWidth()→1){
                 宽度= iv.getMeasuredWidth();
                 Log.e(TAG,宽:+宽+身高+宽/ 2);
                 waveBitmap = Bitmap.createBitmap((int)的宽度,(i​​nt)的((int)的宽度/ 2),Config.RGB_565);
                 Log.e(TAG,创建位图);
                 waveCanvas =新的Canvas(waveBitmap);
                 Log.e(TAG,位图设置为画布);
                 。iv.getLayoutParams()高度=宽度/ 2;
                 Log.e(TAG,ImageView的高度变);
                 iv.setImageBitmap(waveBitmap);
                 Log.e(TAG,位图设置为ImageView的);
                 drawWaveFor​​m(真正的);
                 Log.e(TAG,波形绘制);
                 waveFor​​mMeasured = TRUE;
              }
          }
     返回true;
     }
  });
 

什么是错在这里?

  02-21 17:12:48.301:E /鼓手(375):宽:272高:136
02-21 17:12:48.301:E /鼓手(375):位图创建
02-21 17:12:48.301:E /鼓手(375):位图设置为画布
02-21 17:12:48.301:E /鼓手(375):ImageView的高度变化
02-21 17:12:48.301:E /鼓手(375):位图设置为ImageView的
02-21 17:12:48.623:E /鼓手(375):波形绘制
02-21 17:12:48.842:E / AndroidRuntime(375):致命异常:主要
02-21 17:12:48.842:E / AndroidRuntime(375):java.lang.IllegalArgumentException:如果位图的大小超过32位
02-21 17:12:48.842:E / AndroidRuntime(375):在android.graphics.Bitmap.nativeCreate(本机方法)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.graphics.Bitmap.createBitmap(Bitmap.java:477)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.view.View.buildDrawingCache(View.java:6577)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.view.ViewGroup.onAnimationStart(ViewGroup.java:1259)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.view.ViewGroup.drawChild(ViewGroup.java:1505)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.view.View.draw(View.java:6883)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.view.ViewGroup.drawChild(ViewGroup.java:1646)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.view.View.draw(View.java:6986)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.widget.FrameLayout.draw(FrameLayout.java:357)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.widget.ScrollView.draw(ScrollView.java:1409)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.view.ViewGroup.drawChild(ViewGroup.java:1646)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.view.ViewGroup.drawChild(ViewGroup.java:1644)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.view.ViewGroup.drawChild(ViewGroup.java:1644)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.view.View.buildDrawingCache(View.java:6640)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.view.ViewGroup.onAnimationStart(ViewGroup.java:1259)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.view.ViewGroup.drawChild(ViewGroup.java:1505)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.view.View.draw(View.java:6883)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.widget.FrameLayout.draw(FrameLayout.java:357)
02-21 17:12:48.842:E / AndroidRuntime(375):在com.android.internal.policy.impl.PhoneWindow $ DecorView.draw(PhoneWindow.java:1862)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.view.ViewRoot.draw(ViewRoot.java:1522)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.os.Handler.dispatchMessage(Handler.java:99)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.os.Looper.loop(Looper.java:123)
02-21 17:12:48.842:E / AndroidRuntime(375):在android.app.ActivityThread.main(ActivityThread.java:3683)
02-21 17:12:48.842:E / AndroidRuntime(375):在java.lang.reflect.Method.invokeNative(本机方法)
02-21 17:12:48.842:E / AndroidRuntime(375):在java.lang.reflect.Method.invoke(Method.java:507)
02-21 17:12:48.842:E / AndroidRuntime(375):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
02-21 17:12:48.842:E / AndroidRuntime(375):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-21 17:12:48.842:E / AndroidRuntime(375):在dalvik.system.NativeStart.main(本机方法)
 

解决方案

位图有大小限制取决于设备。的大小通常是屏幕本身的大小。有在堆栈溢出几个问题,解决这个问题,比如<一个href="http://stackoverflow.com/questions/2928002/outofmemoryerror-bitmap-size-exceeds-vm-budget-android">OutOfMemoryError:位图的大小超过VM预算: - 安卓或<一href="http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object">Android:奇怪的内存不足的问题,同时加载图像到一个位图对象。如果你需要一个非常大的图像,你可以简单地把它分解成更易于管理的大小,并将其放置在多个 ImageViews 。我创建了一个应用程序,数个月前,这是关于串起15幅图像无缝地一起大,滚动的图像 - 验证,这是一个很好的方式去

在最后要注意,位图很难与直接工作,因为它们会导致在Android上的内存问题。看看 BitmapFactory.Options ,并永远记住以抵消和循环未使用的位图。

修改

这也可能是与修改查看内容上preDraw()。这个帖子:我什么时候可以测量一个视图讨论如何编辑在这种方法的视图大小会引起在preDraw()来不断被调用,而执行这些操作与 onGlobalLayoutListener 将产生预期的结果。

This is not a duplicate of Mysterious stacktrace in Android developer console (bitmap size exceeds 32bits)

That question didn't provide a single line of code and there's no answer either; besides, I get this error even if I set the Bitmap size to 32x32, it is the only Bitmap, so it is not memory related.

What I want to do

  1. Get the size of an ImageView,
  2. create a BitMap
  3. and draw something on a Canvas.

The Log.e output for the size of the ImageView and therefore Bitmap is:

Width: 272
Height: 136

What happens

The below code works well on Nexus4, Nexus7 and Desire HD (CM10), but running the app on the emulator gives me the error shown below (API 8).

Findings

  1. I've tried it with half the size and 32x32, which gives the same error.

  2. I am displaying the ImageView in a DialogFragment (ActionBarSherlock/HoloEveryWhere for API 8), maybe that's the culprit?

ImageView:

<ImageView
      android:id="@+id/imageView1"
      android:layout_width="match_parent"
      android:layout_height="1dp"
      android:layout_marginBottom="12dp"
      android:layout_marginTop="12dp" />

In my activity:

iv = (ImageView) view.findViewById(R.id.imageView1);
ViewTreeObserver vto = iv.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
    @Override
    public boolean onPreDraw() {
         if (!waveFormMeasured) {
             if (iv.getMeasuredWidth() > 1) {
                 width = iv.getMeasuredWidth();
                 Log.e(TAG, "Width: " + width + " Height: " + width / 2);
                 waveBitmap = Bitmap.createBitmap((int) width, (int) ((int) width / 2), Config.RGB_565);
                 Log.e(TAG, "Bitmap created");
                 waveCanvas = new Canvas(waveBitmap);
                 Log.e(TAG, "Bitmap set to Canvas");
                 iv.getLayoutParams().height = width / 2;
                 Log.e(TAG, "ImageView Height changed");
                 iv.setImageBitmap(waveBitmap);
                 Log.e(TAG, "Bitmap set to ImageView");
                 drawWaveForm(true);
                 Log.e(TAG, "WaveForm drawn");
                 waveFormMeasured = true;
              }
          }
     return true;
     }
  });

What is wrong here ?

02-21 17:12:48.301: E/Drummers(375): Width: 272 Height: 136
02-21 17:12:48.301: E/Drummers(375): Bitmap created
02-21 17:12:48.301: E/Drummers(375): Bitmap set to Canvas
02-21 17:12:48.301: E/Drummers(375): ImageView Height changed
02-21 17:12:48.301: E/Drummers(375): Bitmap set to ImageView
02-21 17:12:48.623: E/Drummers(375): WaveForm drawn
02-21 17:12:48.842: E/AndroidRuntime(375): FATAL EXCEPTION: main
02-21 17:12:48.842: E/AndroidRuntime(375): java.lang.IllegalArgumentException: bitmap size exceeds 32bits
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.graphics.Bitmap.nativeCreate(Native Method)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.View.buildDrawingCache(View.java:6577)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.onAnimationStart(ViewGroup.java:1259)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.drawChild(ViewGroup.java:1505)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.View.draw(View.java:6883)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.View.draw(View.java:6986)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.widget.ScrollView.draw(ScrollView.java:1409)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.View.buildDrawingCache(View.java:6640)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.onAnimationStart(ViewGroup.java:1259)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.drawChild(ViewGroup.java:1505)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.View.draw(View.java:6883)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
02-21 17:12:48.842: E/AndroidRuntime(375):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewRoot.draw(ViewRoot.java:1522)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.os.Looper.loop(Looper.java:123)
02-21 17:12:48.842: E/AndroidRuntime(375):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-21 17:12:48.842: E/AndroidRuntime(375):  at java.lang.reflect.Method.invokeNative(Native Method)
02-21 17:12:48.842: E/AndroidRuntime(375):  at java.lang.reflect.Method.invoke(Method.java:507)
02-21 17:12:48.842: E/AndroidRuntime(375):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-21 17:12:48.842: E/AndroidRuntime(375):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-21 17:12:48.842: E/AndroidRuntime(375):  at dalvik.system.NativeStart.main(Native Method)

解决方案

bitmaps have a size limit that depends on the device. The size is generally the size of the screen itself. There are several questions on stack overflow that address this issue, such as OutOfMemoryError: bitmap size exceeds VM budget :- Android or Android: Strange out of memory issue while loading an image to a Bitmap object . If you need a very large image, you can simply break it down into more manageable sizes, and place them in multiple ImageViews. I created an app several months ago with a large, scrollable image that was about 15 images strung together seamlessly - validation that this is a good way to go.

On a final note, bitmaps are difficult to work with directly, due to the memory issues they cause on Android. Look into BitmapFactory.Options, and always remember to nullify and recycle unused bitmaps.

Edit

It may also have something to do with modifying the contents of the View in onPreDraw(). This post: When Can I First Measure a View? discusses how editing the size of a View in this method will cause onPreDraw() to continually be called, whereas performing these operations with an onGlobalLayoutListener will give the expected results.

这篇关于安卓:位图大小超过DialogFragment 32位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆