【问题标题】:assignment bitmap to variable error将位图分配给变量错误
【发布时间】:2013-09-04 21:12:19
【问题描述】:

我无法从变量中绘制位图。当我为它加注星标时,我的应用程序崩溃了。

public class krou extends View {


    public static int Width = aktivita.width;
    public static int Height = aktivita.height;



    final Paint mMalovani;
    public static Bitmap mBitmap;
    public static Bitmap rc;


    public void draw(Canvas canvas) {

        Paint p = new Paint();

        p.setColor(Color.RED);

        int ods = (Height - Width) / 2;



        Bitmap imageOne = BitmapFactory.decodeResource(getResources(), R.drawable.ri50_z);
        Bitmap ImageTwo = BitmapFactory.decodeResource(getResources(), R.drawable.ri50);

        rc = ImageTwo;



        Bitmap scaledBitmap = Bitmap.createScaledBitmap(rc, Width, Width, true);

        Rect dstRectForRender = new Rect(0, odstup, Width, Width + ods);
        canvas.drawBitmap(scaledBitmap, null, dstRectForRender, null);

如果我不使用rc 变量,而我简单地使用imageOne 或ImageTwo,一切都很好。

感谢您的建议。

我的 LogCat:

09-01 19:29:16.197: E/dalvikvm-heap(1472): Out of memory on a 5760016-byte allocation.
09-01 19:29:16.197: I/dalvikvm(1472): "main" prio=5 tid=1 RUNNABLE
09-01 19:29:16.197: I/dalvikvm(1472):   | group="main" sCount=0 dsCount=0 obj=0x40a729a0 self=0x2a00bba8
09-01 19:29:16.197: I/dalvikvm(1472):   | sysTid=1472 nice=0 sched=0/0 cgrp=apps handle=1073849308
09-01 19:29:16.207: I/dalvikvm(1472):   | state=R schedstat=( 3712781811 1124880434 230 ) utm=328 stm=43 core=0
09-01 19:29:16.207: I/dalvikvm(1472):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
09-01 19:29:16.207: I/dalvikvm(1472):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
09-01 19:29:16.207: I/dalvikvm(1472):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
09-01 19:29:16.207: I/dalvikvm(1472):   at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:378)
09-01 19:29:16.207: I/dalvikvm(1472):   at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:408)
09-01 19:29:16.207: I/dalvikvm(1472):   at org.vkedco.mobappdev.draw_touch_drive_00001.krouzky.draw(krouzky.java:71)
09-01 19:29:16.217: I/dalvikvm(1472):   at android.view.View.draw(View.java:13596)
09-01 19:29:16.217: I/dalvikvm(1472):   at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
09-01 19:29:16.217: I/dalvikvm(1472):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
09-01 19:29:16.217: I/dalvikvm(1472):   at android.view.View.draw(View.java:13715)
09-01 19:29:16.217: I/dalvikvm(1472):   at android.view.View.draw(View.java:13596)
09-01 19:29:16.217: I/dalvikvm(1472):   at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
09-01 19:29:16.217: I/dalvikvm(1472):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
09-01 19:29:16.217: I/dalvikvm(1472):   at android.view.View.draw(View.java:13594)
09-01 19:29:16.227: I/dalvikvm(1472):   at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
09-01 19:29:16.227: I/dalvikvm(1472):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
09-01 19:29:16.237: I/dalvikvm(1472):   at android.view.View.draw(View.java:13594)
09-01 19:29:16.237: I/dalvikvm(1472):   at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
09-01 19:29:16.237: I/dalvikvm(1472):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
09-01 19:29:16.237: I/dalvikvm(1472):   at android.view.View.draw(View.java:13715)
09-01 19:29:16.237: I/dalvikvm(1472):   at android.widget.FrameLayout.draw(FrameLayout.java:467)
09-01 19:29:16.237: I/dalvikvm(1472):   at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2211)
09-01 19:29:16.237: I/dalvikvm(1472):   at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2281)
09-01 19:29:16.237: I/dalvikvm(1472):   at android.view.ViewRootImpl.draw(ViewRootImpl.java:2177)
09-01 19:29:16.237: I/dalvikvm(1472):   at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2045)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1854)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.view.Choreographer.doCallbacks(Choreographer.java:562)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.view.Choreographer.doFrame(Choreographer.java:532)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.os.Handler.handleCallback(Handler.java:725)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.os.Handler.dispatchMessage(Handler.java:92)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.os.Looper.loop(Looper.java:137)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.app.ActivityThread.main(ActivityThread.java:5041)
09-01 19:29:16.248: I/dalvikvm(1472):   at java.lang.reflect.Method.invokeNative(Native Method)
09-01 19:29:16.248: I/dalvikvm(1472):   at java.lang.reflect.Method.invoke(Method.java:511)
09-01 19:29:16.248: I/dalvikvm(1472):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-01 19:29:16.248: I/dalvikvm(1472):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-01 19:29:16.248: I/dalvikvm(1472):   at dalvik.system.NativeStart.main(Native Method)
09-01 19:29:16.267: D/skia(1472): --- allocation failed for scaled bitmap
09-01 19:29:16.267: D/AndroidRuntime(1472): Shutting down VM
09-01 19:29:16.298: W/dalvikvm(1472): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-01 19:29:16.348: E/AndroidRuntime(1472): FATAL EXCEPTION: main
09-01 19:29:16.348: E/AndroidRuntime(1472): java.lang.OutOfMemoryError
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:378)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:408)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at org.vkedco.mobappdev.draw_touch_drive_00001.krouzky.draw(krouzky.java:71)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.View.draw(View.java:13596)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.View.draw(View.java:13715)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.View.draw(View.java:13596)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.View.draw(View.java:13594)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.View.draw(View.java:13594)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.View.draw(View.java:13715)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.widget.FrameLayout.draw(FrameLayout.java:467)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2211)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2281)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewRootImpl.draw(ViewRootImpl.java:2177)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2045)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1854)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.Choreographer.doFrame(Choreographer.java:532)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.os.Handler.handleCallback(Handler.java:725)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.os.Looper.loop(Looper.java:137)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.app.ActivityThread.main(ActivityThread.java:5041)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at java.lang.reflect.Method.invokeNative(Native Method)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at java.lang.reflect.Method.invoke(Method.java:511)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at dalvik.system.NativeStart.main(Native Method)
09-01 19:29:24.396: I/Process(1472): Sending signal. PID: 1472 SIG: 9

【问题讨论】:

  • 问题是什么?
  • 哪里有问题,为什么我的应用程序崩溃了,如果我使用 rc 变量。
  • 请描述“下来”。你有任何错误吗?什么是ImageTwo:类还是对象?它是在哪里定义的?
  • 对不起,我的意思是关闭。我只有 LogCat 记录,因为 eclipse 没有显示任何错误。
  • I have only logcat record。你的意思是,找出应用程序崩溃原因的最重要和最有用的方法是什么?向我们展示可能会有用! Eclipse 不会向您显示任何错误,否则您将根本无法运行该应用程序。

标签: java android variables bitmap using


【解决方案1】:

正如我之前回答的 - Out of memory while creating bitmaps on device

你得到OutOfMemoryerror 因为你的位图很重他们是over 57MB 用这种方法缩放你所有的位图-

  Bitmap imageOne = decodeSampledBitmapFromResource(getResources(), R.drawable.ri50_z,height,width);//might be your screen hight and width

方法的实现是-

      public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
    int reqWidth, int reqHeight) {

// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}


 public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

    // Calculate ratios of height and width to requested height and width
    final int heightRatio = Math.round((float) height / (float) reqHeight);
    final int widthRatio = Math.round((float) width / (float) reqWidth);

    // Choose the smallest ratio as inSampleSize value, this will guarantee
    // a final image with both dimensions larger than or equal to the
    // requested height and width.
    inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}

return inSampleSize;
}

并且还可以缩放您的位图 src 参考上面的链接-

这将解决您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-02
    • 2018-07-06
    • 1970-01-01
    • 2017-09-23
    • 2014-05-11
    • 2020-09-15
    • 1970-01-01
    相关资源
    最近更新 更多