java - Capture, store and retrieve photo in activity -


i have problem, can't fix. have spent day today trying work, can't , out of ideas, decided ask help.

i not experienced java/android developer bare me! also, have looked around google , stackoverflow answers, none of helped me.

the problem regards photo capturing android. have activity, can "create" new item later on stored in database. can pass name , location, take photo binds it. when press "add" saved database. works, except camera intent. want able take photo in activity, binds "object" i'm creating, later on can view photo when click on particular "object" on list. have permissions (write , read) in androidmanifest, still dont help.

<uses-permission android:name="android.permission.read_external_storage"                  android:maxsdkversion="18"     /> <uses-permission android:name="android.permission.write_external_storage"                  android:maxsdkversion="18"     /> <uses-feature android:name="android.hardware.camera"               android:required="false"     /> 

my android version 6.0. code recent compilation of solutions have gathered try make work. appreciate issue.

import android.manifest; import android.app.activity; import android.content.intent; import android.content.pm.packagemanager; import android.graphics.bitmap; import android.net.uri; import android.os.bundle; import android.os.environment; import android.provider.mediastore; import android.support.v4.app.activitycompat; import android.support.v4.app.fragment; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.button; import android.widget.imagebutton; import android.widget.imageview; import android.widget.textview; import android.widget.toast; import java.io.file; import java.io.ioexception; import java.text.simpledateformat; import java.util.date; import java.util.list;  public class createitemfragment extends fragment {      private static final int request_photo = 2;     private static final string tag = "button clicked";     private static final int request_external_storage = 1;     private static string[] permissions_storage = {             manifest.permission.read_external_storage,             manifest.permission.write_external_storage     };      private textview mnametext;     private textview mlocationtext;     private button maddbutton;     private imagebutton mphotobutton;     private imageview mphotoview;     private string mcurrentphotopath;     private file mphotofile;     private item mitem;     private list<item> mitems;     private static itemstash sitemstash;      public static createitemfragment newinstance(){         return new createitemfragment();     }      @override     public void oncreate(bundle savedinstancestate){         super.oncreate(savedinstancestate);       }       @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {          view view = inflater.inflate(r.layout.fragment_create_item, container, false);          mnametext = (textview)view.findviewbyid(r.id.item_name);         mlocationtext = (textview)view.findviewbyid(r.id.item_location);         sitemstash = sitemstash.get(getcontext());           maddbutton = (button)view.findviewbyid(r.id.add_button);         maddbutton.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 if (mnametext.gettext().length() > 0 && mlocationtext.gettext().length() > 0){                     sitemstash.additem(                             new item(mnametext.gettext().tostring(),                                     mlocationtext.gettext().tostring()));                     toast.maketext(getactivity(), mnametext.gettext().tostring() + " added list!", toast.length_short).show();                 }                 mnametext.settext("");                 mlocationtext.settext("");              }         });          mphotobutton = (imagebutton)view.findviewbyid(r.id.photo_button);         //final intent captureimage = new intent(mediastore.action_image_capture);          /*         packagemanager packagemanager = getactivity().getpackagemanager();         boolean cantakephoto = mphotofile != null && captureimage.resolveactivity(packagemanager) != null;         mphotobutton.setenabled(cantakephoto);          if(cantakephoto) {             uri uri = uri.fromfile(mphotofile);             captureimage.putextra(mediastore.extra_output, uri);         }         */          mphotobutton.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 verifystoragepermissions(getactivity());                 capturephotointent();                 //startactivityforresult(captureimage, request_photo);                 //mphotofile = new file(environment.getexternalstoragepublicdirectory(environment.directory_pictures).tostring());                 //mphotofile = new file(mitem.setphotofilename(mnametext.gettext().tostring()));             }           });          mphotoview = (imageview)view.findviewbyid(r.id.item_photo);         //updatephotoview();          return view;     }       @override     public void onactivityresult(int requestcode, int resultcode, intent data){         if(requestcode == request_photo && resultcode == activity.result_ok){             bundle extras = data.getextras();             bitmap imagebitmap = (bitmap)extras.get("data");             //mphotofile = new file(environment.getexternalstoragepublicdirectory(environment.directory_pictures).tostring());             mphotoview.setimagebitmap(imagebitmap);         }       }      /*     private void updatephotoview(){         if(mphotofile == null || !mphotofile.exists()){             log.d(tag, "is clicked");             mphotoview.setimagedrawable(null);         }else{             bitmap bitmap = photoutils.getscaledbitmap(mphotofile.getpath(), getactivity());             mphotoview.setimagebitmap(bitmap);         }     }     */      private void capturephotointent(){         intent photointent = new intent(mediastore.action_image_capture);         packagemanager packagemanager = getactivity().getpackagemanager();         if(photointent.resolveactivity(packagemanager) != null){             file photofile = null;             try{                 photofile = createimagefile();             }catch (ioexception ioe){                 ioe.printstacktrace();             }              if(photofile != null) {                 photointent.putextra(mediastore.extra_output, uri.fromfile(photofile));                 startactivityforresult(photointent, request_photo);             }          }     }      public file createimagefile() throws ioexception {         verifystoragepermissions(getactivity());          string timestamp = new simpledateformat("yyyymmdd_hhmmss").format(new date());         string imagefilename = "jpeg_" + timestamp + "_";         file storagedir = environment.getexternalstoragepublicdirectory(environment.directory_pictures);         file image = file.createtempfile(                 imagefilename,                 ".jpg",                 storagedir         );          mcurrentphotopath = "file:" + image.getabsolutepath();         return image;     }      public static void verifystoragepermissions(activity activity) {         int permission = activitycompat.checkselfpermission(activity, manifest.permission.write_external_storage);          if (permission != packagemanager.permission_granted) {              activitycompat.requestpermissions(                     activity,                     permissions_storage,                     request_external_storage             );         }     }        } 

*edit forgot paste in stack trace. this:

04-21 22:34:56.688 3574-3604/com.bignerdranch.android.tingleapp   w/openglrenderer: fail change fontrenderer cache size, initialized 04-21 22:34:56.704 3574-3574/com.bignerdranch.android.tingleapp d/android.widget.gridlayout: horizontal constraints: x1-x0>=163, x2-x1>=1080, x2-x0<=1080 inconsistent; permanently removing: x2-x0<=1080.  04-21 22:34:56.708 3574-3574/com.bignerdranch.android.tingleapp d/android.widget.gridlayout: vertical constraints: y2-y0>=745, y2-y1<=136, y1-y0<=136 inconsistent; permanently removing: y2-y1<=136.  04-21 22:34:56.819 3574-3604/com.bignerdranch.android.tingleapp e/surface: getslotfrombufferlocked: unknown buffer: 0xb8767340 04-21 22:34:57.533 3574-3574/com.bignerdranch.android.tingleapp w/system.err: java.io.ioexception: open failed: eacces (permission denied) 04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp w/system.err:     @ java.io.file.createnewfile(file.java:939) 04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp w/system.err:     @ java.io.file.createtempfile(file.java:1004) 04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp w/system.err:     @ com.bignerdranch.android.tingleapp.createitemfragment.createimagefile(createitemfragment.java:173) 04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp w/system.err:     @ com.bignerdranch.android.tingleapp.createitemfragment.capturephotointent(createitemfragment.java:154) 04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp w/system.err:     @ com.bignerdranch.android.tingleapp.createitemfragment.access$300(createitemfragment.java:30) 04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp w/system.err:     @ com.bignerdranch.android.tingleapp.createitemfragment$2.onclick(createitemfragment.java:108) 04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp w/system.err:     @ android.view.view.performclick(view.java:5226) 04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp w/system.err:     @ android.view.view$performclick.run(view.java:21266) 04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp w/system.err:     @ android.os.handler.handlecallback(handler.java:739) 04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp w/system.err:     @ android.os.handler.dispatchmessage(handler.java:95) 04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp w/system.err:     @ android.os.looper.loop(looper.java:168) 04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp w/system.err:     @ android.app.activitythread.main(activitythread.java:5845) 04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp w/system.err:     @ java.lang.reflect.method.invoke(native method) 04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp w/system.err:     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:797) 04-21 22:34:57.539 3574-3574/com.bignerdranch.android.tingleapp w/system.err:     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:687) 04-21 22:34:57.539 3574-3574/com.bignerdranch.android.tingleapp w/system.err: caused by: android.system.errnoexception: open failed: eacces (permission denied) 04-21 22:34:57.539 3574-3574/com.bignerdranch.android.tingleapp w/system.err:     @ libcore.io.posix.open(native method) 04-21 22:34:57.539 3574-3574/com.bignerdranch.android.tingleapp w/system.err:     @ libcore.io.blockguardos.open(blockguardos.java:186) 04-21 22:34:57.539 3574-3574/com.bignerdranch.android.tingleapp w/system.err:     @ java.io.file.createnewfile(file.java:932) 04-21 22:34:57.539 3574-3574/com.bignerdranch.android.tingleapp w/system.err:   ... 14 more 

the method verisfystoragepermission() supposed fix error found in other topics, still error


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -