I have a stupid bug in my app.
I can copy
contents of a folder from a folder like Downloads
to root of my app in android, data,...
But
I can not copy contents of a folder from root of my app in android, data,...
to a folder like Downloads
My code(that does work in android 11):
var this_dir = File("/storage/emulated/0/Download/Sandoghcheh")
var target_dir = File(requireActivity().getExternalFilesDir(null), "/backup")
copyFolder(this_dir, target_dir)
My code(that does not work in android 11):
var this_dir = File(requireActivity().getExternalFilesDir(null), "/backup")
var target_dir = File("/storage/emulated/0/Download/Sandoghcheh")
copyFolder(this_dir, target_dir)
permisions:
<uses-permission
android:name="android.permission.WRITE_SETTINGS" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="31" />
<uses-permission
android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
fun:
fun copyFolder(source: File, destination: File) {
if (source.isDirectory) {
if (!destination.exists()) {
destination.mkdirs()
}
val files = source.list()
for (file in files) {
val srcFile = File(source, file)
val destFile = File(destination, file)
copyFolder(srcFile, destFile)
}
} else {
var `in`: InputStream? = null
var out: OutputStream? = null
try {
`in` = FileInputStream(source)
out = FileOutputStream(destination)
val buffer = ByteArray(1024)
var length: Int
while (`in`.read(buffer).also { length = it } > 0) {
out.write(buffer, 0, length)
}
} catch (e: Exception) {
try {
if (`in` != null) {
`in`.close()
}
} catch (e1: IOException) {
e1.printStackTrace()
}
try {
if (out != null) {
out.close()
}
} catch (e1: IOException) {
e1.printStackTrace()
}
}
}
}
reatore fun:
private fun restore() {
if (Build.VERSION.SDK_INT < 30) {
createDirectory("backup")
val file = File("/storage/emulated/0/Download/Sandoghcheh")
val target_dir = File(requireActivity().getExternalFilesDir(null), "/backup")
copyFolder(target_dir,file )
} else {
val file = File("/storage/emulated/0/Download/Sandoghcheh")
val target_dir = File(requireActivity().getExternalFilesDir(null), "/backup")
copyFolder(file, target_dir)
}
activity?.let {
RoomBackup()
.customRestoreDialogTitle("یکی از بشتیبان ها را انتخاب کنید")
.context(it)
.database(UserDatabase.getInstance(it))
.enableLogDebug(true)
.backupIsEncrypted(true)
.customEncryptPassword("Amir")
.useExternalStorage(true)
.apply {
onCompleteListener { success, message ->
Log.d("TAG", "success: $success, message: $message")
if (success) restartApp(Intent(context, SplashActivity::class.java))
}
}
.restore()
}
}
backup fun:
private fun backup() {
context?.let {
RoomBackup()
.context(it)
.database(UserDatabase.getInstance(it))
.enableLogDebug(true)
.backupIsEncrypted(true)
.customEncryptPassword("Amir")
.useExternalStorage(true)
.maxFileCount(5)
.apply {
onCompleteListener { success, message ->
}
}
.backup()
}
if (Build.VERSION.SDK_INT < 30) {
if (ContextCompat.checkSelfPermission(
requireActivity(),
Manifest.permission.WRITE_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED
) {
var this_dir = File(requireActivity().getExternalFilesDir(null), "/backup")
var target_dir = File("/storage/emulated/0/Download/Sandoghcheh")
copyFolder(this_dir, target_dir)
} else {
askPermission()
}
var this_dir = File(requireActivity().getExternalFilesDir(null), "/backup")
var target_dir =
File("/storage/emulated/0/Sandoghcheh")
copyFolder(this_dir, target_dir)
} else {
val download_folder =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
var this_dir = File(requireActivity().getExternalFilesDir(null), "/backup")
var target_dir =download_folder
copyFolder(target_dir, this_dir)
Log.d("TAG", "backup: $download_folder,,$this_dir")
// restartApp()
}
}
and last but not the least: when I get this_dir and target_dir path it show me:
2022-01-21 13:41:07.938 23449-23449/com.example.holyquran D/TAG: backup: /storage/emulated/0/Download |||| /storage/emulated/0/Android/data/com.example.holyquran/files/backup
Restore FUN WORKS PERFECT, IT COPIES BUT BACK UP FUN DOES NOT COPY
as I said this code is not working in API +30
TNX for spending time on this Q :)