CSV FILE CREATED- with Images Folder, Images also getting downloaded with CSV-FILE
This commit is contained in:
@@ -44,7 +44,7 @@ dependencies {
|
|||||||
|
|
||||||
implementation 'androidx.core:core-ktx:1.12.0'
|
implementation 'androidx.core:core-ktx:1.12.0'
|
||||||
implementation 'androidx.appcompat:appcompat:1.6.1'
|
implementation 'androidx.appcompat:appcompat:1.6.1'
|
||||||
implementation 'com.google.android.material:material:1.10.0'
|
implementation 'com.google.android.material:material:1.11.0'
|
||||||
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
|
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
|
||||||
implementation 'androidx.preference:preference-ktx:1.2.1'
|
implementation 'androidx.preference:preference-ktx:1.2.1'
|
||||||
testImplementation 'junit:junit:4.13.2'
|
testImplementation 'junit:junit:4.13.2'
|
||||||
@@ -77,8 +77,8 @@ dependencies {
|
|||||||
kapt "androidx.hilt:hilt-compiler:1.1.0"
|
kapt "androidx.hilt:hilt-compiler:1.1.0"
|
||||||
|
|
||||||
// Navigation Component
|
// Navigation Component
|
||||||
implementation "androidx.navigation:navigation-fragment-ktx:2.7.5"
|
implementation "androidx.navigation:navigation-fragment-ktx:2.7.6"
|
||||||
implementation "androidx.navigation:navigation-ui-ktx:2.7.5"
|
implementation "androidx.navigation:navigation-ui-ktx:2.7.6"
|
||||||
|
|
||||||
// Retrofit + GSON
|
// Retrofit + GSON
|
||||||
implementation "com.squareup.retrofit2:retrofit:2.9.0"
|
implementation "com.squareup.retrofit2:retrofit:2.9.0"
|
||||||
@@ -88,7 +88,7 @@ dependencies {
|
|||||||
implementation 'com.jakewharton.timber:timber:4.7.1'
|
implementation 'com.jakewharton.timber:timber:4.7.1'
|
||||||
|
|
||||||
// Activity KTX for viewModels()
|
// Activity KTX for viewModels()
|
||||||
implementation "androidx.activity:activity-ktx:1.8.1"
|
implementation "androidx.activity:activity-ktx:1.8.2"
|
||||||
|
|
||||||
//image
|
//image
|
||||||
implementation 'com.github.bumptech.glide:glide:4.13.2'
|
implementation 'com.github.bumptech.glide:glide:4.13.2'
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.example.hpostesting.data.datasource
|
package com.example.hposregistration.data.datasource
|
||||||
|
|
||||||
import android.os.Environment
|
import android.os.Environment
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
@@ -6,8 +6,15 @@ import com.example.hposregistration.data.user.UserData
|
|||||||
|
|
||||||
import com.opencsv.CSVWriter
|
import com.opencsv.CSVWriter
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.io.FileOutputStream
|
||||||
import java.io.FileWriter
|
import java.io.FileWriter
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
import java.io.InputStream
|
||||||
|
import java.net.HttpURLConnection
|
||||||
|
import java.net.URL
|
||||||
|
import java.text.SimpleDateFormat
|
||||||
|
import java.util.Date
|
||||||
|
import java.util.Locale
|
||||||
|
|
||||||
class LocalFileDataSource {
|
class LocalFileDataSource {
|
||||||
|
|
||||||
@@ -31,9 +38,8 @@ class LocalFileDataSource {
|
|||||||
): Boolean {
|
): Boolean {
|
||||||
try {
|
try {
|
||||||
val formattedFileName = fileName.replace(Regex("[^a-zA-Z0-9.-]"), "_")
|
val formattedFileName = fileName.replace(Regex("[^a-zA-Z0-9.-]"), "_")
|
||||||
val filePath = File(getExternalStorageDirectory(), formattedFileName)
|
val csvFilePath = File(getExternalStorageDirectory(), formattedFileName)
|
||||||
val writer = FileWriter(filePath)
|
val csvWriter = CSVWriter(FileWriter(csvFilePath))
|
||||||
val csvWriter = CSVWriter(writer)
|
|
||||||
// Write CSV header
|
// Write CSV header
|
||||||
val header = arrayOf(
|
val header = arrayOf(
|
||||||
"_id",
|
"_id",
|
||||||
@@ -42,7 +48,6 @@ class LocalFileDataSource {
|
|||||||
"birthYear",
|
"birthYear",
|
||||||
"state",
|
"state",
|
||||||
"abhaId",
|
"abhaId",
|
||||||
"userImageURL",
|
|
||||||
"testStatus", // Assuming testStatus is a boolean field
|
"testStatus", // Assuming testStatus is a boolean field
|
||||||
"gender",
|
"gender",
|
||||||
"appVersion",
|
"appVersion",
|
||||||
@@ -64,12 +69,24 @@ class LocalFileDataSource {
|
|||||||
"createdAt",
|
"createdAt",
|
||||||
"registrationCenterName",
|
"registrationCenterName",
|
||||||
"userImageURL"
|
"userImageURL"
|
||||||
|
|
||||||
)
|
)
|
||||||
csvWriter.writeNext(header)
|
csvWriter.writeNext(header)
|
||||||
|
|
||||||
|
// Create a separate folder for user images
|
||||||
|
// val imagesFolder = File(getExternalStorageDirectory(), "Images/${getCurrentDateTime()}")
|
||||||
|
val imagesFolder = File(getExternalStorageDirectory(), "Images")
|
||||||
|
if (!imagesFolder.exists()) {
|
||||||
|
if (!imagesFolder.mkdirs()) {
|
||||||
|
Log.e("CSVExport", "Error creating images folder: ${imagesFolder.absolutePath}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Write data rows
|
// Write data rows
|
||||||
for (data in dataList) {
|
for (data in dataList) {
|
||||||
|
// Download and save the user image
|
||||||
|
val imageUrl = data.userImageURL
|
||||||
|
val localImageFile = downloadImage(imagesFolder, data._id, imageUrl)
|
||||||
|
|
||||||
val row = arrayOf(
|
val row = arrayOf(
|
||||||
data._id,
|
data._id,
|
||||||
data.name,
|
data.name,
|
||||||
@@ -77,7 +94,6 @@ class LocalFileDataSource {
|
|||||||
data.birthYear,
|
data.birthYear,
|
||||||
data.state,
|
data.state,
|
||||||
data.abhaId,
|
data.abhaId,
|
||||||
data.userImageURL,
|
|
||||||
data.testStatus?.toString() ?: "",
|
data.testStatus?.toString() ?: "",
|
||||||
data.gender,
|
data.gender,
|
||||||
data.appVersion ?: "",
|
data.appVersion ?: "",
|
||||||
@@ -98,12 +114,11 @@ class LocalFileDataSource {
|
|||||||
data.registeredBy ?: "",
|
data.registeredBy ?: "",
|
||||||
data.createdAt ?: "",
|
data.createdAt ?: "",
|
||||||
data.registrationCenterName ?: "",
|
data.registrationCenterName ?: "",
|
||||||
data.userImageURL
|
localImageFile?.absolutePath ?: "" // Use the local image file path in the CSV
|
||||||
|
|
||||||
)
|
)
|
||||||
csvWriter.writeNext(row)
|
csvWriter.writeNext(row)
|
||||||
}
|
}
|
||||||
writer.close()
|
csvWriter.close()
|
||||||
return true
|
return true
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
@@ -112,19 +127,40 @@ class LocalFileDataSource {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getExternalStorageDirectory(): File {
|
private fun downloadImage(folder: File, userId: String, imageUrl: String): File? {
|
||||||
val directory = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "HPOSRegistration")
|
return try {
|
||||||
|
val url = URL(imageUrl)
|
||||||
|
val connection = url.openConnection()
|
||||||
|
|
||||||
if (!directory.exists()) {
|
val inputStream: InputStream = connection.inputStream
|
||||||
if (!directory.mkdirs()) {
|
val localImageFile = File(folder, "$userId.jpg")
|
||||||
Log.e("CSVExport", "Error creating directory: ${directory.absolutePath}")
|
val outputStream = FileOutputStream(localImageFile)
|
||||||
|
|
||||||
|
inputStream.use { input ->
|
||||||
|
outputStream.use { fileOut ->
|
||||||
|
input.copyTo(fileOut)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
localImageFile
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
Log.e("ImageDownloader", "Error downloading image: ${e.message}")
|
||||||
|
null
|
||||||
}
|
}
|
||||||
return directory
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private fun getExternalStorageDirectory(): File {
|
||||||
|
return File(
|
||||||
|
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS),
|
||||||
|
"HPOSRegistration"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getCurrentDateTime(): String {
|
||||||
|
val dateFormat = SimpleDateFormat("dd-MM-yy", Locale.getDefault())
|
||||||
|
val currentDateTime = Date()
|
||||||
|
return dateFormat.format(currentDateTime)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,7 @@ import android.content.Context
|
|||||||
import androidx.room.Room
|
import androidx.room.Room
|
||||||
import com.example.hposregistration.dao.MyDatabase
|
import com.example.hposregistration.dao.MyDatabase
|
||||||
import com.example.hposregistration.dao.UserDao
|
import com.example.hposregistration.dao.UserDao
|
||||||
import com.example.hpostesting.data.datasource.LocalFileDataSource
|
import com.example.hposregistration.data.datasource.LocalFileDataSource
|
||||||
import dagger.Module
|
import dagger.Module
|
||||||
import dagger.Provides
|
import dagger.Provides
|
||||||
import dagger.hilt.InstallIn
|
import dagger.hilt.InstallIn
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import com.example.hposregistration.data.NetworkStatusLiveData
|
|||||||
import com.example.hposregistration.data.Response
|
import com.example.hposregistration.data.Response
|
||||||
import com.example.hposregistration.data.user.UserData
|
import com.example.hposregistration.data.user.UserData
|
||||||
import com.example.hposregistration.dao.UserDao
|
import com.example.hposregistration.dao.UserDao
|
||||||
import com.example.hpostesting.data.datasource.LocalFileDataSource
|
import com.example.hposregistration.data.datasource.LocalFileDataSource
|
||||||
import com.google.firebase.storage.FirebaseStorage
|
import com.google.firebase.storage.FirebaseStorage
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|||||||
Reference in New Issue
Block a user