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.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.preference:preference-ktx:1.2.1'
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
@@ -77,8 +77,8 @@ dependencies {
|
||||
kapt "androidx.hilt:hilt-compiler:1.1.0"
|
||||
|
||||
// Navigation Component
|
||||
implementation "androidx.navigation:navigation-fragment-ktx:2.7.5"
|
||||
implementation "androidx.navigation:navigation-ui-ktx:2.7.5"
|
||||
implementation "androidx.navigation:navigation-fragment-ktx:2.7.6"
|
||||
implementation "androidx.navigation:navigation-ui-ktx:2.7.6"
|
||||
|
||||
// Retrofit + GSON
|
||||
implementation "com.squareup.retrofit2:retrofit:2.9.0"
|
||||
@@ -88,7 +88,7 @@ dependencies {
|
||||
implementation 'com.jakewharton.timber:timber:4.7.1'
|
||||
|
||||
// Activity KTX for viewModels()
|
||||
implementation "androidx.activity:activity-ktx:1.8.1"
|
||||
implementation "androidx.activity:activity-ktx:1.8.2"
|
||||
|
||||
//image
|
||||
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.util.Log
|
||||
@@ -6,8 +6,15 @@ import com.example.hposregistration.data.user.UserData
|
||||
|
||||
import com.opencsv.CSVWriter
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.io.FileWriter
|
||||
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 {
|
||||
|
||||
@@ -31,9 +38,8 @@ class LocalFileDataSource {
|
||||
): Boolean {
|
||||
try {
|
||||
val formattedFileName = fileName.replace(Regex("[^a-zA-Z0-9.-]"), "_")
|
||||
val filePath = File(getExternalStorageDirectory(), formattedFileName)
|
||||
val writer = FileWriter(filePath)
|
||||
val csvWriter = CSVWriter(writer)
|
||||
val csvFilePath = File(getExternalStorageDirectory(), formattedFileName)
|
||||
val csvWriter = CSVWriter(FileWriter(csvFilePath))
|
||||
// Write CSV header
|
||||
val header = arrayOf(
|
||||
"_id",
|
||||
@@ -42,7 +48,6 @@ class LocalFileDataSource {
|
||||
"birthYear",
|
||||
"state",
|
||||
"abhaId",
|
||||
"userImageURL",
|
||||
"testStatus", // Assuming testStatus is a boolean field
|
||||
"gender",
|
||||
"appVersion",
|
||||
@@ -64,12 +69,24 @@ class LocalFileDataSource {
|
||||
"createdAt",
|
||||
"registrationCenterName",
|
||||
"userImageURL"
|
||||
|
||||
)
|
||||
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
|
||||
for (data in dataList) {
|
||||
// Download and save the user image
|
||||
val imageUrl = data.userImageURL
|
||||
val localImageFile = downloadImage(imagesFolder, data._id, imageUrl)
|
||||
|
||||
val row = arrayOf(
|
||||
data._id,
|
||||
data.name,
|
||||
@@ -77,7 +94,6 @@ class LocalFileDataSource {
|
||||
data.birthYear,
|
||||
data.state,
|
||||
data.abhaId,
|
||||
data.userImageURL,
|
||||
data.testStatus?.toString() ?: "",
|
||||
data.gender,
|
||||
data.appVersion ?: "",
|
||||
@@ -98,12 +114,11 @@ class LocalFileDataSource {
|
||||
data.registeredBy ?: "",
|
||||
data.createdAt ?: "",
|
||||
data.registrationCenterName ?: "",
|
||||
data.userImageURL
|
||||
|
||||
localImageFile?.absolutePath ?: "" // Use the local image file path in the CSV
|
||||
)
|
||||
csvWriter.writeNext(row)
|
||||
}
|
||||
writer.close()
|
||||
csvWriter.close()
|
||||
return true
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
@@ -112,19 +127,40 @@ class LocalFileDataSource {
|
||||
}
|
||||
}
|
||||
|
||||
private fun getExternalStorageDirectory(): File {
|
||||
val directory = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "HPOSRegistration")
|
||||
private fun downloadImage(folder: File, userId: String, imageUrl: String): File? {
|
||||
return try {
|
||||
val url = URL(imageUrl)
|
||||
val connection = url.openConnection()
|
||||
|
||||
if (!directory.exists()) {
|
||||
if (!directory.mkdirs()) {
|
||||
Log.e("CSVExport", "Error creating directory: ${directory.absolutePath}")
|
||||
val inputStream: InputStream = connection.inputStream
|
||||
val localImageFile = File(folder, "$userId.jpg")
|
||||
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 com.example.hposregistration.dao.MyDatabase
|
||||
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.Provides
|
||||
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.user.UserData
|
||||
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 dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
Reference in New Issue
Block a user