如何在从资源创建的位图上超级强制转换文本并保存为JPG?

时间:2012-01-14 05:35:36

标签: android graphics bitmap

我正在开发一个应用,用户在文本视图中输入内容,然后点击按钮。此按钮启动一个线程,该线程将文本强加在资源文件中的位图上,然后将此新位图保存到SD卡并启动允许用户发送/通过电子邮件发送此图像的意图。我已经编写了这段代码,它在我的银河系中运行,但它不能在Android模拟器上运行。此应用程序使用8级API,旨在在2.2平台上运行。我试图运行它自己的线程,也作为asynctask运行没有运气。我还阅读了许多stackoverflow问题,并遵循他们关于创建位图的建议,而不会溢出模拟器堆大小。我已经尝试将我的变量实例化为字段,也在方法本身内。这是我的代码:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("INFO","At 2###########");
setContentView(R.layout.blank);
Thread thread = new Thread(this);
thread.start();
}

public void run() {
BitmapFactory.Options factoryOptions = new BitmapFactory.Options();
factoryOptions.inPurgeable = true;
factoryOptions.inInputShareable = true;
Bitmap tempCBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.c6,factoryOptions);
cBitmap = tempCBitmap.copy(Bitmap.Config.ARGB_8888, true);
PrepareLines();
Bitmap dBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dummy);
Bitmap textLayer = Bitmap.createScaledBitmap(dBitmap, w, h, false);
Bitmap workLayer = Bitmap.createScaledBitmap(dBitmap, w, h, false);
canvas = new Canvas(textLayer);
canvas.drawText(lineOne, xCenter, yCenter, paint);
canvas.drawText(lineTwo, xCenter, yCenter+20, paint);
canvas.drawText(lineThree, xCenter, yCenter+40, paint);

Camera mCamera = new Camera();
Matrix mMatrix = new Matrix();
mCamera.save();
mCamera.rotateY(yDegreeRotate+8);
mCamera.getMatrix(mMatrix);
mCamera.restore();

mMatrix.preTranslate(-xCenter, (-yCenter)-verticalOffset);
mMatrix.postTranslate(xCenter, yCenter+verticalOffset);
Log.i("INFO","At before first draw ###########");


canvas.setBitmap(workLayer);
canvas.drawBitmap(Bitmap.createBitmap(textLayer,0,0,xCenter,h), mMatrix, paint);

canvas.setBitmap(cBitmap);
canvas.drawBitmap(workLayer, new Matrix(), paint);

mMatrix = new Matrix();
workLayer = Bitmap.createScaledBitmap(dBitmap, w, h, false);
dBitmap = null;
System.gc();

mCamera.rotateY(-yDegreeRotate);
mCamera.getMatrix(mMatrix);
// WIN DEATH HERE
mCamera.restore();
// WIN DEATH HERE;
mMatrix.preTranslate(0 , (-yCenter)-verticalOffset);
mMatrix.postTranslate(xCenter, (yCenter)+verticalOffset);
Log.i("INFO","At before second draw ###########");



canvas.setBitmap(workLayer);
canvas.drawBitmap(Bitmap.createBitmap(textLayer,xCenter,0,w-xCenter,h), mMatrix, paint);

canvas.setBitmap(cBitmap);
canvas.drawBitmap(workLayer, new Matrix(), paint);

Log.i("INFO","At 20, string measures: "+paint.measureText(message));
canvas = null;
workLayer = null;
textLayer = null;
System.gc();
makeFileAndIntent();
}

这是我最近的logcat输出

01-14 18:00:43.453: D/AndroidRuntime(388): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
01-14 18:00:43.453: D/AndroidRuntime(388): CheckJNI is ON
01-14 18:00:43.543: D/AndroidRuntime(388): --- registering native functions ---
01-14 18:00:44.063: D/dalvikvm(255): GC_EXPLICIT freed 507 objects / 28896 bytes in 126ms
01-14 18:00:44.173: D/PackageParser(59): Scanning package: /data/app/vmdl47264.tmp
01-14 18:00:44.173: W/PackageParser(59): No actions in intent filter at /data/app/vmdl47264.tmp Binary XML file line #30
01-14 18:00:44.443: I/PackageManager(59): Removing non-system package:com.gigabites.fortune
01-14 18:00:44.443: I/ActivityManager(59): Force stopping package com.gigabites.fortune uid=10040
01-14 18:00:44.543: D/PackageManager(59): Scanning package com.gigabites.fortune
01-14 18:00:44.543: I/PackageManager(59): Package com.gigabites.fortune codePath changed from /data/app/com.gigabites.fortune-2.apk to /data/app/com.gigabites.fortune-1.apk; Retaining data and using new
01-14 18:00:44.543: I/PackageManager(59): /data/app/com.gigabites.fortune-1.apk changed; unpacking
01-14 18:00:44.553: D/installd(35): DexInv: --- BEGIN '/data/app/com.gigabites.fortune-1.apk' ---
01-14 18:00:44.803: D/dalvikvm(395): DexOpt: load 37ms, verify 132ms, opt 3ms
01-14 18:00:44.813: D/installd(35): DexInv: --- END '/data/app/com.gigabites.fortune-1.apk' (success) ---
01-14 18:00:44.813: W/PackageManager(59): Code path for pkg : com.gigabites.fortune changing from /data/app/com.gigabites.fortune-2.apk to /data/app/com.gigabites.fortune-1.apk
01-14 18:00:44.823: W/PackageManager(59): Resource path for pkg : com.gigabites.fortune changing from /data/app/com.gigabites.fortune-2.apk to /data/app/com.gigabites.fortune-1.apk
01-14 18:00:44.823: D/PackageManager(59):   Activities: com.gigabites.fortune.FortuneCookieActivity com.gigabites.fortune.NewCookieActivity com.gigabites.fortune.BrowseStockActivity com.gigabites.fortune.BrowseSavedActivity com.gigabites.fortune.SendCookieActivity com.gigabites.fortune.BuildActivity
01-14 18:00:44.833: I/ActivityManager(59): Force stopping package com.gigabites.fortune uid=10040
01-14 18:00:44.913: I/installd(35): move /data/dalvik-cache/data@app@com.gigabites.fortune-1.apk@classes.dex -> /data/dalvik-cache/data@app@com.gigabites.fortune-1.apk@classes.dex
01-14 18:00:44.913: D/PackageManager(59): New package installed in /data/app/com.gigabites.fortune-1.apk
01-14 18:00:45.013: I/ActivityManager(59): Force stopping package com.gigabites.fortune uid=10040
01-14 18:00:45.213: D/dalvikvm(59): GC_EXPLICIT freed 8993 objects / 895112 bytes in 187ms
01-14 18:00:45.253: D/dalvikvm(116): GC_EXPLICIT freed 1237 objects / 68112 bytes in 69ms
01-14 18:00:45.383: W/RecognitionManagerService(59): no available voice recognition services found
01-14 18:00:45.503: D/dalvikvm(143): GC_EXPLICIT freed 2329 objects / 127024 bytes in 241ms
01-14 18:00:45.604: D/dalvikvm(59): GC_EXPLICIT freed 5971 objects / 316560 bytes in 76ms
01-14 18:00:45.613: I/installd(35): unlink /data/dalvik-cache/data@app@com.gigabites.fortune-2.apk@classes.dex
01-14 18:00:45.613: D/AndroidRuntime(388): Shutting down VM
01-14 18:00:45.623: D/dalvikvm(388): Debugger has detached; object registry had 1 entries
01-14 18:00:46.043: D/AndroidRuntime(401): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
01-14 18:00:46.043: D/AndroidRuntime(401): CheckJNI is ON
01-14 18:00:46.133: D/AndroidRuntime(401): --- registering native functions ---
01-14 18:00:46.543: I/ActivityManager(59): Force stopping package com.gigabites.fortune uid=10040
01-14 18:00:46.543: I/ActivityManager(59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.gigabites.fortune/.BuildActivity }
01-14 18:00:46.563: I/ActivityManager(59): Start proc com.gigabites.fortune for activity com.gigabites.fortune/.BuildActivity: pid=407 uid=10040 gids={1015}
01-14 18:00:46.574: W/WindowManager(59): HistoryRecord{450598a8 com.gigabites.fortune/.BuildActivity} failed creating starting window
01-14 18:00:46.574: W/WindowManager(59): android.view.InflateException: Binary XML file line #24: Error inflating class <unknown>
01-14 18:00:46.574: W/WindowManager(59):    at android.view.LayoutInflater.createView(LayoutInflater.java:513)
01-14 18:00:46.574: W/WindowManager(59):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
01-14 18:00:46.574: W/WindowManager(59):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
01-14 18:00:46.574: W/WindowManager(59):    at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
01-14 18:00:46.574: W/WindowManager(59):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
01-14 18:00:46.574: W/WindowManager(59):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
01-14 18:00:46.574: W/WindowManager(59):    at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:2165)
01-14 18:00:46.574: W/WindowManager(59):    at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:2220)
01-14 18:00:46.574: W/WindowManager(59):    at com.android.internal.policy.impl.PhoneWindow.getDecorView(PhoneWindow.java:1407)
01-14 18:00:46.574: W/WindowManager(59):    at com.android.internal.policy.impl.PhoneWindowManager.addStartingWindow(PhoneWindowManager.java:894)
01-14 18:00:46.574: W/WindowManager(59):    at com.android.server.WindowManagerService$H.handleMessage(WindowManagerService.java:9007)
01-14 18:00:46.574: W/WindowManager(59):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 18:00:46.574: W/WindowManager(59):    at android.os.Looper.loop(Looper.java:123)
01-14 18:00:46.574: W/WindowManager(59):    at com.android.server.WindowManagerService$WMThread.run(WindowManagerService.java:570)
01-14 18:00:46.574: W/WindowManager(59): Caused by: java.lang.reflect.InvocationTargetException
01-14 18:00:46.574: W/WindowManager(59):    at android.widget.FrameLayout.<init>(FrameLayout.java:79)
01-14 18:00:46.574: W/WindowManager(59):    at java.lang.reflect.Constructor.constructNative(Native Method)
01-14 18:00:46.574: W/WindowManager(59):    at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
01-14 18:00:46.574: W/WindowManager(59):    at android.view.LayoutInflater.createView(LayoutInflater.java:500)
01-14 18:00:46.574: W/WindowManager(59):    ... 13 more
01-14 18:00:46.574: W/WindowManager(59): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x1010059 a=-1}
01-14 18:00:46.574: W/WindowManager(59):    at android.content.res.Resources.loadDrawable(Resources.java:1681)
01-14 18:00:46.574: W/WindowManager(59):    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
01-14 18:00:46.574: W/WindowManager(59):    at android.widget.FrameLayout.<init>(FrameLayout.java:91)
01-14 18:00:46.574: W/WindowManager(59):    ... 17 more
01-14 18:00:46.574: D/AndroidRuntime(401): Shutting down VM
01-14 18:00:46.583: D/dalvikvm(401): Debugger has detached; object registry had 1 entries
01-14 18:00:46.744: W/ActivityThread(407): Application com.gigabites.fortune is waiting for the debugger on port 8100...
01-14 18:00:46.783: I/System.out(407): Sending WAIT chunk
01-14 18:00:46.783: I/dalvikvm(407): Debugger is active
01-14 18:00:46.983: I/System.out(407): Debugger has connected
01-14 18:00:46.983: I/System.out(407): waiting for debugger to settle...
01-14 18:00:47.183: I/System.out(407): waiting for debugger to settle...
01-14 18:00:47.383: I/System.out(407): waiting for debugger to settle...
01-14 18:00:47.583: I/System.out(407): waiting for debugger to settle...
01-14 18:00:47.783: I/System.out(407): waiting for debugger to settle...
01-14 18:00:47.983: I/System.out(407): waiting for debugger to settle...
01-14 18:00:48.183: I/System.out(407): waiting for debugger to settle...
01-14 18:00:48.393: I/System.out(407): debugger has settled (1335)
01-14 18:00:50.373: I/INFO(407): At 2###########
01-14 18:00:50.433: I/INFO(407): At 1###########
01-14 18:00:50.663: D/dalvikvm(407): GC_EXTERNAL_ALLOC freed 725 objects / 54320 bytes in 39ms
01-14 18:00:51.063: I/ActivityManager(59): Displayed activity com.gigabites.fortune/.BuildActivity: 4505 ms (total 273315 ms)
01-14 18:00:51.103: I/INFO(407): At 2###########
01-14 18:00:51.103: I/INFO(407): At 20, string measures: 653.0
01-14 18:00:51.164: D/dalvikvm(407): GC_EXTERNAL_ALLOC freed 425 objects / 18528 bytes in 34ms
01-14 18:00:51.223: D/dalvikvm(407): GC_EXTERNAL_ALLOC freed 47 objects / 1776 bytes in 31ms
01-14 18:00:51.293: I/INFO(407): At before first draw ###########
01-14 18:00:51.373: D/dalvikvm(407): GC_EXTERNAL_ALLOC freed 15 objects / 680 bytes in 29ms
01-14 18:00:51.433: D/dalvikvm(407): GC_EXPLICIT freed 12 objects / 456 bytes in 30ms
01-14 18:00:51.576: I/DEBUG(31): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
01-14 18:00:51.576: I/DEBUG(31): Build fingerprint: 'generic/google_sdk/generic/:2.2/FRF91/43546:eng/test-keys'
01-14 18:00:51.583: I/DEBUG(31): pid: 407, tid: 414  >>> com.gigabites.fortune <<<
01-14 18:00:51.583: I/DEBUG(31): signal 11 (SIGSEGV), fault addr deadbaad
01-14 18:00:51.583: I/DEBUG(31):  r0 00000000  r1 0000000c  r2 00000027  r3 00000000
01-14 18:00:51.583: I/DEBUG(31):  r4 00000000  r5 deadbaad  r6 00001728  r7 00000000
01-14 18:00:51.583: I/DEBUG(31):  r8 46d2ed10  r9 4185ff74  10 00233e40  fp 4185ff70
01-14 18:00:51.583: I/DEBUG(31):  ip ffffffff  sp 46d2ec80  lr afd154c5  pc afd11dc4  cpsr 40000030
01-14 18:00:51.643: I/DEBUG(31):          #00  pc 00011dc4  /system/lib/libc.so
01-14 18:00:51.643: I/DEBUG(31):          #01  pc 0000be1c  /system/lib/libc.so
01-14 18:00:51.643: I/DEBUG(31): code around pc:
01-14 18:00:51.643: I/DEBUG(31): afd11da4 1c2bd00b 2d00682d e026d1fb 2b0068db 
01-14 18:00:51.643: I/DEBUG(31): afd11db4 4e17d003 51a02001 4d164798 24002227 
01-14 18:00:51.643: I/DEBUG(31): afd11dc4 f7fb702a 2106ee14 ef10f7fc 05592380 
01-14 18:00:51.643: I/DEBUG(31): afd11dd4 6091aa01 1c116054 94012006 eab6f7fc 
01-14 18:00:51.643: I/DEBUG(31): afd11de4 2200a905 f7fc2002 f7fbeac2 2106ee00 
01-14 18:00:51.653: I/DEBUG(31): code around lr:
01-14 18:00:51.653: I/DEBUG(31): afd154a4 b0834a0d 589c447b 26009001 686768a5 
01-14 18:00:51.653: I/DEBUG(31): afd154b4 220ce008 2b005eab 1c28d003 47889901 
01-14 18:00:51.653: I/DEBUG(31): afd154c4 35544306 d5f43f01 2c006824 b003d1ee 
01-14 18:00:51.653: I/DEBUG(31): afd154d4 bdf01c30 0002ae7c 000000d4 1c0fb5f0 
01-14 18:00:51.653: I/DEBUG(31): afd154e4 43551c3d a904b087 1c16ac01 604d9004 
01-14 18:00:51.653: I/DEBUG(31): stack:
01-14 18:00:51.653: I/DEBUG(31):     46d2ec40  00000015  
01-14 18:00:51.653: I/DEBUG(31):     46d2ec44  afd1453b  /system/lib/libc.so
01-14 18:00:51.653: I/DEBUG(31):     46d2ec48  afd405a0  /system/lib/libc.so
01-14 18:00:51.653: I/DEBUG(31):     46d2ec4c  afd4054c  /system/lib/libc.so
01-14 18:00:51.653: I/DEBUG(31):     46d2ec50  00000000  
01-14 18:00:51.653: I/DEBUG(31):     46d2ec54  afd154c5  /system/lib/libc.so
01-14 18:00:51.653: I/DEBUG(31):     46d2ec58  00000000  
01-14 18:00:51.653: I/DEBUG(31):     46d2ec5c  afd1450d  /system/lib/libc.so
01-14 18:00:51.653: I/DEBUG(31):     46d2ec60  afd41724  /system/lib/libc.so
01-14 18:00:51.653: I/DEBUG(31):     46d2ec64  afd40328  /system/lib/libc.so
01-14 18:00:51.653: I/DEBUG(31):     46d2ec68  00000000  
01-14 18:00:51.653: I/DEBUG(31):     46d2ec6c  00001728  
01-14 18:00:51.653: I/DEBUG(31):     46d2ec70  00000000  
01-14 18:00:51.653: I/DEBUG(31):     46d2ec74  afd147ab  /system/lib/libc.so
01-14 18:00:51.653: I/DEBUG(31):     46d2ec78  df002777  
01-14 18:00:51.663: I/DEBUG(31):     46d2ec7c  e3a070ad  
01-14 18:00:51.663: I/DEBUG(31): #00 46d2ec80  8086caa4  /system/lib/libdvm.so
01-14 18:00:51.663: I/DEBUG(31):     46d2ec84  80870eea  /system/lib/libdvm.so
01-14 18:00:51.663: I/DEBUG(31):     46d2ec88  afd418dc  /system/lib/libc.so
01-14 18:00:51.663: I/DEBUG(31):     46d2ec8c  afd10510  /system/lib/libc.so
01-14 18:00:51.663: I/DEBUG(31):     46d2ec90  afd40328  /system/lib/libc.so
01-14 18:00:51.663: I/DEBUG(31):     46d2ec94  fffffbdf  
01-14 18:00:51.663: I/DEBUG(31):     46d2ec98  afd40328  /system/lib/libc.so
01-14 18:00:51.663: I/DEBUG(31):     46d2ec9c  afd41724  /system/lib/libc.so
01-14 18:00:51.663: I/DEBUG(31):     46d2eca0  0000a000  [heap]
01-14 18:00:51.663: I/DEBUG(31):     46d2eca4  afd0be21  /system/lib/libc.so
01-14 18:00:51.663: I/DEBUG(31): #01 46d2eca8  afd40328  /system/lib/libc.so
01-14 18:00:51.663: I/DEBUG(31):     46d2ecac  afd0be21  /system/lib/libc.so
01-14 18:00:51.663: I/DEBUG(31):     46d2ecb0  418cb358  /dev/ashmem/dalvik-LinearAlloc (deleted)
01-14 18:00:51.663: I/DEBUG(31):     46d2ecb4  80846dad  /system/lib/libdvm.so
01-14 18:00:51.663: I/DEBUG(31):     46d2ecb8  0011cda0  [heap]
01-14 18:00:51.663: I/DEBUG(31):     46d2ecbc  00118b68  [heap]
01-14 18:00:51.663: I/DEBUG(31):     46d2ecc0  44f461e0  /dev/ashmem/mspace/dalvik-heap/2 (deleted)
01-14 18:00:51.663: I/DEBUG(31):     46d2ecc4  8083d9b9  /system/lib/libdvm.so
01-14 18:00:51.663: I/DEBUG(31):     46d2ecc8  000013fc  
01-14 18:00:51.663: I/DEBUG(31):     46d2eccc  00130eb8  [heap]
01-14 18:00:51.663: I/DEBUG(31):     46d2ecd0  44f461e0  /dev/ashmem/mspace/dalvik-heap/2 (deleted)
01-14 18:00:51.663: I/DEBUG(31):     46d2ecd4  0011cda0  [heap]
01-14 18:00:51.663: I/DEBUG(31):     46d2ecd8  00240c28  [heap]
01-14 18:00:51.663: I/DEBUG(31):     46d2ecdc  42c3d5f6  /data/dalvik-cache/system@framework@framework.jar@classes.dex
01-14 18:00:51.663: I/DEBUG(31):     46d2ece0  4185ff7c  
01-14 18:00:51.663: I/DEBUG(31):     46d2ece4  afd0cd81  /system/lib/libc.so
01-14 18:00:51.663: I/DEBUG(31):     46d2ece8  0011cda0  [heap]
01-14 18:00:51.673: I/DEBUG(31):     46d2ecec  afc008e3  /system/lib/libstdc++.so
01-14 18:00:52.053: I/BootReceiver(59): Copying /data/tombstones/tombstone_03 to DropBox (SYSTEM_TOMBSTONE)
01-14 18:00:52.073: D/Zygote(33): Process 407 terminated by signal (11)
01-14 18:00:52.143: D/dalvikvm(59): GC_FOR_MALLOC freed 2390 objects / 360624 bytes in 80ms
01-14 18:00:52.143: I/WindowManager(59): WIN DEATH: Window{450eb158 com.gigabites.fortune/com.gigabites.fortune.BuildActivity paused=false}
01-14 18:00:52.143: I/ActivityManager(59): Process com.gigabites.fortune (pid 407) has died.
01-14 18:00:52.154: I/UsageStats(59): Unexpected resume of com.android.launcher while already resumed in com.gigabites.fortune
01-14 18:00:52.183: W/InputManagerService(59): Got RemoteException sending setActive(false) notification to pid 407 uid 10040
01-14 18:00:57.414: D/dalvikvm(255): GC_EXPLICIT freed 224 objects / 15056 bytes in 118ms
01-14 18:01:05.203: D/dalvikvm(262): GC_EXPLICIT freed 2061 objects / 147680 bytes in 127ms
01-14 18:01:06.103: D/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol

编辑:添加了logcat并尝试在setContentView()上使用基本XML布局运行,以确保它与我的布局无关。

1 个答案:

答案 0 :(得分:1)

您是否正确设置了模拟器?尝试在eclipse中查看你的avd编辑器并确保你已经设置了SD卡。