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();
}
}
何が悪いのかについて何か提案はありますか?
ありがとう
更新:それが役立つ場合は、私のログキャットを次に示します:
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)