打开系统图库
来源:互联网 发布:测试工具 python 编辑:程序博客网 时间:2024/06/06 07:26
public class RegisterActivity extends Activity { private static final int XIANGCE_CODE = 2; private static final int PHOTO_CODE = 1; private static final int PHOTO_REQUEST_CUT = 3; private ImageView upLoadHead; private EditText phone,verify,password,passwordCheck; private File photoFile; private Dialog photoDialog; private boolean issaved; private String picpath; private Bitmap bit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_register); Util.setTnit(this); init(); } private void init() { OnClickListener onClick = new MyOnClickListener(); upLoadHead = (ImageView) findViewById(R.id.uploadhead); phone = (EditText) findViewById(R.id.phone); verify = (EditText) findViewById(R.id.verify); password = (EditText) findViewById(R.id.password); passwordCheck = (EditText) findViewById(R.id.passwordcheck); upLoadHead.setOnClickListener(onClick); } public void register(View v){ } private class MyOnClickListener implements OnClickListener{ @Override public void onClick(View v) { switch (v.getId()) { case R.id.uploadhead: View view = LayoutInflater.from(RegisterActivity.this).inflate(R.layout.uploadphotos_popwindow, null); TextView photograph = (TextView) view.findViewById(R.id.photograph); photograph.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { photoDialog.dismiss(); photoFile = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg"); //调用系统的拍照功能 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //指定调用相机拍照后照片的存储路径 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); startActivityForResult(intent, PHOTO_CODE); } }); TextView localphoto = (TextView) view.findViewById(R.id.localphoto); localphoto.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { photoDialog.dismiss(); //打开系统图库 Intent intent = new Intent(Intent.ACTION_PICK,null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(intent, XIANGCE_CODE); } }); photoDialog = new Dialog(RegisterActivity.this,R.style.baseDialog); photoDialog.setContentView(view); photoDialog.show(); break; } } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case PHOTO_CODE: startPhotoZoom(Uri.fromFile(photoFile), 200); break; case XIANGCE_CODE: if (data != null) startPhotoZoom(data.getData(), 200); break; case PHOTO_REQUEST_CUT: if(data != null){ setPicToView(data); } break; } super.onActivityResult(requestCode, resultCode, data); } //将进行剪裁后的图片显示到UI界面上 private void setPicToView(Intent data) { Bundle bundle = data.getExtras(); if(bundle!=null){ Bitmap photo = bundle.getParcelable("data"); Bitmap bitmap = comp(photo); if(bitmap== null){ return; } upLoadHead.setImageBitmap(toRoundCorner(bitmap,8)); issaved = saveFile(bitmap); } } private boolean saveFile(Bitmap bitmap) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); picpath = Environment.getExternalStorageDirectory()+File.separator+"goloveUploadheadimage.jpg"; bit.compress(Bitmap.CompressFormat.PNG, 100, baos); File file = new File(picpath); try { if(file.exists()){ file.delete(); file.createNewFile(); }else{ file.createNewFile(); } FileOutputStream fos = new FileOutputStream(file); fos.write(baos.toByteArray()); fos.flush(); fos.close(); return true; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); picpath = ""; return false; } } //设置上传头像的圆角图片 private Bitmap toRoundCorner(Bitmap bitmap, int pixels) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),Config.ARGB_8888); Canvas canvas =new Canvas(); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0,0,bitmap.getWidth(),bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = pixels; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; } //压缩图片(第一次) private Bitmap comp(Bitmap image) { if(image == null){ return null; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.PNG, 100, baos); if( baos.toByteArray().length / 1024>200) {//判断如果图片大于200KB,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出 baos.reset();//重置baos即清空baos image.compress(Bitmap.CompressFormat.PNG, 50, baos);//这里压缩50%,把压缩后的数据存放到baos中 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); BitmapFactory.Options newOpts = new BitmapFactory.Options(); //开始读入图片,此时把options.inJustDecodeBounds 设回true了 newOpts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts); newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight; //现在主流手机比较多是800*480分辨率,所以高和宽我们设置为 float hh = 1280f;//这里设置高度为800f float ww = 720f;//这里设置宽度为480f //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可 int be = 1;//be=1表示不缩放 if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放 be = (int) (newOpts.outWidth / ww); } else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放 be = (int) (newOpts.outHeight / hh); } if (be <= 0) be = 1; newOpts.inSampleSize = be;//设置缩放比例 //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了 isBm = new ByteArrayInputStream(baos.toByteArray()); bitmap = BitmapFactory.decodeStream(isBm, null, newOpts); return compressImage(bitmap);//压缩好比例大小后再进行质量压缩 } //压缩图片(第二次) private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.PNG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中 int options = 100; while (baos.toByteArray().length / 1024 > 200) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩 baos.reset();// 重置baos即清空baos image.compress(Bitmap.CompressFormat.PNG, 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; } private void startPhotoZoom(Uri uri, int size) { //调用系统裁剪图片 Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/jpg"); // crop为true是设置在开启的intent中设置显示的view可以剪裁 intent.putExtra("crop", true); // aspectX aspectY 是宽高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX,outputY 是剪裁图片的宽高 intent.putExtra("outputX", size); intent.putExtra("outputY", size); intent.putExtra("return-data", true); startActivityForResult(intent, PHOTO_REQUEST_CUT); }}
0 0
- 打开系统图库
- IOS 获取系统图库与打开拍照
- android打开系统图库终极适配
- android 打开系统图库的方法
- Android 打开系统图库实现方法
- 打开图库
- android 调用系统图库打开指定路径的图片
- Android启动系统相机,打开图库选择图片
- Swift 打开图库
- 打开手机图库界面
- 系统图库分享到app、调用自己的音乐app打开MP3
- 打开系统的图库,返回图片并处理图片以防止内存溢出。
- UIPickerViewController 打开相机 图库 相册
- Android 刷新系统图库
- 秒杀系统图库
- 提醒系统更新图库
- 通知系统图库刷新
- 保存图片到系统图库,截图保存到系统图库
- 如何评价 APICloud ?
- 输入挂
- MAC快捷键
- AIDL 实现 IPC(进程间通信)
- 开发中我用到的调试方法
- 打开系统图库
- okhttp+fastjson+三套环境的android—demo
- kafka安装
- java 多线程相关知识
- 第六周书面项目2-变量的变化(1)
- C和C++的混合编译--extern “C”的使用
- NLTK vs Sklearn vs Gensim
- shiro
- sqoop参数类型影响