java - Save a photo selected from gallery to cache directory -


i've written method creates directory in apps cache directory , prompts user take photo or select photo gallery.

if user takes new photo, photo saved cache profile.jpg

i'm trying intent returns photo gallery save returned photo in cache directory of app profile.jpg.

i'm having trouble achieving this.

public void selectimage(view v) {          file newdir = new file(getexternalcachedir(), "recruitswift");         if(!newdir.isdirectory())             newdir.mkdirs();         else             toast.maketext(this, "dir exist", toast.length_long).show();          if(newdir.canwrite())             imagefile = new file(newdir, "profile.jpg");         else             toast.maketext(this, "dir not writable", toast.length_long).show();          final charsequence[] options = { "take photo", "choose gallery","cancel" };          alertdialog.builder builder = new alertdialog.builder(userprofileinterviewscreenactivity.this);         builder.settitle("add photo!");         builder.setitems(options, new dialoginterface.onclicklistener() {             @override             public void onclick(dialoginterface dialog, int item) {                 if (options[item].equals("take photo")){                     intent takeprofileimage = new intent(mediastore.action_image_capture);                     if (takeprofileimage.resolveactivity(getpackagemanager()) != null) {                          uri imageuri = uri.fromfile(imagefile);                         takeprofileimage.putextra(mediastore.extra_output, imageuri);                         startactivityforresult(takeprofileimage, 1);                     }                 }                 else if (options[item].equals("choose gallery")){                     intent takeprofileimage = new   intent(intent.action_pick,android.provider.mediastore.images.media.external_content_uri);                      uri selectedimage = takeprofileimage.getdata();                     imagefile = selectedimage;                     startactivityforresult(takeprofileimage, 2);                  }                 else if (options[item].equals("cancel")) {                     dialog.dismiss();                 }             }         });         builder.show();     } 

if want take photo or pick picture, need call startactivityforresult , override onactivityresult. here codes take photo, take look:


startactivityforresult:

intent intent = new intent("android.media.action.image_capture"); intent.putextra("return-data", false); intent.putextra(mediastore.extra_output, uri.fromfile(avatar_file_tmp)); intent.putextra("outputformat", bitmap.compressformat.jpeg.tostring()); intent.putextra("nofacedetection", true); startactivityforresult(intent, code_take_photo); 

then override onactivityresult:

if (resultcode == activity.result_ok) {         if (requestcode == code_take_photo) {             cropimage(uri.fromfile(avatar_file_tmp));         } } 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -