android - draw image on canvas and save into sd card -
i tried use following codes to
- draw on canvas
- save canvas on image
problem - when try save image, shows error permission denied.my error log below.
code draw on canvas:
public mydrawview(context c, attributeset attrs) { super(c, attrs); mpath = new path(); mbitmappaint = new paint(paint.dither_flag); mpaint = new paint(); mpaint.setantialias(true); mpaint.setdither(true); mpaint.setcolor(0xff000000); mpaint.setstyle(paint.style.stroke); mpaint.setstrokejoin(paint.join.round); mpaint.setstrokecap(paint.cap.round); mpaint.setstrokewidth(9); } @override protected void onsizechanged(int w, int h, int oldw, int oldh) { super.onsizechanged(w, h, oldw, oldh); mbitmap = bitmap.createbitmap(w, h, bitmap.config.argb_8888); mcanvas = new canvas(mbitmap); } @override protected void ondraw(canvas canvas) { canvas.drawbitmap(mbitmap, 0, 0, mbitmappaint); canvas.drawpath(mpath, mpaint); } private float mx, my; private static final float touch_tolerance = 4; private void touch_start(float x, float y) { mpath.reset(); mpath.moveto(x, y); mx = x; = y; } private void touch_move(float x, float y) { float dx = math.abs(x - mx); float dy = math.abs(y - my); if (dx >= touch_tolerance || dy >= touch_tolerance) { mpath.quadto(mx, my, (x + mx)/2, (y + my)/2); mx = x; = y; } } private void touch_up() { mpath.lineto(mx, my); // commit path our offscreen mcanvas.drawpath(mpath, mpaint); // kill don't double draw mpath.reset(); } @override public boolean ontouchevent(motionevent event) { float x = event.getx(); float y = event.gety(); switch (event.getaction()) { case motionevent.action_down: touch_start(x, y); invalidate(); break; case motionevent.action_move: touch_move(x, y); invalidate(); break; case motionevent.action_up: touch_up(); invalidate(); break; } return true; } public bitmap getbitmap() { this.setdrawingcacheenabled(true); this.builddrawingcache(); bitmap bmp = bitmap.createbitmap(this.getdrawingcache()); this.setdrawingcacheenabled(false); return bmp; } public void clear(){ mbitmap.erasecolor(color.green); invalidate(); system.gc(); } }
second code save canvas image in main activity. since beginner, appreciate advice.
second code mainactivity:
public class mainactivity extends activity { mydrawview mydrawview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mydrawview = (mydrawview) findviewbyid(r.id.draw); button button1 = (button) findviewbyid(r.id.button1); button1.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { file folder = new file(environment .getexternalstoragedirectory().getabsolutepath()+"/folder image"); if (!folder.exists()) { folder.mkdirs(); } file file = new file(folder,"sample.png"); if (!file.exists()) { try { file.createnewfile(); } catch (ioexception e) { e.printstacktrace(); } } fileoutputstream ostream = null; try { ostream = new fileoutputstream(file); system.out.println(ostream); view targetview = mydrawview; bitmap = mydrawview.getbitmap(); bitmap save = bitmap.createbitmap(320, 480, config.argb_8888); paint paint = new paint(); paint.setcolor(color.white); canvas = new canvas(save); now.drawrect(new rect(0, 0, 320, 480), paint); now.drawbitmap(well, new rect(0, 0, well.getwidth(), well.getheight()), new rect(0, 0, 320, 480), null); if (save == null) { system.out.println("null bitmap save\n"); } save.compress(bitmap.compressformat.png, 100, ostream); } catch (nullpointerexception e) { e.printstacktrace(); toast.maketext(getapplicationcontext(), "null error", toast.length_short).show(); } catch (filenotfoundexception e) { e.printstacktrace(); toast.maketext(getapplicationcontext(), "file error", toast.length_short).show(); } catch (ioexception e) { e.printstacktrace(); toast.maketext(getapplicationcontext(), "io error", toast.length_short).show(); } } }); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); return true; } }
my error log is:
12-16 13:12:13.848: w/system.err(2010): java.io.ioexception: open failed: eacces (permission denied) 12-16 13:12:13.848: w/system.err(2010): @ java.io.file.createnewfile(file.java:940) 12-16 13:12:13.848: w/system.err(2010): @ com.example.drawimage.mainactivity$1.onclick(mainactivity.java:48) 12-16 13:12:13.848: w/system.err(2010): @ android.view.view.performclick(view.java:4084) 12-16 13:12:13.848: w/system.err(2010): @ android.view.view$performclick.run(view.java:16966) 12-16 13:12:13.848: w/system.err(2010): @ android.os.handler.handlecallback(handler.java:615) 12-16 13:12:13.848: w/system.err(2010): @ android.os.handler.dispatchmessage(handler.java:92) 12-16 13:12:13.848: w/system.err(2010): @ android.os.looper.loop(looper.java:137) 12-16 13:12:13.848: w/system.err(2010): @ android.app.activitythread.main(activitythread.java:4745) 12-16 13:12:13.848: w/system.err(2010): @ java.lang.reflect.method.invokenative(native method) 12-16 13:12:13.848: w/system.err(2010): @ java.lang.reflect.method.invoke(method.java:511) 12-16 13:12:13.848: w/system.err(2010): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786) 12-16 13:12:13.848: w/system.err(2010): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 12-16 13:12:13.848: w/system.err(2010): @ dalvik.system.nativestart.main(native method) 12-16 13:12:13.848: w/system.err(2010): caused by: libcore.io.errnoexception: open failed: eacces (permission denied) 12-16 13:12:13.848: w/system.err(2010): @ libcore.io.posix.open(native method) 12-16 13:12:13.848: w/system.err(2010): @ libcore.io.blockguardos.open(blockguardos.java:110) 12-16 13:12:13.848: w/system.err(2010): @ java.io.file.createnewfile(file.java:933) 12-16 13:12:13.848: w/system.err(2010): ... 12 more 12-16 13:12:13.848: w/system.err(2010): java.io.filenotfoundexception: /mnt/sdcard/folder image/sample.png: open failed: eacces (permission denied) 12-16 13:12:13.848: w/system.err(2010): @ libcore.io.iobridge.open(iobridge.java:416) 12-16 13:12:13.848: w/system.err(2010): @ java.io.fileoutputstream.<init>(fileoutputstream.java:88) 12-16 13:12:13.848: w/system.err(2010): @ java.io.fileoutputstream.<init>(fileoutputstream.java:73) 12-16 13:12:13.848: w/system.err(2010): @ com.example.drawimage.mainactivity$1.onclick(mainactivity.java:58) 12-16 13:12:13.848: w/system.err(2010): @ android.view.view.performclick(view.java:4084) 12-16 13:12:13.848: w/system.err(2010): @ android.view.view$performclick.run(view.java:16966) 12-16 13:12:13.848: w/system.err(2010): @ android.os.handler.handlecallback(handler.java:615) 12-16 13:12:13.848: w/system.err(2010): @ android.os.handler.dispatchmessage(handler.java:92) 12-16 13:12:13.848: w/system.err(2010): @ android.os.looper.loop(looper.java:137) 12-16 13:12:13.848: w/system.err(2010): @ android.app.activitythread.main(activitythread.java:4745) 12-16 13:12:13.848: w/system.err(2010): @ java.lang.reflect.method.invokenative(native method) 12-16 13:12:13.848: w/system.err(2010): @ java.lang.reflect.method.invoke(method.java:511) 12-16 13:12:13.848: w/system.err(2010): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786) 12-16 13:12:13.848: w/system.err(2010): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 12-16 13:12:13.848: w/system.err(2010): @ dalvik.system.nativestart.main(native method) 12-16 13:12:13.848: w/system.err(2010): caused by: libcore.io.errnoexception: open failed: eacces (permission denied) 12-16 13:12:13.848: w/system.err(2010): @ libcore.io.posix.open(native method) 12-16 13:12:13.848: w/system.err(2010): @ libcore.io.blockguardos.open(blockguardos.java:110) 12-16 13:12:13.848: w/system.err(2010): @ libcore.io.iobridge.open(iobridge.java:400) 12-16 13:12:13.848: w/system.err(2010): ... 14 more
androidmanifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.drawimage" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="21" /> <uses-permission android:name="android.permission.read_external_storage"/> <uses-permission android:name="android.permission.write_external_storage"/> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
you should not call folder "/folder image", remove space should fine.
Comments
Post a Comment