Android 拍照强制横屏解决...

Android Camera 三星BUG  :

              最近在Android项目中使用拍照功能 , 其它型号(华为,小米,中兴,魅族...)的手机运行成功了  唯独在三星的相机上遇到了bug .

BUG具体体现为 :

(1) 摄像头拍照后图片数据不一定能返回 ;  onActivityResult的data为空  

(2) 三星的camera强制切换到横屏  导致Activity重启生命周期 (但是部分机型  配置  android:configChanges  也不能阻止横竖屏切换); 



我的解决方法为  

如果 activity 的销毁如果无法避免   那么在activity销毁之前调用 onSaveInstanceState  保存图片的路径   

当activity重新创建的时候 会将 onSaveInstanceState  保存的文件传递给onCreate()当中

在onCreate当中  检查照片的地址是否存在文件  以此来判定拍照是否成功


运气不错  终于通过了测试同学们的验证.....


我的代码如下:


配置   Androidmanifest.xml  中的配置 activity

[java]  view plain  copy
  1. <activity  
  2.     android:name=".UseCameraActivity"  
  3.     android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|navigation"  
  4.     android:launchMode="singleTop"  
  5.     android:screenOrientation="portrait" />  


 增加权限:

[java]  view plain  copy
  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
  2. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />  

贴上代码:

1) 主activity    

功能是: 根据指定的路径  生成bitmap  ; 显示图片

[java]  view plain  copy
  1. package com.example.camerabaozi;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7.   
  8. import android.app.Activity;  
  9. import android.content.ContentResolver;  
  10. import android.content.Intent;  
  11. import android.graphics.Bitmap;  
  12. import android.graphics.BitmapFactory;  
  13. import android.net.Uri;  
  14. import android.os.Bundle;  
  15. import android.util.Log;  
  16. import android.view.View;  
  17. import android.view.View.OnClickListener;  
  18. import android.widget.Button;  
  19. import android.widget.ImageView;  
  20.   
  21. import com.nostra13.universalimageloader.core.ImageLoader;  
  22.   
  23. /** 
  24.  * 启动界面 
  25.  *  
  26.  * 照片生成的目录在 sd卡的/a/image/camera/.. .jpg 
  27.  *  
  28.  * @author baozi 
  29.  *  
  30.  */  
  31. public class MainActivity extends Activity {  
  32.   
  33.     protected static final int REQCAMERA = 11;  
  34.     private static final String TAG = "MainActivity";  
  35.     private View button1;  
  36.     private ImageView photo_iv;  
  37.     private ContentResolver mContentResolver;  
  38.     final int IMAGE_MAX_SIZE = 1024;  
  39.   
  40.     @Override  
  41.     protected void onCreate(Bundle savedInstanceState) {  
  42.         super.onCreate(savedInstanceState);  
  43.         setContentView(R.layout.activity_main);  
  44.         mContentResolver = getContentResolver();  
  45.   
  46.         button1 = (Button) findViewById(R.id.button1);  
  47.         button1.setOnClickListener(new OnClickListener() {  
  48.   
  49.             @Override  
  50.             public void onClick(View v) {  
  51.                 Intent intent = new Intent(MainActivity.this,  
  52.                         UseCameraActivity.class);  
  53.                 startActivityForResult(intent, REQCAMERA);  
  54.             }  
  55.         });  
  56.         photo_iv = (ImageView) findViewById(R.id.imageView1);  
  57.   
  58.     }  
  59.   
  60.     @Override  
  61.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  62.         switch (requestCode) {  
  63.         case REQCAMERA:  
  64.             String path = data.getStringExtra(UseCameraActivity.IMAGE_PATH);  
  65.             Log.i("123", path);  
  66.   
  67.             // 根据照片的位置获取图片  
  68.             Bitmap bitmap = getBitmap(path);  
  69.             photo_iv.setImageBitmap(bitmap);  
  70.   
  71.             // ImageLoader.getInstance().displayImage(  
  72.             // getImageUri(path).toString(), photo_iv);  
  73.   
  74.             break;  
  75.   
  76.         default:  
  77.             super.onActivityResult(requestCode, resultCode, data);  
  78.             break;  
  79.         }  
  80.   
  81.     }  
  82.   
  83.     private Uri getImageUri(String path) {  
  84.   
  85.         return Uri.fromFile(new File(path));  
  86.     }  
  87.   
  88.     private Bitmap getBitmap(String path) {  
  89.   
  90.         Uri uri = getImageUri(path);  
  91.         InputStream in = null;  
  92.         try {  
  93.             in = mContentResolver.openInputStream(uri);  
  94.   
  95.             // Decode image size  
  96.             BitmapFactory.Options o = new BitmapFactory.Options();  
  97.             o.inJustDecodeBounds = true;  
  98.   
  99.             BitmapFactory.decodeStream(in, null, o);  
  100.             in.close();  
  101.   
  102.             int scale = 1;  
  103.             if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {  
  104.                 scale = (int) Math.pow(  
  105.                         2,  
  106.                         (int) Math.round(Math.log(IMAGE_MAX_SIZE  
  107.                                 / (double) Math.max(o.outHeight, o.outWidth))  
  108.                                 / Math.log(0.5)));  
  109.             }  
  110.   
  111.             BitmapFactory.Options o2 = new BitmapFactory.Options();  
  112.             o2.inSampleSize = scale;  
  113.             in = mContentResolver.openInputStream(uri);  
  114.             Bitmap b = BitmapFactory.decodeStream(in, null, o2);  
  115.             in.close();  
  116.   
  117.             return b;  
  118.         } catch (FileNotFoundException e) {  
  119.             Log.e(TAG, "file " + path + " not found");  
  120.         } catch (IOException e) {  
  121.             Log.e(TAG, "file " + path + " not found");  
  122.         }  
  123.         return null;  
  124.     }  
  125. }  
  126. // ┏┓   ┏┓  
  127. // ┏┛┻━━━┛┻┓  
  128. // ┃       ┃    
  129. // ┃   ━   ┃  
  130. // ┃ ┳┛ ┗┳ ┃  
  131. // ┃       ┃  
  132. // ┃   ┻   ┃  
  133. // ┃       ┃  
  134. // ┗━┓   ┏━┛  
  135. // ┃   ┃ 神兽保佑          
  136. // ┃   ┃ 代码无BUG!  
  137. // ┃   ┗━━━┓  
  138. // ┃       ┣┓  
  139. // ┃       ┏┛  
  140. // ┗┓┓┏━┳┓┏┛  
  141. // ┃┫┫ ┃┫┫  
  142. // ┗┻┛ ┗┻┛  


2)  UseCameraActivity.java   

本类的功能是调用   生成图片拍摄后的路径  

        

照片生成的目录在 sd卡的/a/image/camera/.. .jpg

  

[java]  view plain  copy
  1. package com.example.camerabaozi;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5.   
  6. import android.app.Activity;  
  7. import android.content.Context;  
  8. import android.content.Intent;  
  9. import android.content.res.Configuration;  
  10. import android.net.Uri;  
  11. import android.os.Bundle;  
  12. import android.os.Environment;  
  13. import android.provider.MediaStore;  
  14. import android.util.Log;  
  15.   
  16. /** 
  17.  * 照片生成的目录在 sd卡的/a/image/camera/.. .jpg 
  18.  *  
  19.  * @author baozi 
  20.  *  
  21.  */  
  22. public class UseCameraActivity extends Activity {  
  23.     private String mImageFilePath;  
  24.     public static final String IMAGEFILEPATH = "ImageFilePath";  
  25.     public final static String IMAGE_PATH = "image_path";  
  26.     static Activity mContext;  
  27.     public final static int GET_IMAGE_REQ = 5000;  
  28.     private static Context applicationContext;  
  29.   
  30.     @Override  
  31.     protected void onCreate(Bundle savedInstanceState) {  
  32.         super.onCreate(savedInstanceState);  
  33.   
  34.         //判断 activity被销毁后 有没有数据被保存下来  
  35.         if (savedInstanceState != null) {  
  36.   
  37.             mImageFilePath = savedInstanceState.getString(IMAGEFILEPATH);  
  38.   
  39.             Log.i("123---savedInstanceState", mImageFilePath);  
  40.   
  41.             File mFile = new File(IMAGEFILEPATH);  
  42.             if (mFile.exists()) {  
  43.                 Intent rsl = new Intent();  
  44.                 rsl.putExtra(IMAGE_PATH, mImageFilePath);  
  45.                 setResult(Activity.RESULT_OK, rsl);  
  46.                 Log.i("123---savedInstanceState""图片拍摄成功");  
  47.                 finish();  
  48.             } else {  
  49.                 Log.i("123---savedInstanceState""图片拍摄失败");  
  50.             }  
  51.         }  
  52.   
  53.         mContext = this;  
  54.         applicationContext = getApplicationContext();  
  55.         if (savedInstanceState == null) {  
  56.             initialUI();  
  57.         }  
  58.   
  59.     }  
  60.   
  61.     public void initialUI() {  
  62.         //根据时间生成 后缀为  .jpg 的图片  
  63.         long ts = System.currentTimeMillis();  
  64.         mImageFilePath = getCameraPath() + ts + ".jpg";  
  65.         File out = new File(mImageFilePath);  
  66.         showCamera(out);  
  67.   
  68.     }  
  69.   
  70.     private void showCamera(File out) {  
  71.         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  72.         intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out)); // set  
  73.         startActivityForResult(intent, GET_IMAGE_REQ);  
  74.     }  
  75.   
  76.     @Override  
  77.     public void onActivityResult(int requestCode, int resultCode, Intent intent) {  
  78.   
  79.         if (GET_IMAGE_REQ == requestCode && resultCode == Activity.RESULT_OK) {  
  80.   
  81.             Intent rsl = new Intent();  
  82.             rsl.putExtra(IMAGE_PATH, mImageFilePath);  
  83.             mContext.setResult(Activity.RESULT_OK, rsl);  
  84.             mContext.finish();  
  85.   
  86.         } else {  
  87.             mContext.finish();  
  88.         }  
  89.     }  
  90.   
  91.     @Override  
  92.     protected void onDestroy() {  
  93.         super.onDestroy();  
  94.     }  
  95.   
  96.     @Override  
  97.     protected void onSaveInstanceState(Bundle outState) {  
  98.         super.onSaveInstanceState(outState);  
  99.         outState.putString("ImageFilePath", mImageFilePath + "");  
  100.   
  101.     }  
  102.   
  103.     @Override  
  104.     public void onConfigurationChanged(Configuration newConfig) {  
  105.         super.onConfigurationChanged(newConfig);  
  106.     }  
  107.   
  108.     @Override  
  109.     protected void onRestoreInstanceState(Bundle savedInstanceState) {  
  110.         super.onRestoreInstanceState(savedInstanceState);  
  111.   
  112.     }  
  113.   
  114.     public static String getCameraPath() {  
  115.         String filePath = getImageRootPath() + "/camera/";  
  116.         File file = new File(filePath);  
  117.         if (!file.isDirectory()) {  
  118.             file.mkdirs();  
  119.         }  
  120.         file = null;  
  121.         return filePath;  
  122.     }  
  123.   
  124.     public static String getImageRootPath() {  
  125.         String filePath = getAppRootPath() + "/image";  
  126.         File file = new File(filePath);  
  127.         if (!file.exists()) {  
  128.             file.mkdirs();  
  129.         }  
  130.         file = null;  
  131.         return filePath;  
  132.     }  
  133.   
  134.     public static String getAppRootPath() {  
  135.         String filePath = "/a";  
  136.         if (Environment.getExternalStorageState().equals(  
  137.                 Environment.MEDIA_MOUNTED)) {  
  138.             filePath = Environment.getExternalStorageDirectory() + filePath;  
  139.         } else {  
  140.             filePath = applicationContext.getCacheDir() + filePath;  
  141.         }  
  142.         File file = new File(filePath);  
  143.         if (!file.exists()) {  
  144.             file.mkdirs();  
  145.         }  
  146.         file = null;  
  147.         File nomedia = new File(filePath + "/.nomedia");  
  148.         if (!nomedia.exists())  
  149.             try {  
  150.                 nomedia.createNewFile();  
  151.             } catch (IOException e) {  
  152.             }  
  153.         return filePath;  
  154.     }  
  155.   
  156. }  
  157. //┏┓   ┏┓  
  158. //┏┛┻━━━┛┻┓  
  159. //┃       ┃    
  160. //┃   ━   ┃  
  161. //┃ ┳┛ ┗┳ ┃  
  162. //┃       ┃  
  163. //┃   ┻   ┃  
  164. //┃       ┃  
  165. //┗━┓   ┏━┛  
  166. //┃   ┃   神兽保佑          
  167. //┃   ┃   代码无BUG!  
  168. //┃   ┗━━━┓  
  169. //┃       ┣┓  
  170. //┃       ┏┛  
  171. //┗┓┓┏━┳┓┏┛  
  172. //  ┃┫┫ ┃┫┫  
  173. //  ┗┻┛ ┗┻┛  
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值