基本的头像更换功能代码我就不贴了,度娘那里一大堆,我今天来说下从相册选取图片崩溃,其实网上的很多头像更换功能例子很多,适合于4.4以下的机型可能 都 没问题,但是4.4以上的就会出现如题所出现的BUG,直接 上代码吧!
view.findViewById(R.id.image_take).setOnClickListener(this);
view.findViewById(R.id.image_video).setOnClickListener(this);
// 相册
view.findViewById(R.id.image_video).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
} else {
intent.setAction(Intent.ACTION_GET_CONTENT);
};
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
}
});
// 拍照
view.findViewById(R.id.image_take).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
// 判断存储卡是否可以用,可用进行存储
if (hasSdcard()) {
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), PHOTO_FILE_NAME)));
}
startActivityForResult(intent, PHOTO_REQUEST_CAMERA);
}
});
”重要的代码我用图片来展示“
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PHOTO_REQUEST_GALLERY) {
if (data != null) {
// 得到图片的全路径
uri = data.getData();
System.out.println("data-----" + data);
crop(uri);
}
} else if (requestCode == PHOTO_REQUEST_CAMERA) {
if (hasSdcard()) {
tempFile = new File(Environment.getExternalStorageDirectory(),
PHOTO_FILE_NAME);
crop(Uri.fromFile(tempFile));
} else {
Toast.makeText(PersonalActivity.this, "未找到存储卡,无法存储照片!", 0)
.show();
}
} else if (requestCode == PHOTO_REQUEST_CUT) {
try {
bitmap = data.getParcelableExtra("data");
this.headImage.setImageBitmap(bitmap);
// 删除临时文件
ImageUtil.saveToSdcard(bitmap);
imagePath = Config.SDPATH_HEAD + "temp_photo.jpg";
System.out.println("imagePath=="+imagePath);
// 替换服务器上面的头像
toUploadFile() ;
} catch (Exception e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* 剪切图片
*
* @function:
* @author:yy
* @date:2017-09-13
* @param uri
*/
private void crop(Uri uri) {
// 裁剪图片意图
Intent intent = new Intent("com.android.camera.action.CROP");
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
String url=getPath(getBaseContext(),uri);
intent.setDataAndType(Uri.fromFile(new File(url)), "image/*");
}else{
intent.setDataAndType(uri, "image/*");
}
// intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
// 裁剪框的比例,1:1
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// 裁剪后输出图片的尺寸大小
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
// 图片格式
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true);// 取消人脸识别
intent.putExtra("return-data", true);// true:不返回uri,false:返回uri
startActivityForResult(intent, PHOTO_REQUEST_CUT);
}
@SuppressLint("NewApi")
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
inal String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[] { split[1] };
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
其他都 差不多了,就不多BB了,如果 有需要替换服务器上的头像也就是上传图片的代码小伙伴可以滴滴我!
网友评论