Android take Photo and load the aviary SKD -
Android take Photo and load the aviary SKD -
i trying learning aviary sdk , have downloaded sample.
what want take photo using :
intent takepictureintent = new intent(mediastore.action_image_capture); if (takepictureintent.resolveactivity(getpackagemanager()) != null) { startactivityforresult(takepictureintent, request_image_capture); } and load image taken aviary skd: sample says use:
intent newintent = new intent( this, featheractivity.class ); newintent.setdata( uri.parse("content://media/external/images/media/32705") ); newintent.putextra( constants.extra_in_api_key_secret, "your api secret" ); startactivityforresult( newintent, 1 ); but if taken image using intent, dont yet have image uri right ?
really want take photo , load direct photo editing sdk.
here code far, using sample. guys
... public class mainactivity extends activity { private string mapikey; private static final int action_request_gallery = 99; private static final int action_request_feather = 100; private static final int external_storage_unavailable = 1; static final int request_image_capture = 1; ... meditbutton.setonclicklistener( new view.onclicklistener() { @override public void onclick( view v ) { if ( mimageuri != null ) { startfeather( mimageuri ); } } } ); mimagecontainer.setonclicklistener( new view.onclicklistener() { @override public void onclick( view v ) { /* uri uri = pickrandomimage(); if ( uri != null ) { log.d( log_tag, "image uri: " + uri ); loadasync( uri ); } */ // todo add together here load photographic camera intent takepictureintent = new intent(mediastore.action_image_capture); if (takepictureintent.resolveactivity(getpackagemanager()) != null) { startactivityforresult(takepictureintent, request_image_capture); } }// end onclick } ); mimagecontainer.setlongclickable( true ); mimagecontainer.setonlongclicklistener( new onlongclicklistener() { @override public boolean onlongclick( view v ) { if ( mimageuri != null ) { log.d( log_tag, "onlongclick: " + v ); opencontextmenu( v ); homecoming true; } homecoming false; } } ); toast.maketext( this, "launcher: " + getlibraryversion() + ", sdk: " + sdkutils.sdk_version, toast.length_short ) .show(); mgalleryfolder = createfolders(); registerforcontextmenu( mimagecontainer ); // pre-load cds service intent cdsintent = aviaryintent.createcdsinitintent( getbasecontext(), api_secret, null ); startservice( cdsintent ); new apikeyreader().execute(); } @override protected void onresume() { log.i( log_tag, "onresume" ); super.onresume(); if ( getintent() != null ) { handleintent( getintent() ); setintent( new intent() ); } } @override public void oncreatecontextmenu( contextmenu menu, view v, contextmenuinfo menuinfo ) { super.oncreatecontextmenu( menu, v, menuinfo ); menu.setheadertitle( "menu" ); menu.add( 0, 0, 0, "details" ); } @override public boolean oncontextitemselected( menuitem item ) { final int order = item.getorder(); switch ( order ) { case 0: showcurrentimagedetails(); homecoming true; } homecoming super.oncontextitemselected( item ); } private void setapikey( string value ) { log.i( log_tag, "api-key: " + value ); mapikey = value; if( null == value ) { string message = sdkutils.missing_apikey_message; new alertdialog.builder( ).settitle( "api-key missing!" ).setmessage( message ).show(); } } /** * handle incoming {@link intent} */ private void handleintent( intent intent ) { string action = intent.getaction(); if ( null != action ) { if ( intent.action_send.equals( action ) ) { bundle extras = intent.getextras(); if ( extras != null && extras.containskey( intent.extra_stream ) ) { uri uri = (uri) extras.get( intent.extra_stream ); loadasync( uri ); } } else if ( intent.action_view.equals( action ) ) { uri info = intent.getdata(); log.d( log_tag, "data: " + info ); loadasync( info ); } } } /** * load incoming image * * @param uri */ private void loadasync( final uri uri ) { log.i( log_tag, "loadasync: " + uri ); drawable torecycle = mimage.getdrawable(); if ( torecycle != null && torecycle instanceof bitmapdrawable ) { if ( ( (bitmapdrawable) mimage.getdrawable() ).getbitmap() != null ) ( (bitmapdrawable) mimage.getdrawable() ).getbitmap().recycle(); } mimage.setimagedrawable( null ); mimageuri = null; downloadasync task = new downloadasync(); task.execute( uri ); } @override protected void ondestroy() { log.i( log_tag, "ondestroy" ); super.ondestroy(); moutputfilepath = null; } /** * load image details , pass result * {@link imageinfoactivity} activity */ private void showcurrentimagedetails() { if ( null != mimageuri ) { imageinfo info; seek { info = new imageinfo( this, mimageuri ); } grab ( ioexception e ) { e.printstacktrace(); return; } if ( null != info ) { intent intent = new intent( this, imageinfoactivity.class ); intent.putextra( "image-info", info ); startactivity( intent ); } } } /** * delete file without throwing exception * * @param path * @return */ private boolean deletefilenothrow( string path ) { file file; seek { file = new file( path ); } grab ( nullpointerexception e ) { homecoming false; } if ( file.exists() ) { homecoming file.delete(); } homecoming false; } @override public void oncontentchanged() { super.oncontentchanged(); mgallerybutton = (button) findviewbyid( r.id.button1 ); meditbutton = (button) findviewbyid( r.id.button2 ); mimage = ( (imageview) findviewbyid( r.id.image ) ); mimagecontainer = findviewbyid( r.id.image_container ); } @override public boolean oncreateoptionsmenu( menu menu ) { menuinflater inflater = getmenuinflater(); inflater.inflate( r.menu.main_menu, menu ); homecoming true; } @override public boolean onoptionsitemselected( menuitem item ) { intent intent; final int id = item.getitemid(); if ( id == r.id.view_documentation ) { intent = new intent( intent.action_view ); intent.setdata( uri.parse( "http://www.aviary.com/android-documentation" ) ); startactivity( intent ); } else if ( id == r.id.get_sdk ) { intent = new intent( intent.action_view ); intent.setdata( uri.parse( "https://github.com/aviaryinc/mobile-feather-sdk-for-android" ) ); startactivity( intent ); } homecoming super.onoptionsitemselected( item ); } @override protected dialog oncreatedialog( int id ) { dialog dialog = null; switch ( id ) { // external sdcard not mounted! case external_storage_unavailable: dialog = new alertdialog.builder( ).settitle( r.string.external_storage_na_title ) .setmessage( r.string.external_storage_na_message ).create(); break; } homecoming dialog; } @override /** * method called when feather has completed ( ie. user clicked on "done" or exit activity without saving ). <br /> * if user clicked "done" button you'll receive result_ok resultcode, result_canceled otherwise. * * @param requestcode * - code passed startactivityforresult * @param resultcode * - result code of activity launched ( can result_ok or result_canceled ) * @param info * - result info */ public void onactivityresult( int requestcode, int resultcode, intent info ) { if ( resultcode == result_ok ) { switch ( requestcode ) { case action_request_gallery: // user chose image gallery loadasync( data.getdata() ); break; case action_request_feather: boolean changed = true; if( null != info ) { bundle = data.getextras(); if( null != ) { // image changed user? changed = extra.getboolean( constants.extra_out_bitmap_changed ); } } if( !changed ) { log.w( log_tag, "user did not modify image, clicked on 'done' button" ); } // send notification media scanner updatemedia( moutputfilepath ); // update preview result loadasync( data.getdata() ); onsavecompleted( moutputfilepath ); moutputfilepath = null; break; } } else if ( resultcode == result_canceled ) { switch ( requestcode ) { case action_request_feather: // feather cancelled without saving. // need delete entire session if ( null != msessionid ) deletesession( msessionid ); // delete result file, if exists if ( moutputfilepath != null ) { deletefilenothrow( moutputfilepath ); moutputfilepath = null; } break; } } } /** * lo-res process completed, inquire user if wants process hi-res image * * @param filepath * lo-res file name ( in case want delete ) */ private void onsavecompleted( final string filepath ) { if ( msessionid != null ) { onclicklistener yeslistener = new onclicklistener() { @override public void onclick( dialoginterface dialog, int ) { if ( null != msessionid ) { processhd( msessionid ); } msessionid = null; } }; onclicklistener nolistener = new onclicklistener() { @override public void onclick( dialoginterface dialog, int ) { if ( null != msessionid ) { deletesession( msessionid ); } if ( !isfinishing() ) { dialog.dismiss(); } msessionid = null; } }; dialog dialog = new alertdialog.builder( ).settitle( "hires" ) .setmessage( "a low-resolution image created. want save hi-res image too?" ) .setpositivebutton( android.r.string.yes, yeslistener ).setnegativebutton( android.r.string.no, nolistener ) .setcancelable( false ).create(); dialog.show(); } } /** * given uri load bitmap current imageview , resize fit image container size * * @param uri */ @suppresswarnings("deprecation") private boolean setimageuri( final uri uri, final bitmap bitmap ) { log.d( log_tag, "image size: " + bitmap.getwidth() + "x" + bitmap.getheight() ); mimage.setimagebitmap( bitmap ); mimage.setbackgrounddrawable( null ); meditbutton.setenabled( true ); mimageuri = uri; homecoming true; } /** * need notify mediascanner when new file created. * in way gallery applications notified too. * * @param file */ private void updatemedia( string filepath ) { log.i( log_tag, "updatemedia: " + filepath ); mediascannerconnection.scanfile( getapplicationcontext(), new string[] { filepath }, null, null ); } /** * pick random image user gallery * * @return */ @suppresswarnings("unused") private uri pickrandomimage() { cursor c = getcontentresolver().query( images.media.external_content_uri, new string[] { imagecolumns._id, imagecolumns.data }, imagecolumns.size + ">?", new string[] { "90000" }, null ); uri uri = null; if ( c != null ) { int total = c.getcount(); int position = (int) ( math.random() * total ); log.d( log_tag, "pickrandomimage. total images: " + total + ", position: " + position ); if ( total > 0 ) { if ( c.movetoposition( position ) ) { string info = c.getstring( c.getcolumnindex( images.imagecolumns.data ) ); long id = c.getlong( c.getcolumnindex( images.imagecolumns._id ) ); // can pass aviary-sdk uri "content://" scheme // or abolute file path "file:///mnt/..." or "/mnt/..." // using "content:/" style uri // uri = uri.withappendedpath( images.media.external_content_uri, string.valueof( id ) ); // using file scheme uri, passing real path uri = uri.parse( info ); log.d( log_tag, uri.tostring() ); } } c.close(); } homecoming uri; } /** * homecoming current application version string * * @return */ private string getlibraryversion() { string result = ""; seek { packagemanager manager = getpackagemanager(); packageinfo info = manager.getpackageinfo( getpackagename(), 0 ); result = info.versionname; } grab ( exception e ) {} homecoming result; } /** * homecoming new image file. name based on current time. parent folder 1 created createfolders * * @return * @see #createfolders() */ private file getnextfilename() { if ( mgalleryfolder != null ) { if ( mgalleryfolder.exists() ) { file file = new file( mgalleryfolder, "aviary_" + system.currenttimemillis() + ".jpg" ); homecoming file; } } homecoming null; } /** * 1 time you've chosen image can start feather activity * * @param uri */ @suppresswarnings("deprecation") private void startfeather( uri uri ) { log.d( log_tag, "uri: " + uri ); // first check external storage availability if ( !isexternalstorageavilable() ) { showdialog( external_storage_unavailable ); return; } // create temporary file store resulting image file file = getnextfilename(); if ( null != file ) { moutputfilepath = file.getabsolutepath(); } else { new alertdialog.builder( ).settitle( android.r.string.dialog_alert_title ).setmessage( "failed create new file" ) .show(); return; } msessionid = stringutils.getsha256( system.currenttimemillis() + mapikey ); log.d( log_tag, "session: " + msessionid + ", size: " + msessionid.length() ); newintent.putextra( constants.extra_output_hires_session_id, msessionid ); newintent.putextra( constants.extra_in_save_on_no_changes, true ); // ..and start feather startactivityforresult( newintent, action_request_feather ); } /** * check external storage status * * @return */ private boolean isexternalstorageavilable() { string state = environment.getexternalstoragestate(); if ( environment.media_mounted.equals( state ) ) { homecoming true; } homecoming false; } /** * start activity pick image user gallery */ private void pickfromgallery() { intent intent = new intent( intent.action_get_content ); intent.settype( "image/*" ); intent chooser = intent.createchooser( intent, "choose picture" ); startactivityforresult( chooser, action_request_gallery ); } /** * seek create required folder on sdcard images saved to. * * @return */ private file createfolders() { file basedir; if ( android.os.build.version.sdk_int < 8 ) { basedir = environment.getexternalstoragedirectory(); } else { basedir = environment.getexternalstoragepublicdirectory( environment.directory_pictures ); } if ( basedir == null ) homecoming environment.getexternalstoragedirectory(); log.d( log_tag, "pictures folder: " + basedir.getabsolutepath() ); file aviaryfolder = new file( basedir, folder_name ); if ( aviaryfolder.exists() ) homecoming aviaryfolder; if ( aviaryfolder.mkdirs() ) homecoming aviaryfolder; homecoming environment.getexternalstoragedirectory(); } /** * start hi-res image processing. * */ private void processhd( final string session_name ) { log.i( log_tag, "processhd: " + session_name ); // new file hi-res file file destination = getnextfilename(); seek { if ( destination == null || !destination.createnewfile() ) { log.e( log_tag, "failed create new file" ); return; } } grab ( ioexception e ) { log.e( log_tag, e.getmessage() ); toast.maketext( this, e.getlocalizedmessage(), toast.length_short ).show(); return; } string error = null; // need fetch session info content provider feathercontentprovider.sessionsdbcolumns.session session = null; uri sessionuri = feathercontentprovider.sessionsdbcolumns.getcontenturi( this, session_name ); // query homecoming cursor informations given session cursor cursor = getcontentresolver().query( sessionuri, null, null, null, null ); if ( null != cursor ) { session = feathercontentprovider.sessionsdbcolumns.session.create( cursor ); cursor.close(); } if ( null != session ) { // print out session informations log.d( log_tag, "session.id: " + session.id ); // session _id log.d( log_tag, "session.name: " + session.session ); // session name log.d( log_tag, "session.ctime: " + session.ctime ); // creation time log.d( log_tag, "session.file_name: " + session.file_name ); // original file, same passed in // startactivityforresult intent // now, based on session info need retrieve // list of actions apply hi-res image uri actionsuri = feathercontentprovider.actionsdbcolumns.getcontenturi( this, session.session ); // query homecoming list of actions performed on original file, during featheractivity session. // can apply each action hi-res image replicate same result on bigger image cursor = getcontentresolver().query( actionsuri, null, null, null, null ); if ( null != cursor ) { // if cursor valid start new asynctask process query cursor // , apply actions in queue hdasynctask task = new hdasynctask( uri.parse( session.file_name ), destination.getabsolutepath(), session_name ); task.execute( cursor ); } else { error = "failed retrieve list of actions!"; } } else { error = "failed retrieve session informations"; } if ( null != error ) { toast.maketext( this, error, toast.length_long ).show(); } } /** * delete session , it's actions. not need anymore.<br /> * note optional. old sessions automatically removed in feather. * * @param session_id */ private void deletesession( final string session_id ) { uri uri = feathercontentprovider.sessionsdbcolumns.getcontenturi( this, session_id ); getcontentresolver().delete( uri, null, null ); } /** * asynctask hi-res image processing * * @author alessandro * */ private class hdasynctask extends asynctask<cursor, integer, string> { uri uri_; string dstpath_; progressdialog progress_; string session_; exifinterfaceextended exif_; /** * initialize hires async task * * @param source * - source image file * @param destination * - destination image file * @param session_id * - session id used retrieve list of actions */ public hdasynctask( uri source, string destination, string session_id ) { uri_ = source; dstpath_ = destination; session_ = session_id; } @override protected void onpreexecute() { super.onpreexecute(); progress_ = new progressdialog( mainactivity.this ); progress_.setindeterminate( true ); progress_.settitle( "processing hi-res image" ); progress_.setmessage( "loading image..." ); progress_.setprogressstyle( progressdialog.style_spinner ); progress_.setcancelable( false ); progress_.show(); } @override protected void onprogressupdate( integer... values ) { super.onprogressupdate( values ); final int index = values[0]; final int total = values[1]; string message = ""; if ( index == -1 ) message = "saving image..."; else message = "applying action " + ( index + 1 ) + " of " + ( total ); progress_.setmessage( message ); log.d( log_tag, index + "/" + total + ", message: " + message ); } @override protected string doinbackground( cursor... params ) { cursor cursor = params[0]; if ( null != cursor ) { // of import note: // if in manifest you're using different process featheractivity activity // *must* phone call method before using of moahd methods, otherwise // receive java exception seek { nativefilterproxy.init( getbasecontext() ); } grab ( aviaryinitializationexception e ) { homecoming e.getmessage(); } // initialize class perform hd operations moahd moa = new moahd(); // premium partners only: // default maximum image size hi-res set 13mp ( fixed 3mp free version of sdk ) moa.setmaxmegapixels( megapixels.mp15 ); boolean loaded; seek { loaded = loadimage( moa ); } grab ( aviaryexecutionexception e ) { homecoming e.getmessage(); } // if image loaded if ( loaded ) { final int total_actions = cursor.getcount(); log.d( log_tag, "total actions: " + total_actions ); if ( cursor.movetofirst() ) { // total number of actions in queue // we're adding 'load' , 'save' action total count // each action in given cursor, apply action // moahd instance { // send progress notification progressbar dialog publishprogress( cursor.getposition(), total_actions ); // load action current cursor action action = action.create( cursor ); if ( null != action ) { log.d( log_tag, "executing: " + action.id + "(" + action.session_id + " on " + action.ctime + ") = " + action.getactions() ); // apply list of actions current image moa.applyactions( action.getactions() ); } else { log.e( log_tag, "woa, went wrong! invalid action returned" ); } // move cursor next position } while ( cursor.movetonext() ); } // @ end of operations need save // modified image new file publishprogress( -1, -1 ); seek { moa.save( dstpath_ ); } grab ( aviaryexecutionexception e ) { homecoming e.getmessage(); } { moa.dispose(); } // ok, can save source image exif tags // new image if ( null != exif_ ) { saveexif( exif_, dstpath_ ); } } else { homecoming "failed load image"; } cursor.close(); // , unload current bitmap. note *must* phone call method free memory allocated load // method if( moa.isloaded() ) { seek { moa.unload(); } grab ( aviaryexecutionexception e ) {} } if( !moa.isdisposed() ) { // dispose moahd instance moa.dispose(); } } homecoming null; } /** * save exif tags new image * * @param originalexif * @param filename */ private void saveexif( exifinterfaceextended originalexif, string filename ) { // ok, can save exif tags // new file exifinterfaceextended newexif = null; seek { newexif = new exifinterfaceextended( dstpath_ ); } grab ( ioexception e ) { // todo auto-generated grab block e.printstacktrace(); } if ( null != newexif && null != originalexif ) { // save original exif tags bundle object bundle out = new bundle(); originalexif.copyto( out ); // import exif tags original file newexif.copyfrom( out, true ); // should changed because editor rotate image newexif.setattribute( exifinterfaceextended.tag_exif_orientation, "0" ); // let's update software tag newexif.setattribute( exifinterfaceextended.tag_exif_software, "aviary " + sdkutils.sdk_version ); // ...and modification date newexif.setattribute( exifinterfaceextended.tag_exif_datetime, exifinterfaceextended.formatdate( new date() ) ); seek { newexif.saveattributes(); } grab ( ioexception e ) { e.printstacktrace(); } } } @override protected void onpostexecute( string errorstring ) { super.onpostexecute( errorstring ); if ( progress_.getwindow() != null ) { progress_.dismiss(); } // in case had error... if ( null != errorstring ) { toast.maketext( mainactivity.this, "there error: " + errorstring, toast.length_short ).show(); return; } ...
i managed figured out.
i utilize android intend :
intent takepictureintent = new intent(mediastore.action_image_capture); if (takepictureintent.resolveactivity(getpackagemanager()) != null) { startactivityforresult(takepictureintent, request_image_capture); } and in onactivityresult
case request_image_capture: // user chose image gallery loadasync( data.getdata() ); break; i grab result.
android android-intent android-camera mediastore aviary
Comments
Post a Comment