Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
U
uv-amse-android
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Lucas NAURY
uv-amse-android
Commits
bad02fad
Commit
bad02fad
authored
Feb 16, 2024
by
Lucas NAURY
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ajout des images de la galerie et de l'appareil photo dans la page d'ajout
parent
7c54dcf2
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
132 additions
and
5 deletions
+132
-5
AjoutAnnonceFragment.java
...le/tpleboncoin/ui/ajout_annonce/AjoutAnnonceFragment.java
+89
-0
activity_main.xml
app/src/main/res/layout/activity_main.xml
+1
-2
fragment_ajout_annonce.xml
app/src/main/res/layout/fragment_ajout_annonce.xml
+42
-3
No files found.
app/src/main/java/com/example/tpleboncoin/ui/ajout_annonce/AjoutAnnonceFragment.java
View file @
bad02fad
package
com
.
example
.
tpleboncoin
.
ui
.
ajout_annonce
;
package
com
.
example
.
tpleboncoin
.
ui
.
ajout_annonce
;
import
android.app.Activity
;
import
android.content.Intent
;
import
android.content.Intent
;
import
android.graphics.Bitmap
;
import
android.net.Uri
;
import
android.os.Bundle
;
import
android.os.Bundle
;
import
android.provider.MediaStore
;
import
android.util.Log
;
import
android.view.LayoutInflater
;
import
android.view.LayoutInflater
;
import
android.view.View
;
import
android.view.View
;
import
android.view.ViewGroup
;
import
android.view.ViewGroup
;
import
android.widget.Button
;
import
android.widget.Button
;
import
android.widget.EditText
;
import
android.widget.EditText
;
import
android.widget.ImageView
;
import
android.widget.TextView
;
import
android.widget.TextView
;
import
androidx.activity.result.ActivityResult
;
import
androidx.activity.result.ActivityResultCallback
;
import
androidx.activity.result.ActivityResultLauncher
;
import
androidx.activity.result.contract.ActivityResultContracts
;
import
androidx.annotation.NonNull
;
import
androidx.annotation.NonNull
;
import
androidx.fragment.app.Fragment
;
import
androidx.fragment.app.Fragment
;
import
androidx.lifecycle.ViewModelProvider
;
import
androidx.lifecycle.ViewModelProvider
;
...
@@ -23,6 +33,8 @@ public class AjoutAnnonceFragment extends Fragment {
...
@@ -23,6 +33,8 @@ public class AjoutAnnonceFragment extends Fragment {
private
FragmentAjoutAnnonceBinding
binding
;
private
FragmentAjoutAnnonceBinding
binding
;
private
ActivityResultLauncher
<
Intent
>
activityResultLauncher
;
public
View
onCreateView
(
@NonNull
LayoutInflater
inflater
,
public
View
onCreateView
(
@NonNull
LayoutInflater
inflater
,
ViewGroup
container
,
Bundle
savedInstanceState
)
{
ViewGroup
container
,
Bundle
savedInstanceState
)
{
AjoutAnnonceViewModel
ajout_annonceViewModel
=
AjoutAnnonceViewModel
ajout_annonceViewModel
=
...
@@ -59,6 +71,63 @@ public class AjoutAnnonceFragment extends Fragment {
...
@@ -59,6 +71,63 @@ public class AjoutAnnonceFragment extends Fragment {
}
}
});
});
final
Button
boutonAppareilPhoto
=
binding
.
photoBtn
;
final
Button
boutonGalerie
=
binding
.
galerieBtn
;
final
ImageView
imageAnnonce
=
binding
.
imageAnnonce
;
boutonGalerie
.
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
v
)
{
choisirImageDansGalerie
();
}
});
boutonAppareilPhoto
.
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
v
)
{
prendrePhoto
();
}
});
activityResultLauncher
=
registerForActivityResult
(
new
ActivityResultContracts
.
StartActivityForResult
(),
new
ActivityResultCallback
<
ActivityResult
>()
{
@Override
public
void
onActivityResult
(
ActivityResult
result
)
{
if
(
result
.
getResultCode
()
==
Activity
.
RESULT_OK
)
{
Intent
data
=
result
.
getData
();
if
(
data
!=
null
)
{
// On récupère l'URL de l'image
Uri
selectedImageUri
=
data
.
getData
();
if
(
selectedImageUri
!=
null
)
{
// S'il y a bien une image, on l'affiche à l'écran
imageAnnonce
.
setImageURI
(
selectedImageUri
);
imageAnnonce
.
setVisibility
(
View
.
VISIBLE
);
}
else
{
// S'il n'y a pas de chemin, c'est que l'image est stockée en BMP
// dans les données du résultat (appareil photo)
Bitmap
photo
=
(
Bitmap
)
data
.
getExtras
().
get
(
"data"
);
if
(
photo
==
null
)
{
return
;
}
// S'il y a bien une photo, on l'affiche à l'écran
imageAnnonce
.
setImageBitmap
(
photo
);
imageAnnonce
.
setVisibility
(
View
.
VISIBLE
);
}
}
}
}
});
return
root
;
return
root
;
}
}
...
@@ -67,4 +136,24 @@ public class AjoutAnnonceFragment extends Fragment {
...
@@ -67,4 +136,24 @@ public class AjoutAnnonceFragment extends Fragment {
super
.
onDestroyView
();
super
.
onDestroyView
();
binding
=
null
;
binding
=
null
;
}
}
// Gestion des images
private
void
choisirImageDansGalerie
(){
// Create an instance of the Intent of the type image
Intent
i
=
new
Intent
();
i
.
setType
(
"image/*"
);
i
.
setAction
(
Intent
.
ACTION_GET_CONTENT
);
// Lancement de l'activité qui permet de sélectionner une image dans la galerie
activityResultLauncher
.
launch
(
Intent
.
createChooser
(
i
,
"Select Picture"
));
}
private
void
prendrePhoto
(){
Intent
camera_intent
=
new
Intent
(
MediaStore
.
ACTION_IMAGE_CAPTURE
);
// Lancement de l'activité qui permet de prendre une photo
activityResultLauncher
.
launch
(
Intent
.
createChooser
(
camera_intent
,
"Take Picture"
));
}
}
}
\ No newline at end of file
app/src/main/res/layout/activity_main.xml
View file @
bad02fad
...
@@ -3,8 +3,7 @@
...
@@ -3,8 +3,7 @@
xmlns:app=
"http://schemas.android.com/apk/res-auto"
xmlns:app=
"http://schemas.android.com/apk/res-auto"
android:id=
"@+id/container"
android:id=
"@+id/container"
android:layout_width=
"match_parent"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
android:layout_height=
"match_parent"
>
android:paddingTop=
"?attr/actionBarSize"
>
<com.google.android.material.bottomnavigation.BottomNavigationView
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id=
"@+id/nav_view"
android:id=
"@+id/nav_view"
...
...
app/src/main/res/layout/fragment_ajout_annonce.xml
View file @
bad02fad
...
@@ -28,11 +28,50 @@
...
@@ -28,11 +28,50 @@
android:layout_height=
"wrap_content"
android:layout_height=
"wrap_content"
android:minHeight=
"@dimen/activity_vertical_margin"
/>
android:minHeight=
"@dimen/activity_vertical_margin"
/>
<
ImageButton
<
TextView
android:id=
"@+id/
imageButton
"
android:id=
"@+id/
titreImage
"
android:layout_width=
"match_parent"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:layout_height=
"wrap_content"
android:src=
"@drawable/ic_baseline_add_box_24"
/>
android:fontFamily=
"sans-serif-medium"
android:text=
"Image :"
android:textAppearance=
"@style/TextAppearance.AppCompat.Medium"
/>
<ImageView
android:id=
"@+id/imageAnnonce"
android:layout_width=
"match_parent"
android:layout_height=
"162dp"
android:visibility=
"gone"
tools:src=
"@tools:sample/backgrounds/scenic"
/>
<LinearLayout
android:id=
"@+id/imagesBtnContainer"
android:layout_width=
"match_parent"
android:layout_height=
"58dp"
android:orientation=
"horizontal"
>
<Button
android:id=
"@+id/photoBtn"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
android:text=
"Photo"
app:icon=
"@drawable/ic_baseline_add_a_photo_24"
app:iconGravity=
"textStart"
/>
<Space
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
/>
<Button
android:id=
"@+id/galerieBtn"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
android:text=
"Galerie"
app:icon=
"@drawable/ic_baseline_add_photo_alternate_24"
app:iconGravity=
"textStart"
/>
</LinearLayout>
<Space
<Space
android:layout_width=
"match_parent"
android:layout_width=
"match_parent"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment