diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8cd83cd6b8395ebe55c20df5039a45cf7b1ac0f5..a0d73de547f2aff884394ddc5540f34b13b5ddbb 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -15,6 +15,15 @@ android:supportsRtl="true" android:theme="@style/Theme.DM_td2"> <activity android:name=".userInfo.UserInfoActivity"></activity> + <provider + android:name="androidx.core.content.FileProvider" + android:authorities="${applicationId}.fileprovider" + android:exported="false" + android:grantUriPermissions="true"> + <meta-data + android:name="android.support.FILE_PROVIDER_PATHS" + android:resource="@xml/file_paths" /> + </provider> <activity android:name=".task.TaskActivity" /> <activity android:name=".MainActivity"> <intent-filter> diff --git a/app/src/main/java/com/clemhaowen/dm_td2/tasklist/TaskListFragment.kt b/app/src/main/java/com/clemhaowen/dm_td2/tasklist/TaskListFragment.kt index b46cf70d186f4945d13d76e01033f6addc8a047c..08bf294fd5cd48b6bb03a58bc4e6b53043df2c3c 100644 --- a/app/src/main/java/com/clemhaowen/dm_td2/tasklist/TaskListFragment.kt +++ b/app/src/main/java/com/clemhaowen/dm_td2/tasklist/TaskListFragment.kt @@ -137,16 +137,15 @@ class TaskListFragment : Fragment() { override fun onResume() { super.onResume() - val profilPhoto = view?.findViewById<ImageView>(R.id.imageViewProfilPhoto) - profilPhoto?.load("https://goo.gl/gEgYUd") { - crossfade(true) - transformations(CircleCropTransformation()) - } lifecycleScope.launch { val userInfo = Api.userService.getInfo().body()!! val textView = view?.findViewById<TextView>(R.id.textViewInfo) textView?.text = "${userInfo.firstName} ${userInfo.lastName}" - + val profilPhoto = view?.findViewById<ImageView>(R.id.imageViewProfilPhoto) + profilPhoto?.load( userInfo?.avatar /*"https://goo.gl/gEgYUd"*/) { + crossfade(true) + transformations(CircleCropTransformation()) + } // TODO comment this when refactoring is done. //tasksRepository.refresh() diff --git a/app/src/main/java/com/clemhaowen/dm_td2/userInfo/UserInfoActivity.kt b/app/src/main/java/com/clemhaowen/dm_td2/userInfo/UserInfoActivity.kt index 7080a8dc94eb52ababf6115f130bbe0e379c7966..93ace1b50c77f1c65537fe5c83b97471397b5c2c 100644 --- a/app/src/main/java/com/clemhaowen/dm_td2/userInfo/UserInfoActivity.kt +++ b/app/src/main/java/com/clemhaowen/dm_td2/userInfo/UserInfoActivity.kt @@ -30,6 +30,11 @@ import java.io.File class UserInfoActivity : AppCompatActivity() { private val userService = Api.userService + // register + private val pickInGallery = registerForActivityResult(ActivityResultContracts.GetContent()) { + lifecycleScope.launch { handleImage(it) } + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_user_info) @@ -37,6 +42,12 @@ class UserInfoActivity : AppCompatActivity() { buttonToTakePicture.setOnClickListener{ askCameraPermissionAndOpenCamera() } + + val buttonToRetrieveLocalImage = findViewById<Button>(R.id.upload_image_button) + buttonToRetrieveLocalImage.setOnClickListener { + pickInGallery.launch("image/*") + } + lifecycleScope.launch { val userInfoResponse = userService.getInfo() if (userInfoResponse.isSuccessful){ @@ -84,11 +95,15 @@ class UserInfoActivity : AppCompatActivity() { ) private suspend fun handleImage(photoUri: Uri){ - userService.updateAvatar(convert(photoUri)) + val userInfoResponse = userService.updateAvatar(convert(photoUri)) + if (userInfoResponse.isSuccessful){ + val imageView = findViewById<ImageView>(R.id.image_view) + imageView.load(userInfoResponse.body()?.avatar) + } } // use - private fun openCamera() = takePicture.launch() + //private fun openCamera() = takePicture.launch() // register private val takePicture = registerForActivityResult(ActivityResultContracts.TakePicturePreview()) { bitmap -> @@ -99,41 +114,22 @@ class UserInfoActivity : AppCompatActivity() { lifecycleScope.launch { handleImage(tmpFile.toUri()) } } - - - - - - - /* // create a temp file and get a uri for it - private val photoUri = FileProvider.getUriForFile( - this, - BuildConfig.APPLICATION_ID +".fileProvider", - File.createTempFile("avatar", "jpeg") - ) + private val photoUri by lazy { + FileProvider.getUriForFile( + this, + BuildConfig.APPLICATION_ID +".fileprovider", + File.createTempFile("avatar", ".jpeg", externalCacheDir) + + ) + } // register - private val takePicture = - registerForActivityResult(ActivityResultContracts.TakePicture()) { success -> + private val takePicture2 = registerForActivityResult(ActivityResultContracts.TakePicture()) { success -> if (success) lifecycleScope.launch { handleImage(photoUri) } - else Toast.makeText(this, "Si vous refusez, on peux pas prendre de photo ! 😢", Toast.LENGTH_LONG).show() + else Toast.makeText(this, "Erreur ! 😢", Toast.LENGTH_LONG).show() } // use - private fun openCamera() = takePicture.launch(photoUri) - - // convert - private fun convert(uri: Uri) = - MultipartBody.Part.createFormData( - name = "avatar", - filename = "temp.jpeg", - body = uri.toFile().asRequestBody() - ) - - // register - private val pickInGallery = registerForActivityResult(ActivityResultContracts.GetContent()) { uri -> lifecycleScope.launch { handleImage(uri) } } - - // use*/ - //private fun uploadFromGallery() = pickInGallery.launch("image/*") + private fun openCamera() = takePicture2.launch(photoUri) } \ No newline at end of file diff --git a/app/src/main/res/xml/file_paths.xml b/app/src/main/res/xml/file_paths.xml new file mode 100644 index 0000000000000000000000000000000000000000..ee4e471c6012873869be268770937111c2b68225 --- /dev/null +++ b/app/src/main/res/xml/file_paths.xml @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="utf-8"?> +<paths > + <external-path name="external_files" path="."/> +</paths> \ No newline at end of file