android照相、相冊獲取圖片剪裁報(bào)錯(cuò)的解決方法
這是調(diào)用相機(jī)
public static File getImageFromCamer(Context context, File cameraFile, int REQUE_CODE_CAMERA, Intent intent) { intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File fileDir = HelpUtil.getFile(context, "/Tour/user_photos"); cameraFile = new File(fileDir.getAbsoluteFile() + "/" + System.currentTimeMillis() + ".jpg"); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraFile)); ((Activity) context).startActivityForResult(intent, REQUE_CODE_CAMERA); return cameraFile; }
在這里我返回了一個(gè)file對象,這是應(yīng)為項(xiàng)目中需要,大家可以不必真寫,直接傳一個(gè)Uri對象過來就好了
下面是調(diào)用相冊
public static void getImageFromPhoto(Context context, int REQUE_CODE_PHOTO) { Intent intent = new Intent(Intent.ACTION_PICK, null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); ((Activity) context).startActivityForResult(intent, REQUE_CODE_PHOTO); }
當(dāng)然接下來是調(diào)用Activity的OnActivityResult了
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { switch (requestCode) { case ConstantUtil.REQUE_CODE_CAMERA: uri = Uri.fromFile(cameraFile); PhotoUtil.startPhotoZoom(context, uri, ConstantUtil.REQUE_CODE_CROP); break; case ConstantUtil.REQUE_CODE_PHOTO: if (null != data) {//為了取消選取不報(bào)空指針用的 uri = data.getData(); PhotoUtil.startPhotoZoom(context, uri, ConstantUtil.REQUE_CODE_CROP); } break; case ConstantUtil.REQUE_CODE_CROP: if(uri==null){ break; } cropBitmap=HelpUtil.getBitmapFromUri(uri,context); if (cropBitmap != null) { iv_headphoto.setImageBitmap(cropBitmap); baos = new ByteArrayOutputStream(); cropBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); headPicString = new String(Base64.encode( baos.toByteArray(), 0)); UploadPic(headPicString); } break; default: break; } }
當(dāng)然還有大家關(guān)心的剪切
public static void startPhotoZoom(Context context, Uri uri, int REQUE_CODE_CROP) { int dp = 500; Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // 下面這個(gè)crop=true是設(shè)置在開啟的Intent中設(shè)置顯示的VIEW可裁剪 intent.putExtra("crop", "true"); intent.putExtra("scale", true);// 去黑邊 intent.putExtra("scaleUpIfNeeded", true);// 去黑邊 // aspectX aspectY 是寬高的比例 intent.putExtra("aspectX", 1);//輸出是X方向的比例 intent.putExtra("aspectY", 1); // outputX outputY 是裁剪圖片寬高,切忌不要再改動(dòng)下列數(shù)字,會卡死 intent.putExtra("outputX", dp);//輸出X方向的像素 intent.putExtra("outputY", dp); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); intent.putExtra("return-data", false);//設(shè)置為不返回?cái)?shù)據(jù) ((Activity) context).startActivityForResult(intent, REQUE_CODE_CROP); }
在很多博客中都把“return-data”設(shè)置為了true然后在onActivityResult中通過data.getParcelableExtra("data")來獲取數(shù)據(jù),不過這樣的話dp這個(gè)變量的值就不能太大了,不然你的程序就掛了。這里也就是我遇到問題的地方了,在大多數(shù)高配手機(jī)上這樣用是沒有問題的,不過很多低配手機(jī)就有點(diǎn)hold不住了,直接就異常了,包括我們的國產(chǎn)神機(jī)米3也沒能hold住,所以我建議大家不要通過return data 大數(shù)據(jù),小數(shù)據(jù)還是沒有問題的,說以我們在剪切圖片的時(shí)候就盡量使用Uri這個(gè)東東來幫助我們。
下面是我們進(jìn)行剪裁用到的一些參數(shù)
Exta Options Table for image/* crop:
SetExtra | DataType | Description |
crop | String | Signals the crop feature |
aspectX | int | Aspect Ratio |
aspectY | int | Aspect Ratio |
outputX | int | width of output created from this Intent |
outputY | int | width of output created from this Intent |
scale | boolean | should it scale |
return-data | boolean | Return the bitmap with Action=inline-data by using the data |
data | Parcelable | Bitmap to process, you may provide it a bitmap (not tested) |
circleCrop | String | if this string is not null, it will provide some circular cr |
MediaStore.EXTRA_OUTPUT ("output") | URI | Set this URi to a File:///, see example code |
最后把通過Uri獲得bitmap的方法給大家貼上
public static Bitmap getBitmapFromUri(Uri uri,Context mContext) { try { // 讀取uri所在的圖片 Bitmap bitmap = MediaStore.Images.Media.getBitmap(mContext.getContentResolver(), uri); return bitmap; } catch (Exception e) { e.printStackTrace(); return null; } }
- android 調(diào)用系統(tǒng)的照相機(jī)和圖庫實(shí)例詳解
- Android自定義照相機(jī)Camera出現(xiàn)黑屏的解決方法
- android 照相功能的簡單實(shí)例
- Android 簡單的照相機(jī)程序的實(shí)例代碼
- Android自定義照相機(jī)詳解
- 淺談Android 照相機(jī)權(quán)限的聲明
- Android 實(shí)現(xiàn)調(diào)用系統(tǒng)照相機(jī)拍照和錄像的功能
- Android 實(shí)現(xiàn)IOS選擇拍照相冊底部彈出的實(shí)例
- Android調(diào)用系統(tǒng)照相機(jī)拍照與攝像的方法
- Android實(shí)現(xiàn)簡單的照相功能
相關(guān)文章
android ftp上傳功能實(shí)現(xiàn)步驟
在本篇內(nèi)容里小編給大家分享的是關(guān)于android ftp上傳功能實(shí)現(xiàn)步驟內(nèi)容,需要的朋友們可以學(xué)習(xí)下。2018-12-12Android中判斷網(wǎng)絡(luò)連接是否可用及監(jiān)控網(wǎng)絡(luò)狀態(tài)
獲取網(wǎng)絡(luò)信息需要在AndroidManifest.xml文件中加入相應(yīng)的權(quán)限,接下來詳細(xì)介紹Android中判斷網(wǎng)絡(luò)連接是否可用及監(jiān)控網(wǎng)絡(luò)狀態(tài),感興趣的朋友可以參考下2012-12-12ShareSDK造成App崩潰的一個(gè)BUG原因分析以及Fix方法
這篇文章主要介紹了ShareSDK造成App崩潰的一個(gè)BUG原因分析以及Fix方法,使用的是Cocos2d-x專用ShareSDK組件,需要的朋友可以參考下2014-04-04利用Android中的TextView實(shí)現(xiàn)逐字顯示動(dòng)畫
在安卓程序啟動(dòng)的時(shí)候,想逐字顯示一段話,每個(gè)字都有一個(gè)從透明到不透明的漸變動(dòng)畫。那如何顯示這個(gè)效果,下面一起來看看。2016-08-08基于Android中手勢交互的實(shí)現(xiàn)方法
本篇文章是對Android中手勢交互的實(shí)現(xiàn)進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05詳解Android中ListView實(shí)現(xiàn)圖文并列并且自定義分割線(完善仿微信APP)
本篇文章主要介紹了Android中ListView實(shí)現(xiàn)圖文并列并且自定義分割線(完善仿微信APP),具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12AndroidStudio項(xiàng)目打包成jar的簡單方法
JAR(Java Archive,Java 歸檔文件)是與平臺無關(guān)的文件格式,它允許將許多文件組合成一個(gè)壓縮文件,在eclipse中我們知道如何將一個(gè)項(xiàng)目導(dǎo)出為jar包,供其它項(xiàng)目使用呢?下面通過本文給大家介紹ndroidStudio項(xiàng)目打包成jar的簡單方法,需要的朋友參考下吧2017-11-11讓Android應(yīng)用不被殺死(killer)的方法
這篇文章主要介紹了讓Android應(yīng)用不被殺死(killer)的方法,本文講解了實(shí)現(xiàn)方法和原理分析,需要的朋友可以參考下2015-04-04