Android Camera Samsung BUG:
Recent use of the camera feature in Android projects, other models of mobile phone execution success only on the Samsung camera has encountered a bug.
The bug is detailed as follows:
(1) Image data may not be returned after the camera is photographed; The Onactivityresult data is empty
(2) Samsung's camera is forced to switch to a horizontal screen resulting in activity restart life cycle (but some models configuration android:configchanges also can not prevent horizontal screen switching);
My workaround is
Assuming that the activity's destruction hypothesis cannot be avoided, then call Onsaveinstancestate to save the path to the picture before the activity is destroyed
When the activity is created again, the onsaveinstancestate saved file is passed to OnCreate () where
Determine if the photo was successful by checking to see if there is a file in OnCreate
Good luck finally passed the test of the students to verify that .....
My code for example is the following:
Configuring Configuration activity in Androidmanifest.xml
<activity android:name= ". Usecameraactivity " android:configchanges=" keyboard|keyboardhidden|orientation|screenlayout|uimode| Screensize|smallestscreensize|navigation " android:launchmode=" singletop " android:screenorientation=" Portrait "/>
Add Permissions:
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission Android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
Paste the code:
1) Main activity
The function is: Generate bitmap according to the specified path; Show pictures
Package Com.example.camerabaozi;import Java.io.file;import Java.io.filenotfoundexception;import Java.io.ioexception;import Java.io.inputstream;import Android.app.activity;import Android.content.ContentResolver ; Import Android.content.intent;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.net.uri;import Android.os.bundle;import Android.util.log;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.imageview;import com.nostra13.universalimageloader.core.imageloader;/** * Start Screen * * Photo generated folder in SD card/a/image/camera/. . jpg * * @author Baozi * */public class Mainactivity extends Activity {protected static final int reqcamera = 11;private Static final String TAG = "mainactivity";p rivate View button1;private ImageView photo_iv;private contentresolver mcontent resolver;final int image_max_size = 1024x768, @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); setcOntentview (r.layout.activity_main); mcontentresolver = Getcontentresolver (); button1 = (Button) Findviewbyid ( R.id.button1); Button1.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {Intent Intent = New Intent (Mainactivity.this,usecameraactivity.class); Startactivityforresult (Intent, Reqcamera);}); Photo_iv = (ImageView) Findviewbyid (r.id.imageview1);} @Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {switch (requestcode) {case Reqcam era:string path = Data.getstringextra (Usecameraactivity.image_path); LOG.I ("123", path);//Get the picture bitmap bitmap = getbitmap (path);p hoto_iv.setimagebitmap (bitmap) depending on the location of the photo;// Imageloader.getinstance (). DisplayImage (//Getimageuri (path). ToString (), Photo_iv); Break;default: Super.onactivityresult (Requestcode, ResultCode, data); break;}} Private Uri Getimageuri (String path) {return uri.fromfile (path);} Private Bitmap Getbitmap (String path) {uri uri = Getimageuri (path); InputStream in = null;try {in =Mcontentresolver.openinputstream (URI);//Decode image sizebitmapfactory.options o = new Bitmapfactory.options (); O. Injustdecodebounds = true; Bitmapfactory.decodestream (in, NULL, O); In.close (); int scale = 1;if (O.outheight > Image_max_size | | o.outwidth > IM Age_max_size) {scale = (int) Math.pow (2, (int) Math.Round (Math.log (image_max_size/(double) Math.max (O.outheight, o.outwidth))/Math.log (0.5));} Bitmapfactory.options O2 = new Bitmapfactory.options (); o2.insamplesize = Scale;in = Mcontentresolver.openinputstream ( URI); Bitmap B = Bitmapfactory.decodestream (in, null, O2); In.close (); return b;} catch (FileNotFoundException e) {log.e (TAG, "file" + path + "not Found");} catch (IOException e) {log.e (TAG, "file" + path + "not Found");} return null;}} ┏┓┏┓//┏┛┻━━━┛┻┓//┃┃//┃━┃//┃┳┛┗┳┃//┃┃//┃┻┃//┃┃//┗━┓┏━┛//┃┃ God Beast Bless There is no bug in ┃┃ code. ┃┗━━━┓//┃┣┓//┃┏┛//┗┓┓┏━┳┓┏┛//┃┫┫┃┫┫//┗┻┛┗┻┛
2) Usecameraactivity.java
The function of this class is to call the path after the shot of the generated picture
Photo generated folder on SD card/a/image/camera/. . jpg
Package Com.example.camerabaozi;import Java.io.file;import Java.io.ioexception;import android.app.Activity;import Android.content.context;import Android.content.intent;import Android.content.res.configuration;import Android.net.uri;import Android.os.bundle;import Android.os.environment;import Android.provider.MediaStore;import android.util.log;/** * Photo generated folder on SD card/a/image/camera/. . jpg * * @author Baozi * */public class Usecameraactivity extends Activity {private String mimagefilepath;public static Final String Imagefilepath = "Imagefilepath";p ublic final static string image_path = "Image_path"; static Activity Mcontext ;p ublic final static int get_image_req = 5000;private static Context applicationcontext; @Overrideprotected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate);//infer that the activity is destroyed after there is no data saved if (savedinstancestate! = NULL) {Mimagefilepath = savedinstancestate.getstring (Imagefilepath); LOG.I ("123---savedinstancestate", mimagefilepath); File Mfile = NEW File (Imagefilepath), if (Mfile.exists ()) {Intent RSL = new Intent (); Rsl.putextra (Image_path, Mimagefilepath); Setresult (ACTIVITY.RESULT_OK, RSL); LOG.I ("123---savedinstancestate", "Photo shoot success"); Finish ();} else {log.i ("123---savedinstancestate", "Picture shot Failed");}} Mcontext = This;applicationcontext = Getapplicationcontext (); if (savedinstancestate = = null) {Initialui ();}} public void Initialui () {//To generate a picture suffix of. jpg by time long ts = System.currenttimemillis (); mimagefilepath = Getcamerapath () + TS + ". jpg"; File out = new file (Mimagefilepath), Showcamera (out);} private void Showcamera (File out) {Intent Intent = new Intent (mediastore.action_image_capture); Intent.putextra ( Mediastore.extra_output, Uri.fromfile (out)); Setstartactivityforresult (Intent, get_image_req);} @Overridepublic void Onactivityresult (int requestcode, int resultcode, Intent Intent) {if (Get_image_req = = Requestcode &A mp;& ResultCode = = ACTIVITY.RESULT_OK) {Intent RSL = new Intent (); Rsl.putextra (Image_path, Mimagefilepath); Mcontext.setreSult (ACTIVITY.RESULT_OK, RSL); Mcontext.finish ();} else {mcontext.finish ();}} @Overrideprotected void OnDestroy () {Super.ondestroy ();} @Overrideprotected void Onsaveinstancestate (Bundle outstate) {super.onsaveinstancestate (outstate); o Utstate.putstring ("Imagefilepath", Mimagefilepath + "");} @Overridepublic void onconfigurationchanged (Configuration newconfig) {super.onconfigurationchanged (newconfig);} @Overrideprotected void Onrestoreinstancestate (Bundle savedinstancestate) {super.onrestoreinstancestate ( Savedinstancestate);} public static string Getcamerapath () {String filePath = Getimagerootpath () + "/camera/"; File File = new file (FilePath), if (!file.isdirectory ()) {file.mkdirs ();} File = Null;return FilePath;} public static string Getimagerootpath () {String filePath = Getapprootpath () + "/image"; File File = new file (FilePath), if (!file.exists ()) {file.mkdirs ();} File = Null;return FilePath;} public static string Getapprootpath () {String filePath = "/a"; if (Environment.getexternalstoragestate ().Equals (environment.media_mounted)) {FilePath = Environment.getexternalstoragedirectory () + FilePath;} else {FilePath = Applicationcontext.getcachedir () + FilePath;} File File = new file (FilePath), if (!file.exists ()) {file.mkdirs ();} FILE = null; File Nomedia = new file (FilePath + "/.nomedia"), if (!nomedia.exists ()) try {nomedia.createnewfile ();} catch (IOException E ) {}return FilePath;}} ┏┓┏┓//┏┛┻━━━┛┻┓//┃┃//┃━┃//┃┳┛┗┳┃//┃┃//┃┻┃//┃┃//┗━┓┏━┛//┃┃ God beast Bless/ There is no bug in/┃┃ code. ┃┗━━━┓//┃┣┓//┃┏┛//┗┓┓┏━┳┓┏┛//┃┫┫┃┫┫//┗┻┛┗┻┛
demo:http://download.csdn.net/detail/aaawqqq/7653475
Wishing you every day to be diligent
Thank you
Android camera Photo Samsung Bug Summary