将画布保存为图像

时间:2012-12-04 03:38:14

标签: android bitmap save android-canvas

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

这就是我所拥有的:

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)

1 个答案:

答案 0 :(得分:0)

看看这个code希望这就是你想要的。

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

您可以截取您想要的视图。 R.id.CanvasView。 希望这有帮助