Android实现上传头像
程序员文章站
2022-06-22 23:02:55
本文实例为大家分享了android实现上传头像的具体代码,供大家参考,具体内容如下上传头像可以从相册获取和拍照1.加入权限
本文实例为大家分享了android实现上传头像的具体代码,供大家参考,具体内容如下
上传头像可以从相册获取和拍照
1.加入权限
<uses-permission android:name="android.permission.camera" /> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.read_external_storage" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.vibrate" /> <uses-permission android:name="android.permission.wake_lock" />
2.编写xml文件
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="mypath" path="dcim"></external-path> </paths>
3.activityd代码
//相机拍照 img.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = new intent(); intent.setaction(mediastore.action_image_capture); path="/sdcard/dcim/camera/"+system.currenttimemillis()+".jpg"; uri uriforfile = fileprovider.geturiforfile(main3activity.this, "com.example.zhoukao3", new file(path)); intent.putextra(mediastore.extra_output,uriforfile); startactivityforresult(intent,111); } }); //从图库获取 img.setonlongclicklistener(new view.onlongclicklistener() { @override public boolean onlongclick(view v) { intent intent = new intent(); intent.setaction(intent.action_pick); intent.settype("image/*"); startactivityforresult(intent,555); return true; } }); //将图片存放在头像位置 @override protected void onactivityresult(int requestcode, int resultcode, @nullable intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode==111&&resultcode== activity.result_ok){ glide.with(this).load(path).transform(new circlecrop()).into(img); }else if (requestcode==555&&resultcode==activity.result_ok){ uri data1 = data.getdata(); glide.with(this).load(data1).transform(new circlecrop()).into(img); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。