2

我一直在尝试将我的画布保存为 android 中的图像,但我的代码似乎在每次尝试保存时都会继续崩溃。我在互联网上找到了这样做的代码(因为那里似乎有很多),但我似乎无法让它适用于我的代码。

这是我所拥有的:

private File APP_FILE_PATH = new File("/sdcard/newpics");
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    paint.setStyle(Paint.Style.FILL);


         Bitmap toDisk = Bitmap.createBitmap(canvas.getWidth(),canvas.getHeight(),Bitmap.Config.ARGB_8888);
         canvas.setBitmap(toDisk);



         try {
            toDisk.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File(APP_FILE_PATH + "/myAwesomeDrawing.png")));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }            
    }

有什么不好的建议吗?

谢谢

更新:如果有帮助,这是我的 logcat:

12-03 22:39:48.593: E/AndroidRuntime(17637): FATAL EXCEPTION: main

12-03 22:39:48.593: E/AndroidRuntime(17637): java.lang.UnsupportedOperationException

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.HardwareCanvas.setBitmap(HardwareCanvas.java:37)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at com.example.myDraw.Draw_View.onDraw(Draw_View.java:56)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.View.draw(View.java:13707)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.View.getDisplayList(View.java:12645)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.View.getDisplayList(View.java:12689)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.View.getDisplayList(View.java:12583)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.View.getDisplayList(View.java:12689)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.View.getDisplayList(View.java:12583)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.View.getDisplayList(View.java:12689)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.View.getDisplayList(View.java:12583)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.View.getDisplayList(View.java:12689)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.View.getDisplayList(View.java:12583)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.View.getDisplayList(View.java:12689)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.View.getDisplayList(View.java:12583)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.View.getDisplayList(View.java:12689)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.View.getDisplayList(View.java:12583)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.View.getDisplayList(View.java:12689)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1198)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.ViewRootImpl.draw(ViewRootImpl.java:2173)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2045)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1854)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.Choreographer.doFrame(Choreographer.java:532)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.os.Handler.handleCallback(Handler.java:725)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.os.Handler.dispatchMessage(Handler.java:92)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.os.Looper.loop(Looper.java:137)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at android.app.ActivityThread.main(ActivityThread.java:5039)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at java.lang.reflect.Method.invokeNative(Native Method)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at java.lang.reflect.Method.invoke(Method.java:511)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

12-03 22:39:48.593: E/AndroidRuntime(17637):    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

看看这段代码希望这是你想要的。

View content = findViewById(R.id.layoutroot);

您可以截取所需视图的屏幕截图。R.id.CanvasView 。希望这可以帮助

于 2012-12-04T05:59:49.920 回答