Android中设置用户的头像
来源:互联网 发布:json.parse 特殊字符 编辑:程序博客网 时间:2024/05/12 18:28
在开发中我们经常会遇到为用户设置自定义头像的功能,下面我介绍一下如何实现这一功能:
具体代码如下:
package com.example.seticon;import java.io.File;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends Activity {private Button mFromAlbum; // 从相册选取private Button mTakePhotos; // 照相private ImageView mImgView;private final int FROM_ALBUM = 0;private final int FROM_TAKE_PHOTOS = 1;private final int PHOTO_CUT = 2;private final String FILE_NAME = "tempimg.jpg";private File tempFile;private Bitmap bitmap;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mImgView = (ImageView) findViewById(R.id.img);mFromAlbum = (Button) findViewById(R.id.fromAlbum);mTakePhotos = (Button) findViewById(R.id.takePhoto);//从相册选取照片mFromAlbum.setOnClickListener(new OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubIntent intent = new Intent(Intent.ACTION_PICK);intent.setType("image/*");startActivityForResult(intent, FROM_ALBUM);}});//即时拍摄头像mTakePhotos.setOnClickListener(new OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubIntent intent = new Intent("android.media.action.IMAGE_CAPTURE");// 判断存储卡是否可以用,可用进行存储if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), FILE_NAME)));}startActivityForResult(intent, FROM_TAKE_PHOTOS);}});}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubswitch (requestCode) {case FROM_ALBUM:if (data != null) {// 取到照片路径Uri uri = data.getData();editPic(uri);}break;case FROM_TAKE_PHOTOS:if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {tempFile = new File(Environment.getExternalStorageDirectory(),FILE_NAME);editPic(Uri.fromFile(tempFile));} else {Toast.makeText(MainActivity.this, "未找到存储卡!", 0).show();}break;case PHOTO_CUT:try {bitmap = data.getParcelableExtra("data");mImgView.setImageBitmap(bitmap);//删除缓存图片tempFile.delete();} catch (Exception e) {e.printStackTrace();}break;}super.onActivityResult(requestCode, resultCode, data);}/** 编辑选中的照片 */private void editPic(Uri uri) {Intent intent = new Intent("com.android.camera.action.CROP");intent.setDataAndType(uri, "image/*");intent.putExtra("crop", "true");// 裁剪框的比例,1:1intent.putExtra("aspectX", 1);intent.putExtra("aspectY", 1);// 裁剪后输出图片的尺寸大小intent.putExtra("outputX", 250);intent.putExtra("outputY", 250);intent.putExtra("outputFormat", "JPEG");intent.putExtra("noFaceDetection", true);intent.putExtra("return-data", true);startActivityForResult(intent, PHOTO_CUT);}}xml布局文件如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/fromAlbum" android:layout_width="150dp" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="从相册选取" /> <Button android:id="@+id/takePhoto" android:layout_width="150dp" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="拍照" /> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" /></LinearLayout>
所用到的权限有:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
0 0
- Android中设置用户的头像
- Android用户头像的设置
- Android设置用户头像代码
- Android中通过访问本地相册或者相机设置用户头像
- 设置用户圆形头像
- Android 离线用户的灰色头像处理
- Android 离线用户的灰色头像处理
- android给用户头像设置圆形加边框
- android用户头像截取
- Android更改用户头像
- android用户头像截取
- 通过代码设置Android联系人的头像
- 安卓用户头像设置
- android中百度地图撒点标识后台附近的人坐标点及用户头像
- 28.用户登录(1.圆形头像的设置)
- android头像设置功能
- android头像设置
- android设置头像
- Android获取debug签名和release签名方法
- 【CERC2013】【BZOJ4052】Magical GCD
- HttpWatch+HttpClient模拟登录学校教务系统
- 记不住
- 64位win7系统下安装USB下载器驱动FriendlyArm-usb-dnw-driver-的解决方案
- Android中设置用户的头像
- Could not find destination factory for transport解决方法
- 机房水题欢乐赛 2016-02-02
- 如何在FrameLayout添加布局
- Java程序员在用的大数据工具,MongoDB稳居第一!
- ctextart类的文档仅仅首页修改页眉
- day05作业
- 策略模式
- FFmpeg 中的一些参数意义(tbr, tbn, tbc)