Android实现上传头像
本文实例为大家分享了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); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持海外IDC网。
【本文由:日本服务器 欢迎转载】