作者 liyan

添加拍照和打开相册工具类2

1 <?xml version="1.0" encoding="utf-8"?> 1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3 + xmlns:tools="http://schemas.android.com/tools"
3 package="com.br_technology.securitytrain_master"> 4 package="com.br_technology.securitytrain_master">
4 5
5 <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> 6 <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
@@ -7,6 +8,8 @@ @@ -7,6 +8,8 @@
7 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 8 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
8 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 9 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
9 <uses-permission android:name="android.permission.CAMERA" /> 10 <uses-permission android:name="android.permission.CAMERA" />
  11 + <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
  12 + tools:ignore="ScopedStorage" />
10 13
11 <application 14 <application
12 android:name=".base.view.BaseApplication" 15 android:name=".base.view.BaseApplication"
@@ -38,6 +41,16 @@ @@ -38,6 +41,16 @@
38 android:name="notch.config" 41 android:name="notch.config"
39 android:value="portrait|landscape" /> 42 android:value="portrait|landscape" />
40 43
  44 + <provider
  45 + android:name="androidx.core.content.FileProvider"
  46 + android:authorities="${applicationId}.provider"
  47 + android:exported="false"
  48 + android:grantUriPermissions="true">
  49 + <meta-data
  50 + android:name="android.support.FILE_PROVIDER_PATHS"
  51 + android:resource="@xml/file_paths" />
  52 + </provider>
  53 +
41 54
42 <activity 55 <activity
43 android:name=".ui.login.AccountLoginActivity" 56 android:name=".ui.login.AccountLoginActivity"
1 package com.br_technology.securitytrain_master.ui.mine.activity 1 package com.br_technology.securitytrain_master.ui.mine.activity
2 2
3 import android.Manifest 3 import android.Manifest
  4 +import android.content.Intent
4 import android.view.View 5 import android.view.View
5 import android.widget.Toast 6 import android.widget.Toast
6 import com.br_technology.securitytrain_master.R 7 import com.br_technology.securitytrain_master.R
7 import com.br_technology.securitytrain_master.databinding.ActivityEditPersonalInfoBinding 8 import com.br_technology.securitytrain_master.databinding.ActivityEditPersonalInfoBinding
8 import com.br_technology.securitytrain_master.ext.initPermissions 9 import com.br_technology.securitytrain_master.ext.initPermissions
9 import com.br_technology.securitytrain_master.ui.mine.viewmodel.EditPersonalInfoViewModel 10 import com.br_technology.securitytrain_master.ui.mine.viewmodel.EditPersonalInfoViewModel
  11 +import com.br_technology.securitytrain_master.util.PhotoUtils
10 import com.gyf.immersionbar.ImmersionBar 12 import com.gyf.immersionbar.ImmersionBar
11 import com.wjx.android.wanandroidmvvm.base.view.BaseLifeCycleActivity 13 import com.wjx.android.wanandroidmvvm.base.view.BaseLifeCycleActivity
12 14
@@ -51,7 +53,18 @@ class EditPersonalInfoActivity : @@ -51,7 +53,18 @@ class EditPersonalInfoActivity :
51 R.id.rl_up_load_image -> { 53 R.id.rl_up_load_image -> {
52 //上传和修改头像 54 //上传和修改头像
53 initPermissions(Manifest.permission.CAMERA, 55 initPermissions(Manifest.permission.CAMERA,
  56 + Manifest.permission.WRITE_EXTERNAL_STORAGE,
54 onGranted = { 57 onGranted = {
  58 + PhotoUtils.get().photoDialogShow(this,"",object : PhotoUtils.PhotoCallBack{
  59 + override fun onSuccessListener(imagePath: String?) {
  60 + //上传照片到服务器
  61 + }
  62 +
  63 + override fun onFailListener() {
  64 + //失败的回调
  65 + }
  66 +
  67 + })
55 Toast.makeText(baseContext, "ccccc", Toast.LENGTH_SHORT).show() 68 Toast.makeText(baseContext, "ccccc", Toast.LENGTH_SHORT).show()
56 }, 69 },
57 onDenied = { 70 onDenied = {
@@ -62,4 +75,12 @@ class EditPersonalInfoActivity : @@ -62,4 +75,12 @@ class EditPersonalInfoActivity :
62 } 75 }
63 } 76 }
64 } 77 }
  78 +
  79 + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  80 + super.onActivityResult(requestCode, resultCode, data)
  81 + if (resultCode == RESULT_OK){
  82 + PhotoUtils.get().onActivityResult(this,requestCode,resultCode,data!!)
  83 + }
  84 + }
  85 +
65 } 86 }
@@ -30,8 +30,6 @@ import java.io.FileNotFoundException @@ -30,8 +30,6 @@ import java.io.FileNotFoundException
30 * Description: 初见时你很迷人 30 * Description: 初见时你很迷人
31 */ 31 */
32 class PhotoUtils { 32 class PhotoUtils {
33 - @Volatile  
34 - private var mInstance: PhotoUtils? = null  
35 private val REQUEST_IMAGE_ALBUM = 0 33 private val REQUEST_IMAGE_ALBUM = 0
36 private val REQUEST_IMAGE_CAMERA = 1 34 private val REQUEST_IMAGE_CAMERA = 1
37 private val REQUEST_IMAGE_CUTTING = 2 35 private val REQUEST_IMAGE_CUTTING = 2
@@ -43,15 +41,17 @@ class PhotoUtils { @@ -43,15 +41,17 @@ class PhotoUtils {
43 private var mCallBack: PhotoCallBack? = null 41 private var mCallBack: PhotoCallBack? = null
44 42
45 43
46 - fun getInstance(): PhotoUtils? {  
47 - if (mInstance == null) {  
48 - synchronized(PhotoUtils::class.java) {  
49 - if (mInstance == null) {  
50 - mInstance = PhotoUtils() 44 + companion object {
  45 + private var instance:PhotoUtils ? = null
  46 + get() {
  47 + if (field == null){
  48 + field = PhotoUtils()
51 } 49 }
  50 + return field
52 } 51 }
  52 + fun get(): PhotoUtils{
  53 + return instance!!
53 } 54 }
54 - return mInstance  
55 } 55 }
56 56
57 /** 57 /**
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<paths>
  3 + <!-- root-path 手机存储根目录 -->
  4 + <root-path
  5 + name="download"
  6 + path="" />
  7 +</paths>