|
@@ -1,13 +1,25 @@
|
|
|
package com.ynstkz.shitu.android.activity;
|
|
|
|
|
|
+import android.Manifest;
|
|
|
+import android.app.AlertDialog;
|
|
|
import android.app.ProgressDialog;
|
|
|
+import android.content.ComponentName;
|
|
|
+import android.content.ContentValues;
|
|
|
+import android.content.Context;
|
|
|
+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.Build;
|
|
|
import android.os.Bundle;
|
|
|
import android.os.Environment;
|
|
|
import android.provider.MediaStore;
|
|
|
+import android.support.annotation.NonNull;
|
|
|
import android.support.annotation.Nullable;
|
|
|
+import android.support.v4.app.ActivityCompat;
|
|
|
+import android.support.v4.content.FileProvider;
|
|
|
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
|
|
|
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
|
|
|
import android.text.TextUtils;
|
|
@@ -20,6 +32,7 @@ import com.bumptech.glide.request.target.BitmapImageViewTarget;
|
|
|
import com.common.library.okhttp.callback.Callback;
|
|
|
import com.google.gson.Gson;
|
|
|
import com.tencent.connect.UserInfo;
|
|
|
+import com.ynstkz.shitu.android.BuildConfig;
|
|
|
import com.ynstkz.shitu.android.R;
|
|
|
import com.ynstkz.shitu.android.application.STApplication;
|
|
|
import com.ynstkz.shitu.android.application.STSign;
|
|
@@ -38,6 +51,7 @@ import org.greenrobot.eventbus.EventBus;
|
|
|
import java.io.File;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
|
+import java.util.Locale;
|
|
|
|
|
|
import butterknife.Bind;
|
|
|
import okhttp3.Call;
|
|
@@ -69,12 +83,9 @@ public class AlterHeadpicActivity extends TitleBarActivity implements View.OnCli
|
|
|
|
|
|
private SelectPicDialog selectPicDialog;
|
|
|
private ProgressDialog progressDialog;
|
|
|
-
|
|
|
- private String protraitPath;
|
|
|
- private File protraitFile;
|
|
|
- private Uri origUri;
|
|
|
+ private File mOutputImage;
|
|
|
private Uri cropUri;
|
|
|
- private final static int CROP = 200;
|
|
|
+ private File uploadFile;
|
|
|
|
|
|
@Override
|
|
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
@@ -147,99 +158,106 @@ public class AlterHeadpicActivity extends TitleBarActivity implements View.OnCli
|
|
|
* 相机拍照
|
|
|
*/
|
|
|
private void startActionCamera() {
|
|
|
- Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
|
|
|
- intent.putExtra(MediaStore.EXTRA_OUTPUT, getCameraTempFile());
|
|
|
- startActivityForResult(intent, REQUEST_CODE_GETIMAGE_BYCAMERA);
|
|
|
- }
|
|
|
+ if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ //权限发生了改变 true // false 小米
|
|
|
+ if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
|
|
|
+ new AlertDialog.Builder(this).setTitle("提示")
|
|
|
+ .setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(DialogInterface dialog, int which) {
|
|
|
+ // 请求授权
|
|
|
+ ActivityCompat.requestPermissions(AlterHeadpicActivity.this, new String[]{Manifest.permission.CAMERA}, 1);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .setNegativeButton("取消", new DialogInterface.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(DialogInterface dialog, int which) {
|
|
|
|
|
|
- /**
|
|
|
- * 拍照保存的绝对路径
|
|
|
- */
|
|
|
- private Uri getCameraTempFile() {
|
|
|
- String storageState = Environment.getExternalStorageState();
|
|
|
- if (storageState.equals(Environment.MEDIA_MOUNTED)) {
|
|
|
- File savedir = new File(getFileSavePath());
|
|
|
- if (!savedir.exists()) {
|
|
|
- savedir.mkdirs();
|
|
|
+ }
|
|
|
+ }).create().show();
|
|
|
+ } else {
|
|
|
+ ActivityCompat.requestPermissions(AlterHeadpicActivity.this, new String[]{Manifest.permission.CAMERA}, 1);
|
|
|
}
|
|
|
} else {
|
|
|
- showToast("无法保存上传的头像,请检查SD卡是否挂载");
|
|
|
- return null;
|
|
|
+ closePopDialog();
|
|
|
+ String pictureName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.getDefault()).format(new Date()) +
|
|
|
+ "-" + System.currentTimeMillis() + ".jpg";
|
|
|
+ mOutputImage = new File(getExternalCacheDir(), pictureName);
|
|
|
+ Uri imageUri;
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
|
+ imageUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".fileprovider", mOutputImage);
|
|
|
+ } else {
|
|
|
+ imageUri = Uri.fromFile(mOutputImage);
|
|
|
+ }
|
|
|
+ Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
|
|
|
+ intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); //图片存储的地方.
|
|
|
+ intent.putExtra("return-data", false);
|
|
|
+ intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
|
|
|
+ intent.putExtra("noFaceDetection", true);
|
|
|
+ ComponentName componentName = intent.resolveActivity(getPackageManager());
|
|
|
+ if (componentName != null) {
|
|
|
+ startActivityForResult(intent, REQUEST_CODE_GETIMAGE_BYCAMERA);
|
|
|
+ }
|
|
|
}
|
|
|
- String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss")
|
|
|
- .format(new Date());
|
|
|
- // 照片命名
|
|
|
- String cropFileName = "st_camera_" + timeStamp + ".jpg";
|
|
|
- // 裁剪头像的绝对路径
|
|
|
- protraitPath = getFileSavePath() + cropFileName;
|
|
|
- protraitFile = new File(protraitPath);
|
|
|
- cropUri = Uri.fromFile(protraitFile);
|
|
|
- this.origUri = this.cropUri;
|
|
|
- return this.cropUri;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取文件保存路径
|
|
|
- *
|
|
|
- * @return
|
|
|
- */
|
|
|
- private String getFileSavePath() {
|
|
|
- return Environment
|
|
|
- .getExternalStorageDirectory().getAbsolutePath()
|
|
|
- + "/shitu/Portrait/";
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 拍照后裁剪
|
|
|
- *
|
|
|
- * @param data 原始图片
|
|
|
- * 裁剪后图片
|
|
|
*/
|
|
|
- private void startActionCrop(Uri data) {
|
|
|
+ private void startActionCrop(File file) {
|
|
|
+
|
|
|
+ String cropImageName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.getDefault()).format(new Date()) +
|
|
|
+ "-1-" + System.currentTimeMillis() + ".jpg";
|
|
|
+ File cropFile = new File(getExternalCacheDir(), cropImageName);
|
|
|
+ //注意到此处使用的file:// uri类型.
|
|
|
+ cropUri = Uri.fromFile(cropFile);
|
|
|
Intent intent = new Intent("com.android.camera.action.CROP");
|
|
|
- intent.setDataAndType(data, "image/*");
|
|
|
- intent.putExtra("output", this.getUploadTempFile(data));
|
|
|
+ Uri sourceUri;
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
|
+ sourceUri = getImageContentUri(this, file);
|
|
|
+ } else {
|
|
|
+ sourceUri = Uri.fromFile(file);
|
|
|
+ }
|
|
|
+ intent.setDataAndType(sourceUri, "image/*");
|
|
|
intent.putExtra("crop", "true");
|
|
|
- intent.putExtra("aspectX", 1);// 裁剪框比例
|
|
|
+ intent.putExtra("aspectX", 1);
|
|
|
intent.putExtra("aspectY", 1);
|
|
|
- intent.putExtra("outputX", CROP);// 输出图片大小
|
|
|
- intent.putExtra("outputY", CROP);
|
|
|
- intent.putExtra("scale", true);// 去黑边
|
|
|
- intent.putExtra("scaleUpIfNeeded", true);// 去黑边
|
|
|
- startActivityForResult(intent,
|
|
|
- REQUEST_CODE_GETIMAGE_BYSDCARD);
|
|
|
+ intent.putExtra("outputX", 200);
|
|
|
+ intent.putExtra("outputY", 200);
|
|
|
+ intent.putExtra("return-data", false);
|
|
|
+ intent.putExtra(MediaStore.EXTRA_OUTPUT, cropUri);
|
|
|
+ intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
|
|
|
+ intent.putExtra("noFaceDetection", true);
|
|
|
+ ComponentName componentName = intent.resolveActivity(getPackageManager());
|
|
|
+ if (componentName != null) {
|
|
|
+ startActivityForResult(intent, REQUEST_CODE_GETIMAGE_BYSDCARD);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- // 裁剪头像的绝对路径
|
|
|
- private Uri getUploadTempFile(Uri uri) {
|
|
|
- String storageState = Environment.getExternalStorageState();
|
|
|
- if (storageState.equals(Environment.MEDIA_MOUNTED)) {
|
|
|
- File savedir = new File(getFileSavePath());
|
|
|
- if (!savedir.exists()) {
|
|
|
- savedir.mkdirs();
|
|
|
- }
|
|
|
+ //获取文件的Content uri路径
|
|
|
+ public static Uri getImageContentUri(Context context, File imageFile) {
|
|
|
+ String filePath = imageFile.getAbsolutePath();
|
|
|
+ Cursor cursor = context.getContentResolver().query(
|
|
|
+ MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
|
|
|
+ new String[]{MediaStore.Images.Media._ID},
|
|
|
+ MediaStore.Images.Media.DATA + "=? ",
|
|
|
+ new String[]{filePath}, null);
|
|
|
+
|
|
|
+ if (cursor != null && cursor.moveToFirst()) {
|
|
|
+ int id = cursor.getInt(cursor
|
|
|
+ .getColumnIndex(MediaStore.MediaColumns._ID));
|
|
|
+ Uri baseUri = Uri.parse("content://media/external/images/media");
|
|
|
+ return Uri.withAppendedPath(baseUri, "" + id);
|
|
|
} else {
|
|
|
- showToast("无法保存上传的头像,请检查SD卡是否挂载");
|
|
|
- return null;
|
|
|
- }
|
|
|
- String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss")
|
|
|
- .format(new Date());
|
|
|
- String thePath = ImageUtils.getAbsolutePathFromNoStandardUri(uri);
|
|
|
-
|
|
|
- // 如果是标准Uri
|
|
|
- if (TextUtils.isEmpty(thePath)) {
|
|
|
- thePath = ImageUtils.getAbsoluteImagePath(this, uri);
|
|
|
+ if (imageFile.exists()) {
|
|
|
+ ContentValues values = new ContentValues();
|
|
|
+ values.put(MediaStore.Images.Media.DATA, filePath);
|
|
|
+ return context.getContentResolver().insert(
|
|
|
+ MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
- String ext = FileUtils.getFileFormat(thePath);
|
|
|
- ext = TextUtils.isEmpty(ext) ? "jpg" : ext;
|
|
|
- // 照片命名
|
|
|
- String cropFileName = "st_crop_" + timeStamp + "." + ext;
|
|
|
- // 裁剪头像的绝对路径
|
|
|
- protraitPath = getFileSavePath() + cropFileName;
|
|
|
- protraitFile = new File(protraitPath);
|
|
|
-
|
|
|
- cropUri = Uri.fromFile(protraitFile);
|
|
|
- return this.cropUri;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -249,19 +267,18 @@ public class AlterHeadpicActivity extends TitleBarActivity implements View.OnCli
|
|
|
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
|
|
intent.setType("image/*");
|
|
|
- startActivityForResult(Intent.createChooser(intent, "选择图片"), REQUEST_CODE_GETIMAGE_BYCROP);
|
|
|
+ ComponentName componentName = intent.resolveActivity(getPackageManager());
|
|
|
+ if (componentName != null) {
|
|
|
+ startActivityForResult(intent, REQUEST_CODE_GETIMAGE_BYCROP);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 上传头像
|
|
|
*/
|
|
|
private void userResetHeadpic() {
|
|
|
|
|
|
- if (!TextUtils.isEmpty(protraitPath) && protraitFile.exists()) {
|
|
|
-
|
|
|
- } else {
|
|
|
- showToast("图像不存在,上传失败");
|
|
|
+ if(uploadFile == null){
|
|
|
return;
|
|
|
}
|
|
|
|
|
@@ -269,7 +286,7 @@ public class AlterHeadpicActivity extends TitleBarActivity implements View.OnCli
|
|
|
progressDialog.setMessage("正在提交...");
|
|
|
progressDialog.show();
|
|
|
|
|
|
- RequestGroup.userResetHeadpic(protraitFile, new Callback() {
|
|
|
+ RequestGroup.userResetHeadpic(uploadFile, new Callback() {
|
|
|
@Override
|
|
|
public Object parseNetworkResponse(Response response, int id) throws Exception {
|
|
|
return new Gson().fromJson(response.body().string(), BaseBean.class);
|
|
@@ -288,7 +305,7 @@ public class AlterHeadpicActivity extends TitleBarActivity implements View.OnCli
|
|
|
if (baseBean != null) {
|
|
|
showToast(baseBean.getMsg());
|
|
|
if ("200".equals(baseBean.getCode())) {
|
|
|
- Glide.with(AlterHeadpicActivity.this).load(protraitFile).asBitmap().into(new BitmapImageViewTarget(ivHeadPic){
|
|
|
+ Glide.with(AlterHeadpicActivity.this).load(uploadFile).asBitmap().into(new BitmapImageViewTarget(ivHeadPic){
|
|
|
|
|
|
@Override
|
|
|
protected void setResource(Bitmap resource) {
|
|
@@ -305,16 +322,34 @@ public class AlterHeadpicActivity extends TitleBarActivity implements View.OnCli
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
|
+ super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
|
+
|
|
|
+ if (requestCode == 1) {
|
|
|
+ // camear 权限回调
|
|
|
+ if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
|
+ closePopDialog();
|
|
|
+ startActionCamera();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
switch (requestCode) {
|
|
|
case REQUEST_CODE_GETIMAGE_BYCAMERA:
|
|
|
- startActionCrop(origUri);// 拍照后裁剪
|
|
|
+ startActionCrop(mOutputImage);// 拍照后裁剪
|
|
|
case REQUEST_CODE_GETIMAGE_BYCROP:
|
|
|
if (data != null) {
|
|
|
- startActionCrop(data.getData());// 选图后裁剪
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
|
|
+ startActionCrop(new File(FileUtils.parsePicturePath(AlterHeadpicActivity.this, data.getData())));// 选图后裁剪
|
|
|
+ }
|
|
|
}
|
|
|
break;
|
|
|
case REQUEST_CODE_GETIMAGE_BYSDCARD:
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
|
|
+ uploadFile = new File(FileUtils.parsePicturePath(AlterHeadpicActivity.this, cropUri));
|
|
|
+ }
|
|
|
userResetHeadpic();// 上传新照片
|
|
|
break;
|
|
|
}
|