android - Problems of recording videos and pictures -


i'd create application record video or picture, process , display it. following instructions of official guide of how use camera, invoke existing applications recording videos , pictures. there 2 buttons switch main activity video capture or picture capture. of code copied guide (features , permissions have been added). however, doesn't work. code tested on samsung galaxy 3 mini, android 4.1.2.

1, when taking picture , press "save", shows "unfortunately ..." , quits. error in onactivityresult(int requestcode, int resultcode, intent data), data seems null;

2, when taking video, can open camera view there dialog "failed record".

3, deleted onactivityresult function , tested again, take photos cannot store sd card; take videos cannot return application activity.

most code above mentioned guide, , added 2 buttons. suggestions helpful, thanks!

public class mainactivity extends activity {  private static final int capture_image_activity_request_code = 100; private static final int capture_video_activity_request_code = 200; public static final int media_type_image = 1; public static final int media_type_video = 2;  private uri fileuri; private button bpic; private button bvideo;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      bpic = (button) findviewbyid(r.id.picture);     bpic.setonclicklistener(new onclicklistener(){          @override         public void onclick(view v) {             // todo auto-generated method stub             intent intent = new intent(mediastore.action_image_capture);             fileuri = getoutputmediafileuri(media_type_image);             intent.putextra(mediastore.extra_output, fileuri); // set image file name             startactivityforresult(intent, capture_image_activity_request_code);             }      });      bvideo = (button) findviewbyid(r.id.video);     bvideo.setonclicklistener(new onclicklistener(){          @override         public void onclick(view v) {             // todo auto-generated method stub             intent intent = new intent(mediastore.action_video_capture);             fileuri = getoutputmediafileuri(media_type_video);             intent.putextra(mediastore.extra_output, fileuri); // set video file name             intent.putextra(mediastore.extra_video_quality, 1);             startactivityforresult(intent, capture_video_activity_request_code);             }      });  }  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.main, menu);     return true; }  @override protected void onactivityresult(int requestcode, int resultcode, intent data) {     if (requestcode == capture_image_activity_request_code) {         if (resultcode == result_ok) {             // image captured , saved fileuri specified in intent             toast.maketext(this, "image saved to:\n" +                      data.getdata(), toast.length_long).show();         } else if (resultcode == result_canceled) {             // user cancelled image capture         } else {             // image capture failed, advise user         }     }      if (requestcode == capture_video_activity_request_code) {         if (resultcode == result_ok) {             // video captured , saved fileuri specified in intent             toast.maketext(this, "video saved to:\n" +                      data.getdata(), toast.length_long).show();         } else if (resultcode == result_canceled) {             // user cancelled video capture         } else {             // video capture failed, advise user         }     } }  /** create file uri saving image or video */ private static uri getoutputmediafileuri(int type){     return uri.fromfile(getoutputmediafile(type)); }  private static file getoutputmediafile(int type){     // safe, should check sdcard mounted     // using environment.getexternalstoragestate() before doing this.      file mediastoragedir = new file(environment.getexternalstoragepublicdirectory(               environment.directory_pictures), "mycameraapp");     // location works best if want created images shared     // between applications , persist after app has been uninstalled.      // create storage directory if not exist     if (! mediastoragedir.exists()){         if (! mediastoragedir.mkdirs()){             log.d("mycameraapp", "failed create directory");             return null;         }     }      // create media file name     string timestamp = new simpledateformat("yyyymmdd_hhmmss").format(new date());     file mediafile;     if (type == media_type_image){         mediafile = new file(mediastoragedir.getpath() + file.separator +         "img_"+ timestamp + ".jpg");     } else if(type == media_type_video) {         mediafile = new file(mediastoragedir.getpath() + file.separator +         "vid_"+ timestamp + ".mp4");     } else {         return null;     }      return mediafile; }  @override public boolean onoptionsitemselected(menuitem item) {     // handle action bar item clicks here. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();     if (id == r.id.action_settings) {         return true;     }     return super.onoptionsitemselected(item); } } 

the manifest shown below:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mycameratest" android:versioncode="1" android:versionname="1.0" >  <uses-sdk     android:minsdkversion="15"     android:targetsdkversion="16" />  <uses-permission android:name="android.permission.camera" /> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.record_audio" /> <uses-permission android:name="android.permission.read_external_storage" /> <uses-feature android:name="android.hardware.camera" />  <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> 

someone says need , added still has problems.

update 03/17: use phone, lenovo, android 4.0.3, , video record function correct. however, when taking photo , press "save", still quit , display "unfortunately ...".

update 03/17: error in toast.maketext(this, "image saved to:\n"+data.getdata(), toast.length_long).show() , seems data null here. quite weird because data has been put intent. however, make toast of "image has been saved" instead of original 1 , works correctly now.

as gabe mentioned in comments, adding

   <uses-permission android:name="android.permission.read_external_storage" /> 

to androidmanifest.xml should remedy problem.

as crash when taking picture, seems image exists in file location specified in intent see android camera : data intent returns null more detailed explanation.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -