Android camera Photo Samsung Bug Summary

Source: Internet
Author: User

Android Camera Samsung BUG:

The recent use of the camera feature in Android projects, other models of mobile phones have been successful, but only in Samsung's camera encountered a bug.

The bug is reflected as follows:

(1) Image data may not be returned after the camera is photographed; The Onactivityresult data is empty

(2) Samsung's camera forced to switch to a horizontal screen led to the Activity restart life cycle (but some models configuration android:configchanges also can not prevent horizontal screen switching);



My workaround is

If the activity is destroyed, then call Onsaveinstancestate to save the path to the picture before the activity is destroyed.

The onsaveinstancestate saved file is passed to OnCreate () when the activity is re-created.

Check if the photo's address exists in the OnCreate to determine if it was successful


Good luck finally passed the test of the students to verify .....


My code is as follows:


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 Interface * * Photo generated directory 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 ┃┃ Code No bug! ┃┗━━━┓//┃┣┓//┃┏┛//┗┓┓┏━┳┓┏┛//┃┫┫┃┫┫//┗┻┛┗┻┛


2) Usecameraactivity.java

The function of this class is to call the path after the shot of the generated picture

Photo generated directory 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 directory on SD card of/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);//Determine if activity is destroyed and 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 based on 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/ /┃┃ Code No bug! ┃┗━━━┓//┃┣┓//┃┏┛//┗┓┓┏━┳┓┏┛//┃┫┫┃┫┫//┗┻┛┗┻┛


demo:http://download.csdn.net/detail/aaawqqq/7653475


Wishing you every day to be diligent

Thank you


Android camera Photo Samsung Bug Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.