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-pathname='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);}}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章: