点击头像弹出
自定义dialog
private void alertDialog() {
final String[] items = { "相册","相机" };
AlertDialog.Builder listDialog =
new AlertDialog.Builder(getContext());
listDialog.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case 0:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent,200);
break;
case 1:
Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent1.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(path)));
startActivityForResult(intent1,300);
break;
}
}
}).show();
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==300&&resultCode==RESULT_OK){
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(Uri.fromFile(new File(path)),"image/*");
intent.putExtra("CROP",true);
intent.putExtra("aspectX",1);
intent.putExtra("aspectY",1);
intent.putExtra("outputX",100);
intent.putExtra("outputX",100);
intent.putExtra("return-data",true);
startActivityForResult(intent,400);
}
if(requestCode==200&&resultCode==RESULT_OK){
Intent intent = new Intent("com.android.camera.action.CROP");
Uri uri = data.getData();
intent.setDataAndType(uri,"image/*");
intent.putExtra("CROP",true);
intent.putExtra("aspectX",1);
intent.putExtra("aspectY",1);
intent.putExtra("outputX",100);
intent.putExtra("outputX",100);
intent.putExtra("return-data",true);
startActivityForResult(intent,400);
}
if(requestCode==400&&resultCode==RESULT_OK){
Bitmap bitmap = data.getParcelableExtra("data");
File file=new File(path);//将要保存图片的路径
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file.getName()));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Map<String,String> params=new HashMap<>();
params.put("file",path);
iPersenter.postFiles(dataUrl,params,FliesBean.class);
}
点击头像弹出popwindow
private void popWindow() {
View view=View.inflate(getContext(),R.layout.image,null);
TextView text_camcme = view.findViewById(R.id.cancme);
TextView text_pick = view.findViewById(R.id.pick);
popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.showAtLocation(view,
Gravity.BOTTOM, 0, 0);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setTouchable(true);
text_camcme.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)));
startActivityForResult(intent, 400);
popupWindow.dismiss();
}
});
text_pick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 300);
popupWindow.dismiss();
}
});
}
回调
//相机
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==400&&resultCode==RESULT_OK) {
//打开裁剪
Intent intent = new Intent("com.android.camera.action.CROP");
//将图片设置给裁剪
intent.setDataAndType(Uri.fromFile(new File(path)), "image/*");
//设置是否支持裁剪
intent.putExtra("CROP", true);
//设置宽高比
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
//设置输出后图片大小
intent.putExtra("outputX", 100);
intent.putExtra("outputY", 100);
//返回到data
intent.putExtra("return-data", true);
//启动
startActivityForResult(intent, 200);
}
//相册
if (requestCode==300&&resultCode==RESULT_OK) {
//打开裁剪
Intent intent = new Intent("com.android.camera.action.CROP");
Uri uri=data.getData();
//将图片设置给裁剪
intent.setDataAndType(uri, "image/*");
//设置是否可裁剪
intent.putExtra("CROP", true);
//设置宽高比
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
//设置输出
intent.putExtra("outputX", 100);
intent.putExtra("outputY", 100);
//返回data
intent.putExtra("return-data", true);
startActivityForResult(intent, 200);
}
if (requestCode==200&&resultCode==RESULT_OK) {
Bitmap bitmap = data.getParcelableExtra("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 100;
while (baos.toByteArray().length / 1024 > 500) { //循环判断如果压缩后图片是否大于500kb,大于继续压缩
baos.reset();//重置baos即清空baos
options -= 10;//每次都减少10
bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
}
File file = new File(files);
try {
FileOutputStream fos = new FileOutputStream(file);
try {
fos.write(baos.toByteArray());
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
recycleBitmap(bitmap);
}
Map<String,String> params = new HashMap<>();
//TODO 头像路径
params.put("image",files);
iPersenter.postFiles(Apis.TYPR_INMAGE_XINXI,params,HeadPicBean.class);
}
public static void recycleBitmap(Bitmap... bitmaps) {
if (bitmaps==null) {
return;
}
for (Bitmap bm : bitmaps) {
if (null != bm && !bm.isRecycled()) {
bm.recycle();
}
}
}