* 获取相册的图片路径
*@paramdata
*@return
*/
privateString getImagePath(Intent data) {
ContentResolver resolver =mActivity.getContentResolver();
String[] project = {MediaStore.Images.Media.DATA};
Cursor cursor = resolver.query(data.getData(), project,null,null,null);
intcolumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String path = cursor.getString(columnIndex);
returnpath;
}
package com.kunxun.cgj.presenter.presenter.mine;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import com.kunxun.cgj.R;
import com.kunxun.cgj.activity.BaseActivity;
import com.kunxun.cgj.activity.GeneralActivity;
import com.kunxun.cgj.api.imp.ApiClientV1Async;
import com.kunxun.cgj.api.imp.ApiInterface;
import com.kunxun.cgj.api.model.RespBase;
import com.kunxun.cgj.common.Cons;
import com.kunxun.cgj.common.EventCenter;
import com.kunxun.cgj.common.FragmentFactory;
import com.kunxun.cgj.common.ViewManager;
import com.kunxun.cgj.custom_interface.TaskFinish;
import com.kunxun.cgj.fragment.AccountFragment;
import com.kunxun.cgj.fragment.BaseFragment;
import com.kunxun.cgj.presenter.model.mine.AccountFragmentModel;
import com.kunxun.cgj.presenter.model.mine.ForgetPasswordModel;
import com.kunxun.cgj.presenter.view.mine.AccountFragmentView;
import com.kunxun.cgj.ui.CustomActionSheetDialog;
import com.kunxun.cgj.ui.CustomPositionDialog;
import com.kunxun.cgj.ui.NavigationBar;
import com.kunxun.cgj.ui.view.LeftTextRightImageLayout;
import com.kunxun.cgj.utils.FileUtils;
import com.kunxun.cgj.utils.ImageUtil;
import com.kunxun.cgj.utils.RPermissionUril;
import com.kunxun.cgj.utils.StringUtil;
import com.kunxun.cgj.utils.UserInfoUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.utils.IoUtils;
import org.greenrobot.eventbus.EventBus;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by hjhjl222 on 16/5/19.
*/
public class AccountSettingPresenter extends ItemCommonPresenter<AccountFragmentView,AccountFragmentModel> {
/**
* 打开相机
*/
private final int OPEN_CAMARE = 1;
/**
* 打开系统图库
*/
public static final int OPEN_MAP_DEPOT = 2;
/**
* 切图
*/
private final int ACTIVITY_RESULT_CUT_IMAGE = 3;
/**
* 打开相机
*/
private final int ACTIVITY_RESULT_OPEN_MAP_DEPOT = 4;
private BaseActivity mActivity;
private CustomPositionDialog mDialog;
private CustomActionSheetDialog mSheetDialog;
private Uri mPhotoUri, nPhotoUri;
private String mUrl;
private File mOutputFile;
public AccountSettingPresenter(AccountFragmentView view) {
super(view);
setModel(new AccountFragmentModel());
mActivity = (BaseActivity) ((AccountFragment) view).getActivity();
setLoginStyle();
}
@Override
public void initView() {
LeftTextRightImageLayout layout = new LeftTextRightImageLayout(mActivity);
layout.setText(R.string.update_password);
layout.setTextColor(R.color.black_text_color);
layout.setId(R.id.content);
layout.setLayoutHeight(R.dimen.fifty_five_dp);
ScrollView sv_content = getActiveView().getView(R.id.sv_content);
sv_content.addView(layout);
int height = mActivity.getResources().getDimensionPixelSize(R.dimen.forty_eight_dp);
int margin = mActivity.getResources().getDimensionPixelSize(R.dimen.sixteen_dp);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
params.setMargins(0, 0, 0, margin);
params.gravity = Gravity.BOTTOM;
TextView textView = (TextView) LayoutInflater.from(mActivity).inflate(R.layout.textview, null);
textView.setText(R.string.login_out);
textView.setGravity(Gravity.CENTER);
textView.setLayoutParams(params);
textView.setBackgroundResource(R.drawable.ripple_click_bg_white);
FrameLayout frameLayout = (FrameLayout) mActivity.getWindow().getDecorView();
frameLayout.addView(textView);
AccountFragment fragment = (AccountFragment) getActiveView();
layout.setOnClickListener(fragment);
textView.setOnClickListener(fragment);
getActiveView().getView(R.id.civ_avator).setOnClickListener(fragment);
}
private void setLoginStyle() {
String content = getModel().getNick();
if (StringUtil.isEmpty(content)) {
content = mActivity.getString(R.string.please_login);
}
getActiveView().setText(R.id.tv_name, content);
getActiveView().loadAvator(getModel().getAvatorUrl());
}
@Override
public boolean onItemSelectListener(int viewId) {
switch (viewId) {
case -1:
getActiveView().finishActivity();
break;
}
return true;
}
@Override
public void action(View v) {
switch (v.getId()) {
case R.id.content:
toUpdatePassword();
break;
case R.id.tv_content:
showExitDialog();
break;
case R.id.civ_avator:
changeAvator();
break;
}
}
private void changeAvator() {
if (mSheetDialog == null) {
mSheetDialog = new CustomActionSheetDialog(mActivity);
mSheetDialog.addItem(R.string.photograph);
mSheetDialog.addItem(R.string.select_from_map_depot);
mSheetDialog.setOnClickListener(itemClick);
mSheetDialog.setDialogPosition(CustomPositionDialog.POSTION_BOTTOM);
mSheetDialog.setCanCancelByOut(true);
}
mSheetDialog.show();
}
private void showExitDialog() {
if (mDialog == null) {
mDialog = new CustomPositionDialog(mActivity, R.string.login_out_the_account, R.string.login_out_prompt,
R.string.cancle, R.string.exit, new CustomPositionDialog.OnPositiveClickListener() {
@Override
public void onClick(int which) {
switch (which) {
case -1:
UserInfoUtils.getIns().setUser(null);
ViewManager.getIns().popToMainActivity();
break;
}
}
});
mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
mDialog = null;
}
});
mDialog.setCanCancelByOut(true);
mDialog.show();
}
}
private void toUpdatePassword() {
Intent intent = new Intent(mActivity, GeneralActivity.class);
intent.putExtra(Cons.BundleKey.FRAGMENT_TYPE, FragmentFactory.FRAGMENT_LOGIN);
intent.putExtra(Cons.BundleKey.GET_PASSWORD, ForgetPasswordModel.RESET_PASSWORD);
intent.putExtra(Cons.BundleKey.PRESENTER, BaseCommonChildPresenter.PRESENTER_FORGET_PASSWORD);
mActivity.startActivity(intent);
}
@Override
public void updateNavigationBarStyle(NavigationBar navigationBar, int life_cycle) {
navigationBar.setLeftIcon(R.drawable.ic_back_white);
navigationBar.setTitle(R.string.account_setting);
}
CustomActionSheetDialog.OnItemClick itemClick = new CustomActionSheetDialog.OnItemClick() {
@Override
public void onItemClick(int position) {
Intent intent;
int requestCode;
switch (position) {
case 0:
if (RPermissionUril.checkCamarePermission(mActivity)) {
toCamera();
} else {
RPermissionUril.requestCameraPermission(mActivity, RPermissionUril.REQUEST_CAMERA_PERMISSION);
}
break;
case 1:
requestCode = OPEN_MAP_DEPOT;
nPhotoUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
intent = new Intent(Intent.ACTION_PICK, nPhotoUri);
((BaseFragment) getActiveView()).startActivityForResult(intent, requestCode);
break;
}
}
@Override
public void onDismiss() {
mSheetDialog = null;
}
};
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case RPermissionUril.REQUEST_CAMERA_PERMISSION:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
toCamera();
} else {
mActivity.showToast("没有授予相机权限!!!");
}
break;
}
}
private void toCamera() {
int requestCode = OPEN_CAMARE;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mPhotoUri = FileUtils.getIns().getOutputMediaFileUri(FileUtils.MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoUri);
((BaseFragment) getActiveView()).startActivityForResult(intent, requestCode);
}
@Override
public void onActivityResult( int requestCode, int resultCode, final Intent data) {
if (resultCode==mActivity.RESULT_OK){
if (requestCode != ACTIVITY_RESULT_CUT_IMAGE) {
if (requestCode == OPEN_CAMARE) {
startPhotoZoom(mPhotoUri);
} else if (requestCode == OPEN_MAP_DEPOT) {
startPhotoZoom(data.getData());
}
} else {
Bundle extras = data.getExtras();
if(extras != null) {
String fileName = System.currentTimeMillis() + ".jpg";
Bitmap bitmap = (Bitmap) extras.get("data");
try {
String savePath = saveFile(bitmap, fileName);
doUploadHeadPic(savePath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/**
* 保存文件至SD卡
* @param bm
* @param fileName
* @throws IOException
*/
public String saveFile(Bitmap bm, String fileName) throws IOException {
String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
File dirFile = new File(sdPath);
if(!dirFile.exists()){
dirFile.mkdir();
}
String localHeadPicPath = sdPath + fileName;
File myCaptureFile = new File(localHeadPicPath);
ImageUtil.saveAvatar(bm, localHeadPicPath);
return myCaptureFile.getAbsolutePath();
}
/**
* 裁剪图片
* @param uri
*/
private void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
//裁剪框比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
//图片输出大小
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("return-data", true);
//启用人脸识别
intent.putExtra("noFaceDetection", true);
((BaseFragment) getActiveView()).startActivityForResult(intent, ACTIVITY_RESULT_CUT_IMAGE);
}
/**
* 更换头像
* @param headPicUrl
*/
private void doUploadHeadPic(final String headPicUrl) {
getActiveView().showLoading(true);
File headFile = new File(headPicUrl);
ApiClientV1Async.updateHeadPic(headFile, new TaskFinish<RespBase>() {
@Override
public void finish(RespBase respBase) {
getActiveView().hideLoading(false);
if (respBase.getStatus().equals(ApiInterface.SUCCESS)){
ImageView imageView = getActiveView().getView(R.id.civ_avator);
try {
InputStream inputStream = new FileInputStream(headPicUrl);
ImageLoader.getInstance().getDiskCache().save(respBase.getHead(), inputStream, null);
ImageLoader.getInstance().displayImage(respBase.getHead(),imageView,ImageUtil.getAvatarOptions(mActivity));
}catch (Exception e){
}
EventBus.getDefault().post(new EventCenter(Cons.EB_SCALE_PICTURE_FINISH,respBase.getHead()));
}else {
mActivity.showToast(respBase.getMessage());
}
deleteFile(headPicUrl);
}
});
}
/**
* 删除指定路径的SD卡文件
* @param headPicUrl
*/
private void deleteFile(String headPicUrl){
File sdHeadFile = new File(headPicUrl);
if(!sdHeadFile.isDirectory()){
sdHeadFile.delete();
}else if(sdHeadFile.isDirectory()){
String []headPicList = sdHeadFile.list();
for(int j=0;j<headPicList.length;j++){
File filessFile= new File(headPicUrl+"\\"+headPicList[j]);
if(!filessFile.isDirectory()){
filessFile.delete();
}else if(filessFile.isDirectory()){
deleteFile(headPicUrl+"\\"+headPicList[j]);
}
}
sdHeadFile.delete();
}
}
}