編輯:關於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
增加權限:
功能是: 根據指定的路徑 生成bitmap ; 顯示圖片
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; /** * 啟動界面 * * 照片生成的目錄在 sd卡的/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; private ImageView photo_iv; private ContentResolver mContentResolver; final int IMAGE_MAX_SIZE = 1024; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContentResolver = getContentResolver(); button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, UseCameraActivity.class); startActivityForResult(intent, REQCAMERA); } }); photo_iv = (ImageView) findViewById(R.id.imageView1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case REQCAMERA: String path = data.getStringExtra(UseCameraActivity.IMAGE_PATH); Log.i("123", path); // 根據照片的位置獲取圖片 Bitmap 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 = mContentResolver.openInputStream(uri); // Decode image size BitmapFactory.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))); } 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; } } // ┏┓ ┏┓ // ┏┛┻━━━┛┻┓ // ┃ ┃ // ┃ ━ ┃ // ┃ ┳┛ ┗┳ ┃ // ┃ ┃ // ┃ ┻ ┃ // ┃ ┃ // ┗━┓ ┏━┛ // ┃ ┃ 神獸保佑 // ┃ ┃ 代碼無BUG! // ┃ ┗━━━┓ // ┃ ┣┓ // ┃ ┏┛ // ┗┓┓┏━┳┓┏┛ // ┃┫┫ ┃┫┫ // ┗┻┛ ┗┻┛
本類的功能是調用 生成圖片拍攝後的路徑
照片生成的目錄在 sd卡的/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; /** * 照片生成的目錄在 sd卡的/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; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //判斷 activity被銷毀後 有沒有數據被保存下來 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", "圖片拍攝成功"); finish(); } else { Log.i("123---savedInstanceState", "圖片拍攝失敗"); } } mContext = this; applicationContext = getApplicationContext(); if (savedInstanceState == null) { initialUI(); } } public void initialUI() { //根據時間生成 後綴為 .jpg 的圖片 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)); // set startActivityForResult(intent, GET_IMAGE_REQ); } @Override public 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(); } } @Override protected void onDestroy() { super.onDestroy(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("ImageFilePath", mImageFilePath + ""); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); } @Override protected 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; } } //┏┓ ┏┓ //┏┛┻━━━┛┻┓ //┃ ┃ //┃ ━ ┃ //┃ ┳┛ ┗┳ ┃ //┃ ┃ //┃ ┻ ┃ //┃ ┃ //┗━┓ ┏━┛ //┃ ┃ 神獸保佑 //┃ ┃ 代碼無BUG! //┃ ┗━━━┓ //┃ ┣┓ //┃ ┏┛ //┗┓┓┏━┳┓┏┛ // ┃┫┫ ┃┫┫ // ┗┻┛ ┗┻┛
Demo 下載地址: http://download.csdn.net/detail/aaawqqq/7653475
祝福大家每日精進
謝謝
昨天的(今天凌晨)的博文《Android中Fragment和ViewPager那點事兒》中,我們通過使用Fragment和ViewPager模仿實現了微信的布局框架。今天
效果預覽Send:Click LocationMessage: 實現1:注冊高德地圖開發者賬號,創建應用、獲取高德地圖的 appkey2: jar 包建議直接從 融雲
render進程中 一.webkit模塊 webkit引擎會為網頁內容同時創建Dom tree, Render tree和RenderLayer tree. 這三棵樹之間
0、前言作為android開發人員,經常面對這樣的問題:網上下載的apk預制到系統,第三方oem提供編譯好的庫或者jar包等,你要將這些編制到你的系統中該如何做,那麼這就