Android Camera Photo Samsung BUG Summary
Android Camera Samsung BUG:
Recently, the camera function was used in the Android project. The mobile phone of the Android model runs successfully, but the camera of Samsung encounters a bug.
The BUG details are as follows:
(1) The image data may not be returned after the camera takes a photo; the data of onActivityResult is empty.
(2) Samsung's camera force switch to the landscape screen, resulting in the Activity restart life cycle (however, some models cannot configure android: configChanges to prevent horizontal and vertical screen switching );
My solution is
If activity destruction cannot be avoided, call onSaveInstanceState to save the image path before activity destruction.
When the activity is re-created, it will pass the file saved in onSaveInstanceState to onCreate ().
In onCreate, check whether the photo address contains a file to determine whether the photo was taken successfully.
Good luck finally passed the test student's verification .....
My code is as follows:
Configure activity in Androidmanifest. xml
Add permissions:
Paste the Code:
1) Main activity
Function: Generate bitmap Based on the specified path.
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;/*** startup interface ** the photo generated directory is in/a/image/camera /... jpg ** @ author baozi **/public class MainActivity extends Activity {protected static final int REQCAMERA = 11; private static final String TAG = "MainActivity"; private View button1; priva Te ImageView photo_iv; private ContentResolver mContentResolver; final int IMAGE_MAX_SIZE = 1024; @ 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 REQCAMERA: String path = data. getStringExtra (UseCameraActivity. IMAGE_PATH); Log. I ("123", path); // obtain the image Bitm Based on the photo location Ap bitmap = getBitmap (path); photo_iv.setImageBitmap (bitmap); // ImageLoader. getInstance (). displayImage (// getImageUri (path ). toString (), photo_iv); break; default: super. onActivityResult (requestCode, resultCode, data); break ;}} private Uri getImageUri (String path) {return Uri. fromFile (new File (path);} private Bitmap getBitmap (String path) {Uri uri = getImageUri (path); InputStream in = null; try {in = mCont EntResolver. 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> IMAGE_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);} Bitma PFactory. 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 ;}} // please wait until then // #/// Commandid /// commandid was *****/// the commandid code was BUG-free! // Zookeeper has been completed before // zookeeper has been executed before being executed ┛ ┗ ┻ ┛
2) UseCameraActivity. java
The function of this class is to call the path after generating the image.
The directory generated by the photo is in/a/image/camera/... jpg of the SD card.
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;/*** directory generated by the photo on the SD card/a/image/camera /... jpg ** @ author Baozi **/public class UseCameraActivity extends Activity {private String mImageFilePath; public static final String IMAGEFILEPATH = "ImageFilePath"; public final static String IMAGE_PATH = "image_path"; static Activity mContext; public final static int GET_IMAGE_REQ = 5000; private static Context applicationContext; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstan CeState); // determines if any data is saved after the activity is destroyed. 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", "image shot succeeded"); finish ();} else {Log. I ("123 --- savedInstanceState", "image shooting failed") ;}} mContext = thi S; applicationContext = getApplicationContext (); if (savedInstanceState = null) {initialUI () ;}} public void initialUI () {// generate a suffix. jpg image 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. E XTRA_OUTPUT, Uri. fromFile (out); // setstartActivityForResult (intent, GET_IMAGE_REQ);} @ Overridepublic void onActivityResult (int requestCode, int resultCode, Intent intent) {if (GET_IMAGE_REQ = requestCode & 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); outState. putString ("ImageFilePath", mImageFilePath + "") ;}@ Overridepublic void onConfigurationChanged (Configuration newConfig) {super. onConfigurationChanged (newConfig) ;}@ Overrideprotected void onRestoreInstanceState (Bundle savedInstanceState) {s Uper. 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 ;}} // please wait until then // zookeeper // zookeeper without any bugs! // Zookeeper has been completed before // zookeeper has been executed before being executed ┛ ┗ ┻ ┛
Demo: http://download.csdn.net/detail/aaawqqq/7653475
Wish everyone a daily Improvement
Thank you.