Android實(shí)現(xiàn)上傳頭像
本文實(shí)例為大家分享了Android實(shí)現(xiàn)上傳頭像的具體代碼,供大家參考,具體內(nèi)容如下
上傳頭像可以從相冊(cè)獲取和拍照
1.加入權(quán)限<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-pathname='mypath'path='DCIM'></external-path></paths>
3.activityd代碼
//相機(jī)拍照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); }});//從圖庫(kù)獲取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);}}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python的Tqdm模塊實(shí)現(xiàn)進(jìn)度條配置2. react axios 跨域訪問(wèn)一個(gè)或多個(gè)域名問(wèn)題3. WML語(yǔ)言的基本情況4. CSS代碼檢查工具stylelint的使用方法詳解5. 利用CSS制作3D動(dòng)畫6. Python 多線程之threading 模塊的使用7. 刪除docker里建立容器的操作方法8. .NET6打包部署到Windows Service的全過(guò)程9. ThinkPHP5 通過(guò)ajax插入圖片并實(shí)時(shí)顯示(完整代碼)10. Properties 持久的屬性集的實(shí)例詳解
