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. 如何基于python3和Vue實(shí)現(xiàn)AES數(shù)據(jù)加密2. 10個(gè)提供免費(fèi)PHP腳本下載的網(wǎng)站3. PHP擴(kuò)展之APC——Alternative PHP Cache(可選PHP緩存)4. PHP設(shè)計(jì)模式(四)原型模式Prototype實(shí)例詳解【創(chuàng)建型】5. Java 基于UDP協(xié)議實(shí)現(xiàn)消息發(fā)送6. Python編寫nmap掃描工具7. Java向Runnable線程傳遞參數(shù)方法實(shí)例解析8. python 爬取嗶哩嗶哩up主信息和投稿視頻9. ASP.NET MVC前臺(tái)動(dòng)態(tài)添加文本框并在后臺(tái)使用FormCollection接收值10. php5.6不能擴(kuò)展redis.so的解決方法
