android : camera doesn't open in marshmallow -
so, have below code open camera, capture image , save on sdcard.
public void getphotofromcamera() { intent takepictureintent = new intent(mediastore.action_image_capture); file mediastoragedir = new file( environment.getexternalstoragedirectory() + file.separator + getstring(r.string.directory_name_corp_chat) + file.separator + getstring(r.string.directory_name_temp) ); if (!mediastoragedir.exists()) { mediastoragedir.mkdirs(); } string timestamp = new simpledateformat("yyyymmdd_hhmmss", locale.getdefault()).format(new date()); try { mediafile = file.createtempfile( "temp_full_img_" + timestamp, ".jpg", mediastoragedir ); takepictureintent.putextra(mediastore.extra_output, uri.fromfile(mediafile)); startactivityforresult(takepictureintent, pick_from_camera); } catch (ioexception e) { e.printstacktrace(); } } private void performcrop(uri picuri) { try { intent cropintent = new intent("com.android.camera.action.crop"); cropintent.setdataandtype(picuri, "image/*"); cropintent.putextra("crop", "true"); cropintent.putextra("aspectx", 1); cropintent.putextra("aspecty", 1); cropintent.putextra("outputx", 128); cropintent.putextra("outputy", 128); // retrieve data on return cropintent.putextra("return-data", true); file mediastoragedir = new file( environment.getexternalstoragedirectory() + file.separator + getstring(r.string.directory_name_corp_chat) + file.separator + getstring(r.string.directory_name_temp) ); if (!mediastoragedir.exists()) { mediastoragedir.mkdirs(); } string timestamp = new simpledateformat("yyyymmdd_hhmmss", locale.getdefault()).format(new date()); try { croppedfile = file.createtempfile( "temp_cropped_img_" + timestamp, ".jpg", mediastoragedir ); cropintent.putextra(mediastore.extra_output, uri.fromfile(croppedfile)); startactivityforresult(cropintent, pic_crop); } catch (ioexception e) { e.printstacktrace(); } } // respond users devices not support crop action catch (activitynotfoundexception anfe) { // display error message string errormessage = "whoops - device doesn't support crop action!"; toast toast = toast.maketext(this, errormessage, toast.length_short); toast.show(); } } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == pick_from_camera) { if (resultcode == result_ok) { performcrop(uri.fromfile(mediafile)); } else { log.i("camera", "result cancel. hence, deleting file: " + mediafile.getpath()); log.i("file deleted ", mediafile.delete() + ""); } } if (requestcode == pick_from_gallery) { if (resultcode == result_ok) { performcrop(data.getdata()); } else { log.i("gallery", "result cancel"); } } if (requestcode == pic_crop) { if (resultcode == result_ok) { imageview.setimagebitmap(bitmapfactory.decodefile(croppedfile.getabsolutepath())); if (mediafile != null) { log.i("camera", "result cancel. hence, deleting file: " + mediafile.getpath()); log.i("file deleted ", mediafile.delete() + ""); } } else { if (croppedfile != null) { log.i("camera", "result cancel. hence, deleting file: " + croppedfile.getpath()); log.i("file deleted ", croppedfile.delete() + ""); } if (mediafile != null) { log.i("camera", "result cancel. hence, deleting file: " + mediafile.getpath()); log.i("file deleted ", mediafile.delete() + ""); } } } }
everything works perfect expected below android 6.0. doesn't work on android 6.0 marshmallow. in fact doesn't open camera :(
i don't know whether have special marshmallow. not getting kind of error too, can post here. please me out.
thanks.
so, accomplished task below:
for checking permission created separate class below:
public class marshmallowpermission { public static final int record_permission_request_code = 1; public static final int external_storage_permission_request_code = 2; public static final int camera_permission_request_code = 3; activity activity; public marshmallowpermission(activity activity) { this.activity = activity; } public boolean checkpermissionforrecord(){ int result = contextcompat.checkselfpermission(activity, manifest.permission.record_audio); if (result == packagemanager.permission_granted){ return true; } else { return false; } } public boolean checkpermissionforexternalstorage(){ int result = contextcompat.checkselfpermission(activity, manifest.permission.write_external_storage); if (result == packagemanager.permission_granted){ return true; } else { return false; } } public boolean checkpermissionforcamera(){ int result = contextcompat.checkselfpermission(activity, manifest.permission.camera); if (result == packagemanager.permission_granted){ return true; } else { return false; } } public void requestpermissionforrecord(){ if (activitycompat.shouldshowrequestpermissionrationale(activity, manifest.permission.record_audio)){ toast.maketext(activity, "microphone permission needed recording. please allow in app settings additional functionality.", toast.length_long).show(); } else { activitycompat.requestpermissions(activity,new string[]{manifest.permission.record_audio},record_permission_request_code); } } public void requestpermissionforexternalstorage(){ if (activitycompat.shouldshowrequestpermissionrationale(activity, manifest.permission.write_external_storage)){ toast.maketext(activity, "external storage permission needed. please allow in app settings additional functionality.", toast.length_long).show(); } else { activitycompat.requestpermissions(activity,new string[]{manifest.permission.write_external_storage},external_storage_permission_request_code); } } public void requestpermissionforcamera(){ if (activitycompat.shouldshowrequestpermissionrationale(activity, manifest.permission.camera)){ toast.maketext(activity, "camera permission needed. please allow in app settings additional functionality.", toast.length_long).show(); } else { activitycompat.requestpermissions(activity,new string[]{manifest.permission.camera},camera_permission_request_code); } } }
then, getting
... marshmallowpermission marshmallowpermission = new marshmallowpermission(this); ... public void getphotofromcamera() { if (!marshmallowpermission.checkpermissionforcamera()) { marshmallowpermission.requestpermissionforcamera(); } else { if (!marshmallowpermission.checkpermissionforexternalstorage()) { marshmallowpermission.requestpermissionforexternalstorage(); } else { intent takepictureintent = new intent(mediastore.action_image_capture); file mediastoragedir = new file( environment.getexternalstoragedirectory() + file.separator + getstring(r.string.directory_name_corp_chat) + file.separator + getstring(r.string.directory_name_images) ); if (!mediastoragedir.exists()) { mediastoragedir.mkdirs(); } string timestamp = new simpledateformat("yyyymmdd_hhmmss", locale.getdefault()).format(new date()); try { mediafile = file.createtempfile( "img_" + timestamp, /* prefix */ ".jpg", /* suffix */ mediastoragedir /* directory */ ); takepictureintent.putextra(mediastore.extra_output, uri.fromfile(mediafile)); startactivityforresult(takepictureintent, pick_from_camera); } catch (ioexception e) { e.printstacktrace(); } } } }
Comments
Post a Comment