1.自定义布局 点击弹框内按钮 调用系统相机相册
View p = View.inflate(Personal_Data.this, R.layout.popchang, null);
final PopupWindow popupWindow = new PopupWindow(p, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
popupWindow.setBackgroundDrawable(getResources().getDrawable(
R.mipmap.ic_launcher));
popupWindow.showAtLocation(personalDataImage, Gravity.RIGHT | Gravity.BOTTOM, 10, 10);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
Button cream = p.findViewById(R.id.cream);
Button phone = p.findViewById(R.id.phone);
Button centel = p.findViewById(R.id.centel);
cream.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PermissionsUtils.getInstance().chekPermissions(Personal_Data.this, permiss, new PermissionsUtils.IPermissionsResult() {
@Override
public void passPermissons() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.addCategory("android.intent.category.DEFAULT");
startActivityForResult(intent, 100);
}
@Override
public void forbitPermissons() {
}
});
}
});
phone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PermissionsUtils.getInstance().chekPermissions(Personal_Data.this, permiss, new PermissionsUtils.IPermissionsResult() {
@Override
public void passPermissons() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 99);
}
@Override
public void forbitPermissons() {
}
});
}
});
centel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
2.onActivityResult内的内容
@Override
public void onActivityResult(final int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//获取图片路径
if (data == null) {
return;
}
if (requestCode == 100) {
Bitmap bitmap = data.getParcelableExtra("data");
personalDataImage.setImageBitmap(bitmap);
Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, null, null));
addHeaderPhoto(uri);
}
if (requestCode == 99) {
Uri uri = data.getData();
addHeaderPhoto(uri);
personalDataImage.setImageURI(uri);
}
3.工具类 url转换成file类型的文件
//工具类 url转换成file类型的文件
private void addHeaderPhoto(Uri uri) {
String[] filePathColumn = {MediaStore.Images.Media.DATA};
//游标
Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String string = cursor.getString(columnIndex);
//把获取到的文件转换类型
File file = new File(string);
/*RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpg"), file);
MultipartBody.Part part = MultipartBody.Part.createFormData("image", file.getName(), requestBody);*/
OkGo.<String>post(Constant.IMAGE_URL)
.headers("userId", userId)
.headers("sessionId", sessionId)
.params("image", file)
//.tag(this)
.execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
String json = response.body().toString();
Toast.makeText(Personal_Data.this, "requestCode:" + json, Toast.LENGTH_SHORT).show();
}
});
}
设置背景颜色工具类
/**
* PopupWindow的背景色
*/
public class TipsUtil {
//PopupWindow的背景
public static void setBackgroundAlpha(AppCompatActivity activity, float bgAlpha) {
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
lp.alpha = bgAlpha;
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
activity.getWindow().setAttributes(lp);
}
}
//调用方法
TipsUtil.setBackgroundAlpha(AddPeriodical.this, 1.0f);