android - Set ImageView from Bitmap (null pointer exception) -
so app has actiondialog
contains blank canvas interface simple cancel , save buttons. happening best demonstrated in brief illustration
screen layout before
-(textview)-(imageview)-(button)-
then when user presses button actiondialog
pops requesting sign. once sign captured drawing saved. drawing accessed via memory , placed original imageview
bitmap
. ends happening
screen layout after
----------nothing--------------
they disappear , error in logcat
:
05-14 19:06:27.004: e/error(25274): java.io.filenotfoundexception: /storage/emulated/0signature.png: open failed: eacces (permission denied) 05-14 19:06:27.004: e/bitmapfactory(25274): unable decode stream: java.io.filenotfoundexception: /storage/emulated/0signature.png: open failed: enoent (no such file or directory)
java.io.filenotfoundexception: /storage/emulated/0signature.png: open failed: enoent (no such file or directory)
it not crash program though. anyways here code files
action dialog
public class capturesignature extends dialogfragment { sign sign; view view; public dialog oncreatedialog(bundle savedinstancestate) { alertdialog.builder capsig = new alertdialog.builder(getactivity()); capsig.setview(sign = new sign(this.getactivity(), null)) .setmessage(r.string.store_question) .setpositivebutton(r.string.save, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { try { sign.setdrawingcacheenabled(true); sign.getdrawingcache() .compress( bitmap.compressformat.png, 10, new fileoutputstream( new file( getactivity() .getexternalfilesdir( "img"), "signature.png"))); } catch (exception e) { log.e("error ", e.tostring()); } file mysig = new file(getactivity() .getexternalfilesdir("img"), "signature.png"); imageview sig = (imageview) getactivity() .findviewbyid(r.id.sig_image); bitmap bmp = bitmapfactory.decodefile(mysig .getabsolutepath()); sig.setimagebitmap(bmp); } }) .setnegativebutton(r.string.cancel, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { } }); // create dialog object , return return capsig.create(); } }
so i've messed somewhere. if has insight grateful. thanks!
i think either saving wrong or i'm not correctly declaring sign, in call don't give value, drawing cache
not being accessed.
edit have declared in manifest
<uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.read_external_storage" />
2nd edit
showing new code , logcat, error has changed
final edit
thank matt giles , jrowan, driving me insane. works , above code final version.
the problem line:
new file(environment.getexternalstoragedirectory().getpath() + "signature.png"
the call getpath()
returns path doesn't have trailing '/'. hence file path in error message, /storage/emulated/0signature.png
instead of /storage/emulated/0/signature.png
.
it better use application-specific storage, instead of putting files in sdcard root directory. instead of new file(...)
call have now, use:
new file(getactivity().getexternalfilesdir("img"), "signature.png")
getexternalfilesdir(name)
creates folder called "name" that's dedicated application. prevents app cluttering sdcard root directory.
Comments
Post a Comment