Short introduction, this is very simple image picker, that allows you to pick image file from gallery or camera, you can integrate your own Uploader ViewModel using this library for upload process using your File Storage API
Read the terms https://github.com/muhammadisa/epict/blob/master/CONTRIBUTING.md
For installation just add this code in your app build.gradle file
implementation 'com.github.muhammadisa:epict:1.0.2'
-
add /res/xml/file_paths.xml in app level res directory
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="images" path="/"/> </paths>
-
add this code inside AndroidManifest.xml application tag
<application ...> <provider android:name="androidx.core.content.FileProvider" android:authorities="com.your.app.package.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> </application>
Just click this link https://github.com/muhammadisa/epict/tree/master/example/src/main/java/com/xoxoer/example
// epict data creation
val data = EpictData(
"Choose",
"Take photo profile from?",
"com.your.app.package.fileprovider",
ImageShape.CIRCLE
)
ImageShape only have 2 shape CIRCLE & SQUARE
// epict views creation
val views = EpictViews(
image_view_result, // this is component from your layout screen for views image result
button_upload, // this is component from your layout screen for call image picker dialog
button_retract // this is component from your layout screen for retract or remove picked image
)
// define lateinit var inside activity class
private lateinit var epict: Epict
// initialize it inside override function onCreate
epict = Epict.Builder()
.context(this) // required
.viewModel(PhotoUploaderViewModel()) // optional you can delete
.data(data) // required
.views(views) // required
.build()
// class must be implement EpictViewModelContract interface
class PhotoUploaderViewModel : ViewModel(), EpictViewModelContract {
override var fileAbsolutePath = ObservableField<String>()
override var fileToBeUploaded = ObservableField<File>()
override var uploadedFileObjectName = MutableLiveData<String>()
override var uploadedFileUrl = MutableLiveData<String>()
override fun uploadImageToServer() {
// replace this code with uploader API interaction
GlobalScope.launch {
// delay used for pretend like uploading image to server
delay(1000)
uploadedFileUrl.postValue(fileAbsolutePath.get())
uploadedFileObjectName.postValue(fileToBeUploaded.get()?.name)
}
}
override fun retractImageFromServer() {
// replace this code with uploader API interaction
GlobalScope.launch {
// delay used for pretend like retracting image from server
delay(1000)
uploadedFileUrl.postValue(null)
uploadedFileObjectName.postValue(null)
}
}
}
class MainActivity : AppCompatActivity() {
// epict lateinit var
private lateinit var epict: Epict
private lateinit var photoUploaderViewModel: PhotoUploaderViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
photoUploaderViewModel = PhotoUploaderViewModel()
val data = EpictData(
"Choose",
"Take photo profile from?",
"com.xoxoer.example.fileprovider",
ImageShape.CIRCLE
)
val views = EpictViews(
image_view_result,
button_upload,
button_retract
)
epict = Epict.Builder()
.context(this) // required
.viewModel(photoUploaderViewModel)
.data(data) // required
.views(views) // required
.build()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
CAMERA_REQUEST_CODE -> if (resultCode == Activity.RESULT_OK) {
// usage of pick from camera
epict.upload()
text_view_file_absolute.text = "Absolute Path : ${photoUploaderViewModel.fileToBeUploaded.get()!!.absolutePath}"
text_view_image_uri.text = "URI : ${photoUploaderViewModel.fileAbsolutePath.get()!!}"
Log.e("FILE", photoUploaderViewModel.fileToBeUploaded.get()!!.absolutePath)
Log.e("ABSOLUTE", photoUploaderViewModel.fileAbsolutePath.get()!!)
}
GALLERY_REQUEST_CODE -> if (resultCode == Activity.RESULT_OK) {
// usage of pick from gallery
epict.getRealPathFromURI(data?.data)
epict.upload()
text_view_file_absolute.text = "Absolute Path : ${photoUploaderViewModel.fileToBeUploaded.get()!!.absolutePath}"
text_view_image_uri.text = "URI : ${photoUploaderViewModel.fileAbsolutePath.get()!!}"
Log.e("FILE", photoUploaderViewModel.fileToBeUploaded.get()!!.absolutePath)
Log.e("ABSOLUTE", photoUploaderViewModel.fileAbsolutePath.get()!!)
}
}
}
}
class MainActivity : AppCompatActivity() {
// epict lateinit var
private lateinit var epict: Epict
// for store file and absolute path file
private lateinit var file: File
private lateinit var absoluteFile: String
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val data = EpictData(
"Choose",
"Take photo profile from?",
"com.your.app.package.fileprovider",
ImageShape.CIRCLE
)
val views = EpictViews(
image_view_result,
button_upload,
button_retract
)
epict = Epict.Builder()
.context(this) // required
.data(data) // required
.views(views) // required
.build()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
CAMERA_REQUEST_CODE -> if (resultCode == Activity.RESULT_OK) {
// usage of pick from camera
file = epict.fileResult.get()!!
absoluteFile = epict.fileAbsolutePath.get()!!
text_view_file_absolute.text = "Absolute Path : ${file.absolutePath}"
text_view_image_uri.text = "URI : $absoluteFile"
Log.e("FILE", file.absolutePath)
Log.e("ABSOLUTE", absoluteFile)
}
GALLERY_REQUEST_CODE -> if (resultCode == Activity.RESULT_OK) {
// usage of pick from gallery
epict.getRealPathFromURI(data?.data)
file = epict.fileResult.get()!!
absoluteFile = epict.fileAbsolutePath.get()!!
text_view_file_absolute.text = "Absolute Path : ${file.absolutePath}"
text_view_image_uri.text = "URI : $absoluteFile"
Log.e("FILE", file.absolutePath)
Log.e("ABSOLUTE", absoluteFile)
}
}
}
}
👤 Muhammad Isa Wijaya Kusuma
- Github: @muhammadisa
Give a ⭐️ if this project helped you!
This README was generated with ❤️ by readme-md-generator