和商汇app广东移动动app头像怎么换

Android更换头像功能实现
9882次浏览
现在不管什么APP都有个头像,如果你看见没有头像的APP就会感觉非常奇怪,以前头像都是方的,后来就变成圆的,我估计在过个几年就得来个五角星形状的头像,下面我把更换头像的代码写来:
&RelativeLayout xmlns:android=&/apk/res/android&
xmlns:tools=&/tools&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
tools:context=&.MainActivity& &
&ImageView
android:id=&@+id/iv_head&
android:layout_width=&200dip&
android:layout_height=&200dip&
android:layout_alignParentTop=&true&
android:layout_centerHorizontal=&true&
android:layout_marginTop=&210dip&
android:src=&@drawable/ic_launcher& /&
android:id=&@+id/btn_takephoto&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_alignParentLeft=&true&
android:layout_alignParentTop=&true&
android:text=&拍照& /&
android:id=&@+id/btn_photos&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_alignParentRight=&true&
android:layout_alignParentTop=&true&
android:text=&从相册取& /&
&/RelativeLayout&
首先我定义了一个非常简单的XML布局,里面两个Button,一个ImageView用来显示头像:
下面我来看看MainActivity代码:
package com.example.
import java.io.F
import java.io.FileNotFoundE
import java.io.FileOutputS
import java.io.IOE
import android.net.U
import android.os.B
import android.os.E
import android.provider.MediaS
import android.annotation.SuppressL
import android.app.A
import android.content.I
import android.graphics.B
import android.graphics.BitmapF
import android.graphics.drawable.BitmapD
import android.graphics.drawable.D
import android.view.V
import android.view.View.OnClickL
import android.widget.B
import android.widget.ImageV
@SuppressLint(&SdCardPath&)
public class MainActivity extends Activity implements OnClickListener {
private ImageView ivH//头像显示
private Button btnT//拍照
private Button btnP//相册
private B//头像Bitmap
private static String path=&/sdcard/myHead/&;//sd路径
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
private void initView() {
//初始化控件
btnPhotos = (Button) findViewById(R.id.btn_photos);
btnTakephoto = (Button) findViewById(R.id.btn_takephoto);
btnPhotos.setOnClickListener(this);
btnTakephoto.setOnClickListener(this);
ivHead = (ImageView) findViewById(R.id.iv_head);
Bitmap bt = BitmapFactory.decodeFile(path + &head.jpg&);//从Sd中找头像,转换成Bitmap
if(bt!=null){
@SuppressWarnings(&deprecation&)
Drawable drawable = new BitmapDrawable(bt);//转换成drawable
ivHead.setImageDrawable(drawable);
* 如果SD里面没有则需要从服务器取头像,取回来的头像再保存在SD中
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_photos://从相册里面取照片
Intent intent1 = new Intent(Intent.ACTION_PICK, null);
intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, &image/*&);
startActivityForResult(intent1, 1);
case R.id.btn_takephoto://调用相机拍照
Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
&head.jpg&)));
startActivityForResult(intent2, 2);//采用ForResult打开
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
if (resultCode == RESULT_OK) {
cropPhoto(data.getData());//裁剪图片
if (resultCode == RESULT_OK) {
File temp = new File(Environment.getExternalStorageDirectory()
+ &/head.jpg&);
cropPhoto(Uri.fromFile(temp));//裁剪图片
if (data != null) {
Bundle extras = data.getExtras();
head = extras.getParcelable(&data&);
if(head!=null){
* 上传服务器代码
setPicToView(head);//保存在SD卡中
ivHead.setImageBitmap(head);//用ImageView显示出来
super.onActivityResult(requestCode, resultCode, data);
* 调用系统的裁剪
* @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&, 150);
intent.putExtra(&outputY&, 150);
intent.putExtra(&return-data&, true);
startActivityForResult(intent, 3);
private void setPicToView(Bitmap mBitmap) {
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
FileOutputStream b =
File file = new File(path);
file.mkdirs();// 创建文件夹
String fileName =path + &head.jpg&;//图片名字
b = new FileOutputStream(fileName);
pressFormat.JPEG, 100, b);// 把数据写入文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
b.flush();
b.close();
} catch (IOException e) {
e.printStackTrace();
上面的注释已经很详细了,就这些
对了不要忘记加权限:
&uses-permission android:name=&android.permission.CAMERA&/&
&uses-permission android:name=&android.permission.WRITE_EXTERNAL_STORAGE&/&
&uses-permission android:name=&android.permission.MOUNT_UNMOUNT_FILESYSTEMS&/&
您可能也会对以下文章感兴趣
QQ : 341470
Friend Link
New Member阿加乐怎么换头像?阿加乐app换头像教程
<span id="num_a_人顶
<span id="num_b_人踩
阿加乐怎么改名字?如何换头像?怎么改国家?阿加乐是最近流行的一款新型社交app,小编为大家带来其玩法教程。
阿加乐app安卓版下载地址:
阿加乐app ios版下载地址:
阿加乐怎么换头像 阿加乐换头像方法详解
第一步:下载391k小编给大家地址的最新版阿加乐app,打开登陆后,进入最上栏左侧的我的资料,这时你可以看到是我的头像,点击我的头像
第二步:进入我的头像后会出现这个界面,点击界面中央那个照相机的图标,会弹出两个选项,391k小编选择的是从图库选择,你也可以选择拍摄新照片。
第三步:这时会进入你的相册,391k小编就随便选择一个帅哥图片当头像,选择好后,点击右上角勾的图标进入头像选取区域,你可以调节你的图像大小和旋转,调节好以后点击右下角确定。
第四步:这样你的头像就修改好了,你可以进图自己的个人中心看到你的头像已经修改成功。
以上就是391k小编带给大家的阿加乐换头像方法详解,希望可以帮到大家。
阿加乐怎么改名字?
阿加乐怎么改国家?
热门安卓游戏排行
类别:棋牌游戏
类别:休闲游戏
类别:休闲游戏
413496人推荐511096人推荐610324人推荐79336人推荐89316人推荐99041人推荐108742人推荐
最新安卓游戏排行
类别:射击游戏
类别:塔防游戏
类别:角色扮演
42人推荐52人推荐61人推荐71人推荐82人推荐91人推荐101人推荐Android更换头像功能实现
现在不管什么APP都有个头像,如果你看见没有头像的APP就会感觉非常奇怪,以前头像都是方的,后来就变成圆的,我估计在过个几年就得来个五角星形状的头像,下面我把更换头像的代码写来:
首先我定义了一个非常简单的XML布局,里面两个Button,一个ImageView用来显示头像:
下面我来看看MainActivity代码:
package com.example.
import java.io.F
import java.io.FileNotFoundE
import java.io.FileOutputS
import java.io.IOE
import android.net.U
import android.os.B
import android.os.E
import android.provider.MediaS
import android.annotation.SuppressL
import android.app.A
import android.content.I
import android.graphics.B
import android.graphics.BitmapF
import android.graphics.drawable.BitmapD
import android.graphics.drawable.D
import android.view.V
import android.view.View.OnClickL
import android.widget.B
import android.widget.ImageV
@SuppressLint("SdCardPath")
public class MainActivity extends Activity implements OnClickListener {
private ImageView ivH//头像显示
private Button btnT//拍照
private Button btnP//相册
private B//头像Bitmap
private static String path="/sdcard/myHead/";//sd路径
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
private void initView() {
//初始化控件
btnPhotos = (Button) findViewById(R.id.btn_photos);
btnTakephoto = (Button) findViewById(R.id.btn_takephoto);
btnPhotos.setOnClickListener(this);
btnTakephoto.setOnClickListener(this);
ivHead = (ImageView) findViewById(R.id.iv_head);
Bitmap bt = BitmapFactory.decodeFile(path + "head.jpg");//从Sd中找头像,转换成Bitmap
if(bt!=null){
@SuppressWarnings("deprecation")
Drawable drawable = new BitmapDrawable(bt);//转换成drawable
ivHead.setImageDrawable(drawable);
* 如果SD里面没有则需要从服务器取头像,取回来的头像再保存在SD中
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_photos://从相册里面取照片
Intent intent1 = new Intent(Intent.ACTION_PICK, null);
intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent1, 1);
case R.id.btn_takephoto://调用相机拍照
Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
"head.jpg")));
startActivityForResult(intent2, 2);//采用ForResult打开
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
if (resultCode == RESULT_OK) {
cropPhoto(data.getData());//裁剪图片
if (resultCode == RESULT_OK) {
File temp = new File(Environment.getExternalStorageDirectory()
+ "/head.jpg");
cropPhoto(Uri.fromFile(temp));//裁剪图片
if (data != null) {
Bundle extras = data.getExtras();
head = extras.getParcelable("data");
if(head!=null){
* 上传服务器代码
setPicToView(head);//保存在SD卡中
ivHead.setImageBitmap(head);//用ImageView显示出来
super.onActivityResult(requestCode, resultCode, data);
* 调用的裁剪
* @param uri
public void cropPhoto(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
// ectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
startActivityForResult(intent, 3);
private void setPicToView(Bitmap mBitmap) {
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
FileOutputStream b =
File file = new File(path);
file.mkdirs();// 创建文件夹
String fileName =path + "head.jpg";//图片名字
b = new FileOutputStream(fileName);
pressFormat.JPEG, 100, b);// 把数据写入文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
b.flush();
b.close();
} catch (IOException e) {
e.printStackTrace();
上面的注释已经很详细了,就这些
对了不要忘记加权限:
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467142',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'

我要回帖

更多关于 广东移动app 的文章

 

随机推荐