首先要先写一个类 可以做出PopupWindow
public class PublishSelectPicPopupWindow extends PopupWindow {
private Button btnHand;
private Button btnLibrary;
private Button btnTwocode;
private Button btnCancel;
private View mMenuView;
@SuppressWarnings("deprecation")
public PublishSelectPicPopupWindow(Activity context,OnClickListener itemsOnClick) {
super(context);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mMenuView = inflater.inflate(R.layout.publish_dialog, null);
btnHand = (Button) mMenuView.findViewById(R.id.btn_hand);
btnLibrary = (Button) mMenuView.findViewById(R.id.btn_library);
btnCancel = (Button) mMenuView.findViewById(R.id.btn_cancel);
btnCancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dismiss();
}
});
//设置按钮监听
btnHand.setOnClickListener(itemsOnClick);
btnLibrary.setOnClickListener(itemsOnClick);
btnCancel.setOnClickListener(itemsOnClick);
//设置SelectPicPopupWindow的View
this.setContentView(mMenuView);
//设置SelectPicPopupWindow弹出窗体的宽
this.setWidth(LayoutParams.FILL_PARENT);
//设置SelectPicPopupWindow弹出窗体的高
this.setHeight(LayoutParams.WRAP_CONTENT);
//设置SelectPicPopupWindow弹出窗体可点击
this.setFocusable(true);
//设置SelectPicPopupWindow弹出窗体动画效果
this.setAnimationStyle(R.style.AnimBottom);
//实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
//设置SelectPicPopupWindow弹出窗体的背景
this.setBackgroundDrawable(dw);
//mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
mMenuView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int height = mMenuView.findViewById(R.id.pop_layout).getTop();
int y=(int) event.getY();
if(event.getAction()==MotionEvent.ACTION_UP){
if(y<height){
dismiss();
}
}
return true;
}
});
}
}
他也需要一个布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pop_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_pulish"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="@+id/btn_hand"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="20dip"
android:text="拍照"
android:textStyle="bold" />
<Button
android:id="@+id/btn_library"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="20dip"
android:text="从相册选择"
android:textStyle="bold" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="30dip"
android:layout_marginBottom="30dp"
android:text="取消"
android:textStyle="bold" />
</LinearLayout>
接下来就是在MainActvity中写的了首先是他的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/button_up"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="上传图片"/>
<ImageView
android:id="@+id/touxiang"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@mipmap/ic_launcher_round"/>
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
/* private final int REQUESTCODE_CUTTING = 1001;
private final int REQUESTCODE_PICK = 1002;
private final int REQUESTCODE_TAKE = 1003;*/
private Button button_up;
private PublishSelectPicPopupWindow menuWindow;
private static final String IMAGE_FILE_NAME = "ic_launcher_round.png";
private ImageView touxiang;
private File urlpath; // 图片本地路径
private static ProgressDialog pd;// 等待进度圈
//private String imgUrl = "https://www.zhaoapi.cn/file/upload?uid=2809";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_up = findViewById(R.id.button_up);
touxiang = findViewById(R.id.touxiang);
button_up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 实例化SelectPicPopupWindow
menuWindow = new PublishSelectPicPopupWindow(MainActivity.this,itemsOnClick);
// 显示窗口
menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.button_up),
Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
}
});
}
// 为弹出窗口实现监听类
private View.OnClickListener itemsOnClick = new View.OnClickListener() {
public void onClick(View v) {
menuWindow.dismiss();
switch (v.getId()) {
case R.id.btn_hand:
Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//下面这句指定调用相机拍照后的照片存储的路径
takeIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(Environment.getExternalStorageDirectory(), IMAGE_FILE_NAME)));
startActivityForResult(takeIntent, 1000);
break;
case R.id.btn_library:
Intent pickIntent = new Intent(Intent.ACTION_PICK, null);
// 如果朋友们要限制上传到服务器的图片类型时可以直接写如:"image/jpeg 、 image/png等的类型"
pickIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(pickIntent, 2000);
break;
default:
break;
}
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 2000:// 直接从相册获取
try {
startPhotoZoom(data.getData());
} catch (NullPointerException e) {
e.printStackTrace();// 用户点击取消操作
}
break;
case 1000:// 调用相机拍照
File temp = new File(Environment.getExternalStorageDirectory() + "/" + IMAGE_FILE_NAME);
startPhotoZoom(Uri.fromFile(temp));
break;
case 3000:// 取得裁剪后的图片
if (data != null) {
setPicToView(data);
}
break;
}
}
private void setPicToView(Intent picdata) {
Bundle extras = picdata.getExtras();
if (extras != null) {
// 取得SDCard图片路径做显示
Bitmap photo = extras.getParcelable("data");
Drawable drawable = new BitmapDrawable(null, photo);
urlpath = new File(FileUtil.saveFile(MainActivity.this, "temphead.jpg", photo));
touxiang.setImageDrawable(drawable);
// 新线程后台上传服务端
//pd = ProgressDialog.show(MainActivity.this, null, "正在上传图片,请稍候...");
HashMap<String, String> map = new HashMap<>();
map.put("uid", "2809");
//存到服务端
OkHttp3Util.uploadFile(ApiUtil.UP_TOUXIANG, urlpath, "dash.jpg", map, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("+++", "onFailure: "+e.getMessage());
e.printStackTrace();
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
Log.e("---", "onResponse: "+response.body().string() );
if (response.isSuccessful()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "此时上传成功!!", Toast.LENGTH_SHORT).show();
}
});
//此时上传成功 获取用户信息
OkHttp3Util.doGet("https://www.zhaoapi.cn/user/getUserInfo?uid=2809", new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
final GeRenBean userBean = new Gson().fromJson(string, GeRenBean.class);
runOnUiThread(new Runnable() {
@Override
public void run() {
Glide.with(MainActivity.this)
.load(userBean.getData().getIcon()).transform(new GlideCircleTransform(MainActivity.this))
.into(touxiang);
Toast.makeText(MainActivity.this, "上传并且获取成功", Toast.LENGTH_SHORT).show();
}
});
}
});
}
}
});
}
}
private void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 300);
intent.putExtra("return-data", true);
startActivityForResult(intent, 3000);
}
public static String saveFile(Context c, String filePath, String fileName, byte[] bytes) {
String fileFullName = "";
FileOutputStream fos = null;
String dateFolder = new SimpleDateFormat("yyyyMMdd", Locale.CHINA)
.format(new Date());
try {
String suffix = "";
if (filePath == null || filePath.trim().length() == 0) {
filePath = Environment.getExternalStorageDirectory() + "/JiaXT/" + dateFolder + "/";
}
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
File fullFile = new File(filePath, fileName + suffix);
fileFullName = fullFile.getPath();
fos = new FileOutputStream(new File(filePath, fileName + suffix));
fos.write(bytes);
} catch (Exception e) {
fileFullName = "";
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
fileFullName = "";
}
}
}
return fileFullName;
}
}
最后放上完整的代码!大家上传路径自己搭建啊!