头像上传功能

现在大多数的app都有头像上传功能包含拍照+图库获取+上传服务器的功能,那么下面我就为大家简单的讲解一下这项能把吧!我这里没有进行封装。
先上一下

首先加上权限:

android:name=‘android.permission.CAMERA’/> android:name=‘android.permission.WRITE_EXTERNAL_STORAGE’/>
android:name=‘android.permission.MOUNT_UNMOUNT_FILESYSTEMS’/>

先看看一下:xml布局主布局:activity_main

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="头像"
android:textSize="20dip" />

android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">

android:id="@+id/ig_inco"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@mipmap/ic_launcher" />

popuwindow的布局:pop_window

android:id="@+id/button1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="相库" />

android:id="@+id/button2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="拍照" />

我这里这是简单的写了一下,你们可以按照自己的需求去设定,一般都会加上popupWindows,我这里也简单的实现了,

在Activity中我们需要设置服务器地址,我这里先给她设置成了sd卡中,让后就直接上代码,哈哈。。。注释很详细自己看吧,亲

SuppressLint("SdCardPath")
public class MainActivity extends Activity implements View.OnClickListener {
private ImageView ivHead;//头像显示
private Button btnTakephoto;//拍照
private Button btnPhotos;//相册
private Bitmap head;//头像Bitmap
private static String path = "/sdcard/myHead/";//sd路径
private Context mContext = null;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
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.ig_inco);
ivHead.setOnClickListener(this);
Bitmap bt = BitmapFactory.decodeFile(path + "head.jpg");//从Sd中找头像,转换成Bitmap
if (bt != null) {
@SuppressWarnings("deprecation")
Drawable drawable = new BitmapDrawable(bt);//转换成drawable
ivHead.setImageDrawable(drawable);
} else {}
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ig_inco:
showPopupWindow(v);
break;
default:
break;
}}

private void showPopupWindow(View v) {
// 一个自定义的布局,作为显示的内容
View contentView = LayoutInflater.from(mContext).inflate(
R.layout.pop_window, null);
final PopupWindow popupWindow = new PopupWindow(contentView,
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
// 设置按钮的点击事件
Button photos = (Button) contentView.findViewById(R.id.button1);
Button tackphoto = (Button) contentView.findViewById(R.id.button2);
photos.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent1 = new Intent(Intent.ACTION_PICK, null);
intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image
setPicToView(head);//保存在SD卡中
ivHead.setImageBitmap(head);//用ImageView显示出来
}}
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);};


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是否可用
return;}
FileOutputStream b = null;
File file = new File(path);
file.mkdirs();// 创建文件夹
String fileName = path + "head.jpg";//图片名字
try {
b = new FileOutputStream(fileName);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);
// 把数据写入文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
//关闭流
b.flush();
b.close();
} catch (IOException e) {
e.printStackTrace();
}}}}

如果你还有什么不会的可以给我留言,我们一起探讨呦!-

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值