整个过程中遇到的问题
一、 显示选择对话框,操作选择
问题1:为了解决Activity之间跳转,有黑色背景出现,添加了透明背景的Theme:windowIsTranslucent
问题2:添加后弹出框的title背景变黑(因为样式的parent不是AppBaseTheme后来改回来就好了)
问题3:选择图片后中间跳转到桌面,然后才弹回到activity
因为以上问题,Activity之间的跳转黑色背景问题没有解决
二、 裁切遇到的问题:裁切的时候outputX和outputY设置超过400,裁切后系统直接跳到应用首页
具体原因不知道,后来设置的值为300,程序没有问题了
三代码:
1 调用相机与相册选择弹出框
CharSequence[] items = {"相册", "相机" }; imageHeadChooseItem(items);
2 显示选择对话框,操作选择
/** * 显示选择对话框,操作选择 * * @param items */ public void imageHeadChooseItem(CharSequence[] items) { AlertDialog imageDialog = new AlertDialog.Builder( this) .setTitle(R.string.ui_insert_image) .setItems(items, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { // 手机选图 if (item == 0) { Intent intent = headImgChooser.getSelPictureIntent(); startActivityForResult( Intent.createChooser(intent, "选择图片"), ImageUtils.REQUEST_CODE_GETIMAGE_BYSDCARD); } // 拍照 else if (item == 1) { Intent intent = headImgChooser.getCameraIntent() ; startActivityForResult(intent, ImageUtils.REQUEST_CODE_GETIMAGE_BYCAMERA); } } }).create(); imageDialog.show(); }
3 透明背景样式:
<style name="Theme.HalfTranslucent" parent="AppBaseTheme"> <item name="android:windowBackground">@color/transparent</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowNoTitle">true</item> <item name="android:windowContentOverlay">@null</item> <!-- //对话框是否有遮盖 --> <item name="android:backgroundDimEnabled">true</item> </style>
4 图片选择工具类
/** * 图片选择工具类 * @author root * */ public class ImageChooser { private Context context ; private String savePath ; //拍照图片保存路径 ,无文件名 private String imgPath ; //拍照图片保存路径,包括文件名称 public ImageChooser(Context context ){ this.context = context; } //拍照获取头像 public Intent getCameraIntent() { savePath = getPhotoImgPath(); // 没有挂载SD卡,无法保存文件 if (StringUtils.isEmpty(savePath)) { Toast.makeText(context, "无法保存照片,请检查SD卡是否挂载", Toast.LENGTH_LONG).show(); return null; } String timeStamp = new SimpleDateFormat( "yyyyMMddHHmmss").format(new Date()); String fileName = timeStamp + ".jpg";// 照片命名 File out = new File(savePath, fileName); Uri uri = Uri.fromFile(out); imgPath = savePath + fileName;// 该照片的绝对路径 Intent intent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); return intent; } /** * 取出intent 从相册中选择图片 */ public Intent getSelPictureIntent(){ Intent intent; if (Build.VERSION.SDK_INT < 19) { intent = new Intent(); intent.setAction(Intent.ACTION_GET_CONTENT); } else { intent = new Intent( Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); } intent.setType("image/*"); return intent; } /** * 得到照相机图片存储位置 * @return */ public String getPhotoImgPath(){ String savePath = ""; // 判断是否挂载了SD卡 String storageState = Environment .getExternalStorageState(); if (storageState.equals(Environment.MEDIA_MOUNTED)) { savePath = Environment .getExternalStorageDirectory() + "/项目名称/Camera/";// 存放照片的文件夹 File savedir = new File(savePath); if (!savedir.exists()) { savedir.mkdirs(); } } return savePath; } /** * 得到剪切图片的intent * @return */ public static Intent getCropImgIntent(Uri uri,Integer aspectX, Integer aspectY,Integer outputX ,Integer outputY){ Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // crop为true是设置在开启的intent中设置显示的view可以剪裁 intent.putExtra("crop", "true"); //intent.putExtra("scale", true); // aspectX aspectY 是宽高的比例 intent.putExtra("aspectX", aspectX); intent.putExtra("aspectY", aspectY); // outputX,outputY 是剪裁图片的宽高 intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("return-data", true); intent.putExtra("noFaceDetection", true); return intent; } public String getImgPath() { return imgPath; } public void setImgPath(String imgPath) { this.imgPath = imgPath; } }
5 选择头像和背景的处理,并且选择后进行裁切
//选择头像和背景的处理,并且选择后进行裁切 @SuppressLint("HandlerLeak") @Override public void onActivityResult(final int requestCode, final int resultCode, final Intent imageReturnIntent) { if (resultCode != this.RESULT_OK) return; Uri uri; Intent intent ; switch (requestCode ) { case ImageUtils.REQUEST_CODE_GETIMAGE_BYSDCARD://头像请求相册 if(imageReturnIntent==null) return ; uri = imageReturnIntent.getData(); intent = ImageChooser.getCropImgIntent(uri,1,1,300,300); startActivityForResult(intent, ImageUtils.REQUEST_CODE_GETIMAGE_BYCROP); break; case ImageUtils.REQUEST_CODE_GETIMAGE_BYCAMERA://头像请求相机 //拍照的时候 imageReturnIntent 为空 if(!StringUtils.isEmpty( headImgChooser.getImgPath())){ //跳转图片裁切 //uri = Uri.parse(headImgChooser.getImgPath()); uri = Uri.fromFile(new File(headImgChooser.getImgPath())); intent = ImageChooser.getCropImgIntent(uri,1,1,300,300); startActivityForResult(intent, ImageUtils.REQUEST_CODE_GETIMAGE_BYCROP); } break; case ImageUtils.REQUEST_CODE_GETIMAGE_BYCROP: //头像裁切结束 Bundle bundle = imageReturnIntent.getExtras(); if(bundle!=null){ Bitmap photo = bundle.getParcelable( "data"); if(photo!=null){ //这里上传头像photo upLoadHeadImageHttp(photo); img_my_head.setImageBitmap(photo ); } } break; case ImageUtils.REQUEST_CODE_GETIMAGE_BYCAMERA_BG://背景 照相 //拍照的时候 imageReturnIntent 为空 if(!StringUtils.isEmpty( bgImgChooser.getImgPath())){ //跳转图片裁切 uri = Uri.fromFile(new File(bgImgChooser.getImgPath())); intent = ImageChooser.getCropImgIntent(uri,3,2,300,200); startActivityForResult(intent, ImageUtils.REQUEST_CODE_GETIMAGE_BYCROP_BG1); } break; case ImageUtils.REQUEST_CODE_GETIMAGE_BYSDCARD_BG://选择照片 if(imageReturnIntent==null) return ; uri = imageReturnIntent.getData(); intent = ImageChooser.getCropImgIntent(uri,3,2,300,200); startActivityForResult(intent, ImageUtils.REQUEST_CODE_GETIMAGE_BYCROP_BG1); break ; case ImageUtils.REQUEST_CODE_GETIMAGE_BYCROP_BG1://背景图片裁切 Bundle bundle1 = imageReturnIntent.getExtras(); if(bundle1!=null){ Bitmap photo = bundle1.getParcelable( "data"); if(photo!=null){ //保存背景图片 saveBGImage(photo); } } break; default: break; } }
6 保存背景图片到缓存中
/** * 保存背景图片到缓存中 * @param bgImgPath */ private void saveBGImage(Bitmap bgImg ){ try { ImageUtils.saveImage(this, bgImgName, ImageUtils.compressImage(bgImg) ); sharePre.putString( SharedPreferencesUtils.BG_IMAGE, bgImgName ); headLayout.setBackground( ImageUtils.getDrawable(this, bgImgName)); } catch (IOException e) { e.printStackTrace(); } }
7 图片按比例压缩,压缩到100k左右
public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中 int options = 100; while ( baos.toByteArray().length / 1024>100) { //循环判断如果压缩后图片是否大于100kb,大于继续压缩 baos.reset();//重置baos即清空baos image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中 options -= 10;//每次都减少10 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中 Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片 return bitmap; }
8 写图片文件 在Android系统中,文件保存在 /data/data/PACKAGE_NAME/files 目录下
/** * 写图片文件 在Android系统中,文件保存在 /data/data/PACKAGE_NAME/files 目录下 * * @throws IOException */ public static void saveImage(Context context, String fileName, Bitmap bitmap) throws IOException { saveImage(context, fileName, bitmap, 100); } public static void saveImage(Context context, String fileName, Bitmap bitmap, int quality) throws IOException { if (bitmap == null || fileName == null || context == null) return; FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.JPEG, quality, stream); byte[] bytes = stream.toByteArray(); fos.write(bytes); fos.close(); }