1.简单的打开相册、相机
public class MainActivity extends AppCompatActivity {
private ImageView image1;
PopupWindow popupWindow;
private String path=Environment.getExternalStorageDirectory()+"/image.png";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//相册
image1=findViewById(R.id.image1);
//点击相册 出现pop
image1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取布局
View view=View.inflate(MainActivity.this,R.layout.pop,null);
//获取控件
TextView xc = view.findViewById(R.id.xc);
TextView camera = view.findViewById(R.id.camera);
popupWindow=new PopupWindow(view,ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,true);
//点击外部 pop消失
popupWindow.setTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
//位于相册下方
popupWindow.showAsDropDown(image1,30,50);
//点击拍照
camera.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, 100);
}
});
//点击相册
xc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 100);
}
});
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==100&&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", 250);
intent.putExtra("outputY", 250);
//返回
intent.putExtra("return-data", true);
startActivityForResult(intent, 200);
}
if(requestCode==200&&resultCode==RESULT_OK)
{
Bitmap bitmap=data.getParcelableExtra("data");
//把相册或者拍照的照片赋值给相册
image1.setImageBitmap(bitmap);
}
}
}
2.手机权限8.0
public class My extends Fragment implements IView {
PopupWindow popupWindow;
IPresenter iPresenter;
private final int REQUEST_PICK = 200;
private final int REQUEST_CAMEAR = 100;
private final int REQUEST_PICTRUE = 300;
private final String PATH_FILE = Environment.getExternalStorageDirectory() + "/file.png";
private final String path = Environment.getExternalStorageDirectory() + "/image.png";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.action_my, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//绑定
ButterKnife.bind(this, view);
iPresenter = new IPresenter(this);
//获取昵称
iPresenter.get(Apis.GRZLURL, GRZL.class);
}
@OnClick({R.id.my_personal, R.id.my_footprin, R.id.my_circle, R.id.my_address, R.id.my_price,R.id.tx_icon})
public void setonclick(View v) {
switch (v.getId()) {
case R.id.tx_icon:
//判断权限
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M) {
String[] mStatenetwork = new String[]{
//写的权限
Manifest.permission.WRITE_EXTERNAL_STORAGE,
//读的权限
Manifest.permission.READ_EXTERNAL_STORAGE,
//入网权限
Manifest.permission.ACCESS_NETWORK_STATE,
//WIFI权限
Manifest.permission.ACCESS_WIFI_STATE,
//读手机权限
Manifest.permission.READ_PHONE_STATE,
//网络权限
Manifest.permission.INTERNET,
//相机
Manifest.permission.CAMERA,
Manifest.permission.WRITE_APN_SETTINGS,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_NETWORK_STATE,
};
ActivityCompat.requestPermissions(getActivity(), mStatenetwork, 100);
}
getpop();
break;
default:
break;
}
}
//pop
private void getpop() {
View view = View.inflate(getContext(), R.layout.action_my_pop_item, null);
TextView photo = view.findViewById(R.id.photo);
TextView camera = view.findViewById(R.id.camera);
popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
// popupWindow.setFocusable(true);
//点击pop外部 隐藏
popupWindow.setTouchable(true);
// popupWindow.setContentView(view);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.showAsDropDown(tx_icon, 50, 50);
photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent,REQUEST_PICK);
popupWindow.dismiss();
}
});
camera.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, REQUEST_CAMEAR);
popupWindow.dismiss();
}
});
}
private void getUserAvatar(Map<String,String> map) {
iPresenter.getFile(Apis.SCTX,map,FileBean.class);
}
@Override
public void onSuccess(Object data) {
if (data instanceof GRZL) {
GRZL grzl = (GRZL) data;
//头像
Uri uri = Uri.parse(grzl.getResult().getHeadPic());
tx_icon.setImageURI(uri);
//昵称
String na = grzl.getResult().getNickName();
myname.setText(na);
}
else if(data instanceof FileBean)
{
FileBean fileBean= (FileBean) data;
if(fileBean.getMessage().equals("上传成功"))
{
iPresenter.get(Apis.GRZLURL,GRZL.class);
Uri uri=Uri.parse(fileBean.getHeadpath());
RoundingParams params = RoundingParams.asCircle();
tx_icon.getHierarchy().setRoundingParams(params);
tx_icon.setImageURI(uri);
Toast.makeText(getContext(), "上传成功", Toast.LENGTH_SHORT).show();
}else
{
Toast.makeText(getContext(), "上传失败", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
iPresenter.detach();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CAMEAR) {
//打开裁剪
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, REQUEST_PICTRUE);
}
if(requestCode==REQUEST_PICK)
{
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,REQUEST_PICTRUE);
}
if (requestCode == REQUEST_PICTRUE) {
Bitmap bitmap = data.getParcelableExtra("data");
try {
App.saveBitmap(bitmap, PATH_FILE, 50);
} catch (IOException e) {
e.printStackTrace();
Log.i("TAG", e.getMessage());
}
// File file = new File(PATH_FILE);
Map<String, String> map = new HashMap<>();
map.put("image", PATH_FILE);
getUserAvatar(map);
}
}
}
3.打开相机以及判断权限6.0
//点击打开相机
@OnClick(R.id.pai)
public void setLick(View view)
{
switch (view.getId())
{
case R.id.pai:
//判断
if(Build.VERSION.SDK_INT>=23)
{
int checkCallPhonePermission=ContextCompat.checkSelfPermission(getContext(),Manifest.permission.CAMERA);
if(checkCallPhonePermission!=PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions((Activity) getContext(),new String[]{Manifest.permission.CAMERA},222);
return;
}else
{
getpop();
}
}
getpop();
break;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode)
{
case 222:
if(grantResults[0]==PackageManager.PERMISSION_GRANTED)
{
Toast.makeText(getContext(), "ok", Toast.LENGTH_SHORT).show();
}else
{
Toast.makeText(getContext(), "开启权限失败", Toast.LENGTH_SHORT).show();
}
break;
default:
super.onRequestPermissionsResult(requestCode,permissions,grantResults);
}
}