public class Sheupthepersenter extends AgeInter {
private static String path = "/sdcard/myHead/";//sd路径
private Bitmap head;//头像Bitmap
private ImageView imageView;
@Override
public int getLayoutId() {
return R.layout.sethupthe;
}
@Override
public void initdata() {
super.initdata();
imageView = (ImageView) get(R.id.tou);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Toast.makeText(context,"shzgdshdgshdghdg",Toast.LENGTH_LONG).show();
show();
}
});
}
private void show() {
new SelectImagePopupWindow(context,get(R.id.activity_layout),
new SelectImagePopupWindow.OnSelectPictureListener() {
@Override
public void onTakePhoto() { //拍照
Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
"head.png")));
((Sheupthe)context).startActivityForResult(intent2, 2);//采用ForResult打开
}
@Override
public void onSelectPicture() { // 从手机相册选择
Intent intent1 = new Intent(Intent.ACTION_PICK, null);
intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
((Sheupthe)context).startActivityForResult(intent1, 1);
}
@Override
public void onCancel() {
}
});
}
/**
* 调用系统的裁剪
*
* @param uri
*/
public void cropPhoto(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 127);
intent.putExtra("outputY", 127);
intent.putExtra("scale", true);
intent.putExtra("noFaceDetection", false);//不启用人脸识别
intent.putExtra("return-data", true);
((Sheupthe)context).startActivityForResult(intent, 3);
}
private void setPicToView(Bitmap mBitmap) {
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
return;
}
FileOutputStream b = null;
File file = new File(path);
file.mkdirs();// 创建文件夹
String fileName = path + "/head.png";//图片名字
try {
b = new FileOutputStream(fileName);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
//关闭流
b.flush();
b.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//ok上传
private void uploadImage(String path) {
MediaType mediaType = MediaType.parse("multipart/form-data; charset=utf-8");
OkHttpClient mOkHttpClent = new OkHttpClient();
File file = new File(path);
MultipartBody.Builder builder = new MultipartBody.Builder()
.setType(mediaType)
.addFormDataPart("file", "head.png",
RequestBody.create(MediaType.parse("image/png"), file))
.addFormDataPart("uid", "22007");
RequestBody requestBody = builder.build();
Request request = new Request.Builder()
.url("http://www.zhaoapi.cn/file/upload")
.post(requestBody)
.build();
Call call = mOkHttpClent.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
((Sheupthe)context).runOnUiThread(new Runnable() {
@Override
public void run() {
Log.i("TAG", e.getMessage());
Toast.makeText(context, "失败", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
((Sheupthe)context).runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(context, "成功", Toast.LENGTH_SHORT).show();
}
});
}
});
}
private Context context;
@Override
public void getContext(Context context) {
this.context=context;
}
public void onActvity(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
cropPhoto(data.getData());//裁剪图片
}
break;
case 2:
if (resultCode == RESULT_OK) {
File temp = new File(Environment.getExternalStorageDirectory()
+ "/head.png");
cropPhoto(Uri.fromFile(temp));//裁剪图片
}
break;
case 3:
if (data != null) {
Bundle extras = data.getExtras();
if (extras == null) {
return;
}
head = extras.getParcelable("data");
if (head != null) {
imageView.setImageBitmap(head);
String fileName = path + "/head.png";//图片名字
setPicToView(head);//保存在SD卡中
File file1 = new File(fileName);
//uploadPic(file1);
uploadImage(fileName);
}
}
break;
default:
break;
}
}
}
-------------------------------------------------------
package soexample.umeng.com.jindong.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import soexample.umeng.com.jindong.ActivityPersenter.Sheupthepersenter;
import soexample.umeng.com.jindong.R;
public class SelectImagePopupWindow implements View.OnClickListener {
private Context mContext = null;
private PopupWindow popupWindow = null;
private OnSelectPictureListener listener = null;
public SelectImagePopupWindow(Context mContext, View parentView, OnSelectPictureListener listener) {
this.mContext = mContext;
this.listener = listener;
View view = LayoutInflater.from(mContext).inflate(R.layout.popup_select_imgage, null);
initView(view, parentView);
}
private void initView(View view, View parentView) {
popupWindow = new PopupWindow(parentView,
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
popupWindow.setContentView(view);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
popupWindow.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);//show()
view.findViewById(R.id.popup_select_take_photo).setOnClickListener(this);
view.findViewById(R.id.popup_select_take_picture).setOnClickListener(this);
view.findViewById(R.id.popup_select_take_cancel).setOnClickListener(this);
}
@Override
public void onClick(View v) {
int i = v.getId();
if (i == R.id.popup_select_take_photo) {
listener.onTakePhoto();
} else if (i == R.id.popup_select_take_picture) {
listener.onSelectPicture();
} else if (i == R.id.popup_select_take_cancel) {
listener.onCancel();
}
popupWindow.dismiss();
}
public interface OnSelectPictureListener {
/**
* 拍摄
*/
void onTakePhoto();
/**
* 从相册选择
*/
void onSelectPicture();
/**
* 取消
*/
void onCancel();
}
}
-------------------------------------布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignTop="@+id/xiaojian"
android:layout_marginTop="0dp"
android:text="头像"
android:textSize="20dp" />
<ImageView
android:id="@+id/tou"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="48dp"
android:src="@drawable/c" />
<ImageView
android:id="@+id/xiaojian"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:src="@drawable/xiaojiantou" />
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_alignParentStart="true"
android:layout_below="@+id/tou"
android:background="#696969"></View>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical">
<TextView
android:id="@+id/yoonghuming"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="97dp"
android:text="用户名"
android:textSize="20dp" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignBottom="@+id/view"
android:layout_alignParentEnd="true"
android:src="@drawable/xiaojiantou" />
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="131dp"
android:background="#696969"></View>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="149dp"
android:text="昵称"
android:textSize="20dp" />
<ImageView
android:id="@+id/xiao1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginTop="141dp"
android:src="@drawable/xiaojiantou" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_alignParentStart="true"
android:layout_below="@+id/xiao1"
android:background="#696969"></View>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="209dp"
android:text="性别"
android:textSize="20dp" />
<ImageView
android:id="@+id/xiao2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginTop="194dp"
android:src="@drawable/xiaojiantou" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="251dp"
android:background="#696969"></View>
</RelativeLayout>
</elativeLayout>
----------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/popup_base_background_bg">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/share_main_line"
android:background="#ffffff"
android:orientation="vertical">
<TextView
android:id="@+id/popup_select_take_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_listview_selector"
android:gravity="center"
android:padding="10dp"
android:text="拍照"
android:textColor="#333333"
android:textSize="14sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_above="@+id/popup_select_take_cancel"
android:background="#ebebeb" />
<TextView
android:id="@+id/popup_select_take_picture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_listview_selector"
android:gravity="center"
android:padding="10dp"
android:text="从手机相册选择"
android:textColor="#333333"
android:textSize="14sp" />
</LinearLayout>
<View
android:id="@+id/share_main_line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_above="@+id/popup_select_take_cancel"
android:background="#ebebeb" />
<TextView
android:id="@+id/popup_select_take_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#fff"
android:gravity="center"
android:padding="10dp"
android:text="取消"
android:textColor="#333"
android:textSize="14sp" />
</RelativeLayout>
[点击并拖拽以移动]
[点击并拖拽以移动]