/** * Created by pc on 2016/5/10. * 选择照片的基类 */public class photoActivity extends BaseActivity implements View.OnClickListener { private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照 private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择 private static final int PHOTO_REQUEST_CUT = 3;// 结果 private File tempFile; private String saveBmpToSd_path; /** * 截屏比例 */ private int scale = 1; /** * 储存图片路径 */ private HashMap<String,String> pathMap = new HashMap<>(); private ImageView iv_photo; private String mapKey; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initDialog(); } private Dialog dialog; private void initDialog(){ dialog = new Dialog(this, R.style.dialog); dialog.setContentView(R.layout.dialog_layout); Window dialogWindow = dialog.getWindow(); dialog.setCanceledOnTouchOutside(true);// 设置点击屏幕Dialog消失 dialogWindow.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); WindowManager.LayoutParams lp = dialogWindow.getAttributes(); dialogWindow.setGravity(Gravity.BOTTOM); dialogWindow.setWindowAnimations(R.style.dialogWindowAnim); // 设置窗口弹出动画 TextView tv_photo = (TextView) dialog.findViewById(R.id.tv_photo); tv_photo.setOnClickListener(this); TextView tv_camera = (TextView) dialog.findViewById(R.id.tv_camera); tv_camera.setOnClickListener(this); TextView tv_close = (TextView) dialog.findViewById(R.id.tv_close); tv_close.setOnClickListener(this); String DIR = Environment.getExternalStorageDirectory()+"/DCIM/Camera"; File dirPath = new File(DIR); if (!dirPath.exists()) { dirPath.mkdirs(); }// String name = getPhotoFileName();// photoPath = DIR+"/"+name; tempFile = new File(Environment.getExternalStorageDirectory()+"/DCIM/Camera",getPhotoFileName()); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.tv_photo: intent = new Intent(Intent.ACTION_PICK, null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*"); startActivityForResult(intent, PHOTO_REQUEST_GALLERY); dialog.dismiss(); break; case R.id.tv_camera: intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 打开相机 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile)); startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO);///mnt/sdcard/DCIM//IMG_20151127_143744.jpg dialog.dismiss(); break; case R.id.tv_close: dialog.dismiss(); break; default: break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.e("onActivityResult==>", "" + "running"); switch (requestCode) { case PHOTO_REQUEST_TAKEPHOTO: startPhotoZoom(Uri.fromFile(tempFile), 150); break; case PHOTO_REQUEST_GALLERY: if (data != null) startPhotoZoom(data.getData(), 150); break; case PHOTO_REQUEST_CUT: if (data != null) setPicToView(data); break; default: break; } } private void startPhotoZoom(Uri uri, int size) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // crop为true是设置在开启的intent中设置显示的view可以剪裁 intent.putExtra("crop", "true"); // aspectX aspectY 是宽高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", scale); intent.putExtra("noFaceDetection", true);// 取消人脸识别(直接把截取框放到脸部) // outputX,outputY 是剪裁图片的宽高 intent.putExtra("outputX", size); intent.putExtra("outputY", size); intent.putExtra("return-data", true); // 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_CUT startActivityForResult(intent, PHOTO_REQUEST_CUT); } //将进行剪裁后的图片显示到UI界面上 private void setPicToView(Intent picdata) { Bundle bundle = picdata.getExtras(); if (bundle != null) { Bitmap photo = bundle.getParcelable("data"); if(photo == null){ String url = bundle.getString("croppedpath"); if(ImageHelper.Exist(url)){ photo = ImageHelper.getimage(url, 100); } } if(photo != null){ // imgs[index].setImageBitmap(photo);// tvphotos[index].setVisibility(View.GONE); //压缩图片 Bitmap sbitmap = ImageHelper.compressImage(photo, 50);// Bitmap sbitmap = BitmapUtil.comp(photo); iv_photo.setImageBitmap(sbitmap);// //保存图片 String filename = getPhotoFileName(); saveBmpToSd_path = ImageHelper.saveBmpToSd(sbitmap, filename , 100); if(ImageHelper.Exist(saveBmpToSd_path)){ pathMap.put(mapKey, saveBmpToSd_path); photo.recycle(); photo = null ;// sbitmap.recycle();// sbitmap = null ; } }else { ShowToase.showToast(this, "图片截取异常"); } } } // 使用系统当前日期加以调整作为照片的名称 private String getPhotoFileName() { Date date = new Date(System.currentTimeMillis()); SimpleDateFormat dateFormat = new SimpleDateFormat("‘IMG‘_yyyyMMdd_HHmmss"); return dateFormat.format(date) + ".jpg"; } /** * 上传是调用 * @return */ protected HashMap<String,String> getMap(){ return pathMap ; }; /** * 显示 * @param img * @param key 服务端要求的key字段 */ protected void showDialog(ImageView img ,String key){ iv_photo= img; mapKey= key; dialog.show();; } }
<!--普通dialog--><style name="dialog"> <item name="android:windowFrame">@null</item> <!--Dialog的windowFrame框为无 --> <item name="android:windowIsFloating">true</item> <!--是否浮现在activity之上 --> <item name="android:windowIsTranslucent">true</item> <!-- 是否半透明 --> <item name="android:windowNoTitle">true</item> <item name="android:backgroundDimEnabled">true</item> <item name="android:windowBackground">@android:color/transparent</item></style>
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout_root" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/ll_dialog" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center" android:background="@color/blue"> <TextView android:id="@+id/tv_photo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="相册" android:textColor="@color/white" android:padding="15dip" android:gravity="center"/> <View android:layout_width="fill_parent" android:layout_height="0.5dip" android:background="#c2c2c2"/> <TextView android:id="@+id/tv_camera" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="拍照" android:textColor="@color/white" android:padding="15dip" android:gravity="center"/> <View android:layout_width="fill_parent" android:layout_height="0.5dip" android:background="#c2c2c2"/> <TextView android:id="@+id/tv_close" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="取消" android:textColor="@color/white" android:padding="15dip" android:gravity="center"/> </LinearLayout> </RelativeLayout>
时间: 2024-10-16 15:14:31