1.定义权限
String[] permiss={Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE};
2.点击按钮中,展开popwindow
tou2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View p = View.inflate(PersonalDataActivity.this, R.layout.popchange, 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(v, Gravity.RIGHT | Gravity.BOTTOM, 10,10);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
Button cream = p.findViewById(R.id.cream);
Button photo = p.findViewById(R.id.photo);
Button centel = p.findViewById(R.id.centel);
//相机点击
cream.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PermissionsUtils.getInstance().chekPermissions(PersonalDataActivity.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, 101);
popupWindow.dismiss();
}
@Override
public void forbitPermissons() {
}
});
}
});
//图片点击
photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PermissionsUtils.getInstance().chekPermissions(PersonalDataActivity.this, permiss, new PermissionsUtils.IPermissionsResult() {
@Override
public void passPermissons() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 202);
popupWindow.dismiss();
}
@Override
public void forbitPermissons() {
}
});
}
});
//取消
centel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
}
});
3.重写onActivityResult方法
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
Bitmap bitmap = data.getParcelableExtra("data");
tou2.setImageBitmap(bitmap);
//将bitmap类型转换成uri
Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, null, null));
addHeaderPhoto(uri);
}
if (requestCode == 202) {
Uri uri = data.getData();
addHeaderPhoto(uri);
tou2.setImageURI(uri);
}
}
4.定义一个addHeaderPhoto方法
private void addHeaderPhoto(Uri uri) {
//将uri类型转换成file
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);
//将file类型转换成part
RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpg"), file);
MultipartBody.Part part = MultipartBody.Part.createFormData("image", file.getName(), requestBody);
Map<String, String> map = new HashMap<>();
map.put("userId",userId+"");
map.put("sessionId",sessionId);
//Retrofit解析
RetrofitUtils.getGetInstance().upLoadImage(InterFace.CHANGEIMG_URL,map,part).setOkHttpListener(new RetrofitUtils.OkHttpListener() {
@Override
public void success(String jsons) {
Gson gson=new Gson();
UpdataImgBean updataImgBean = gson.fromJson(jsons, UpdataImgBean.class);
Toast.makeText(PersonalDataActivity.this, "嘿!" + updataImgBean.getMessage(), Toast.LENGTH_SHORT).show();
Log.d( "111222",updataImgBean.getHeadPath());
}
@Override
public void error(String error) {
}
});
}
5.popwindow布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/cream"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="相机"
android:background="@drawable/myline"
/>
<Button
android:id="@+id/photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="相册"
android:background="@drawable/myline"/>
<Button
android:id="@+id/centel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消"
android:background="@drawable/myline"/>
</LinearLayout>
6.权限工具类6.0以上
/**
* 权限工具类
*/
public class PermissionsUtils {
private final int mRequestCode = 100;//权限请求码
public static boolean showSystemSetting = true;
private PermissionsUtils() {
}
private static PermissionsUtils permissionsUtils;
private IPermissionsResult mPermissionsResult;
public static PermissionsUtils getInstance() {
if (permissionsUtils == null) {
permissionsUtils = new PermissionsUtils();
}
return permissionsUtils;
}
public void chekPermissions(Activity context, String[] permissions, @NonNull IPermissionsResult permissionsResult) {
mPermissionsResult = permissionsResult;
if (Build.VERSION.SDK_INT < 23) {//6.0才用动态权限
permissionsResult.passPermissons();
return;
}
//创建一个mPermissionList,逐个判断哪些权限未授予,未授予的权限存储到mPerrrmissionList中
List<String> mPermissionList = new ArrayList<>();
//逐个判断你要的权限是否已经通过
for (int i = 0; i < permissions.length; i++) {
if (ContextCompat.checkSelfPermission(context, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
mPermissionList.add(permissions[i]);//添加还未授予的权限
}
}
//申请权限
if (mPermissionList.size() > 0) {//有权限没有通过,需要申请
ActivityCompat.requestPermissions(context, permissions, mRequestCode);
} else {
//说明权限都已经通过,可以做你想做的事情去
permissionsResult.passPermissons();
return;
}
}
//请求权限后回调的方法
//参数: requestCode 是我们自己定义的权限请求码
//参数: permissions 是我们请求的权限名称数组
//参数: grantResults 是我们在弹出页面后是否允许权限的标识数组,数组的长度对应的是权限名称数组的长度,数组的数据0表示允许权限,-1表示我们点击了禁止权限
public void onRequestPermissionsResult(Activity context, int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
boolean hasPermissionDismiss = false;//有权限没有通过
if (mRequestCode == requestCode) {
for (int i = 0; i < grantResults.length; i++) {
if (grantResults[i] == -1) {
hasPermissionDismiss = true;
}
}
//如果有权限没有被允许
if (hasPermissionDismiss) {
if (showSystemSetting) {
showSystemPermissionsSettingDialog(context);//跳转到系统设置权限页面,或者直接关闭页面,不让他继续访问
} else {
mPermissionsResult.forbitPermissons();
}
} else {
//全部权限通过,可以进行下一步操作。。。
mPermissionsResult.passPermissons();
}
}
}
/**
* 不再提示权限时的展示对话框
*/
AlertDialog mPermissionDialog;
private void showSystemPermissionsSettingDialog(final Activity context) {
final String mPackName = context.getPackageName();
if (mPermissionDialog == null) {
mPermissionDialog = new AlertDialog.Builder(context)
.setMessage("已禁用权限,请手动授予")
.setPositiveButton("设置", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
cancelPermissionDialog();
Uri packageURI = Uri.parse("package:" + mPackName);
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
context.startActivity(intent);
context.finish();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//关闭页面或者做其他操作
cancelPermissionDialog();
//mContext.finish();
mPermissionsResult.forbitPermissons();
}
})
.create();
}
mPermissionDialog.show();
}
//关闭对话框
private void cancelPermissionDialog() {
if (mPermissionDialog != null) {
mPermissionDialog.cancel();
mPermissionDialog = null;
}
}
public interface IPermissionsResult {
void passPermissons();
void forbitPermissons();
}
}