Last active
May 17, 2021 10:16
-
-
Save KryptKode/53567abf6834176d57fd62d7d0a6d225 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface FileManager { | |
suspend fun checkIfFileExists(name: String): Boolean | |
fun saveFile(name: String, content: String): Boolean | |
fun readExternalFile(name: String): String | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.ContentValues | |
import android.content.Context | |
import android.database.Cursor | |
import android.net.Uri | |
import android.os.Build | |
import android.provider.BaseColumns | |
import android.provider.MediaStore | |
import android.util.Log | |
import androidx.annotation.RequiresApi | |
import kotlinx.coroutines.Dispatchers | |
import kotlinx.coroutines.withContext | |
import java.io.FileNotFoundException | |
/** | |
Implementation of FileManager for Android SDK version >= 10 that would save files to the Downloads directory | |
*/ | |
@RequiresApi(Build.VERSION_CODES.Q) | |
class PostQFileManager(context: Context) : FileManager { | |
private val contentResolver = context.contentResolver | |
override suspend fun checkIfFileExists(name: String): Boolean { | |
return withContext(Dispatchers.IO) { | |
try { | |
readExternalFile(name).isNotEmpty() | |
} catch (e: Exception) { | |
Log.e(TAG, "checkIfFileExists: ", e) | |
false | |
} | |
} | |
} | |
override fun saveFile(name: String, content: String): Boolean { | |
val values = ContentValues() | |
values.put(MediaStore.MediaColumns.DISPLAY_NAME, name) | |
val uri = contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, values) | |
if (uri != null) { | |
val outputStream = contentResolver.openOutputStream(uri) | |
return try { | |
outputStream.use { | |
it?.write(content.toByteArray()) | |
true | |
} | |
} catch (e: Exception) { | |
Log.e(TAG, "saveFile: ", e) | |
false | |
} | |
} | |
return false | |
} | |
override fun readExternalFile(name: String): String { | |
val selection = "${MediaStore.MediaColumns.DISPLAY_NAME} = ?" | |
val selectionArgs = arrayOf(name) | |
val uri = contentResolver.query( | |
MediaStore.Downloads.EXTERNAL_CONTENT_URI, | |
arrayOf(BaseColumns._ID), selection, selectionArgs, null | |
)?.use { | |
fromCursorToUri(it) | |
} | |
Log.e(TAG, "uri: $uri") | |
if (uri != null) { | |
try { | |
val inputStream = contentResolver.openInputStream(uri) | |
inputStream ?: return "" | |
val data = String(inputStream.readBytes(), Charsets.UTF_8) | |
inputStream.close() | |
Log.e(TAG, "read: $data") | |
return data | |
} catch (e: FileNotFoundException) { | |
Log.e(TAG, "readExternalFile: ", e) | |
return "" | |
} | |
} | |
return "" | |
} | |
private fun fromCursorToUri(cursor: Cursor): Uri? { | |
return if (cursor.moveToFirst()) { | |
val mediaId = cursor.getString(cursor.getColumnIndex(BaseColumns._ID)) | |
MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY) | |
.buildUpon().appendPath(mediaId).build() | |
} else null | |
} | |
companion object { | |
private const val TAG = "FileManager" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Environment | |
import android.util.Log | |
import java.io.File | |
import java.io.FileInputStream | |
import java.io.FileOutputStream | |
/** | |
Implementation of FileManager for Android SDK version < 10 that would save files to the Downloads directory | |
*/ | |
class PreQFileManager : FileManager { | |
@Suppress("DEPRECATION") | |
private val rootFile = | |
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) | |
override suspend fun checkIfFileExists(name: String): Boolean { | |
val file = getFile(name) | |
return file.exists() | |
} | |
override fun readExternalFile(name: String): String { | |
val file = getFile(name) | |
val fis = FileInputStream(file) | |
return try { | |
String(fis.readBytes(), Charsets.UTF_8) | |
} catch (e: Exception) { | |
Log.e(TAG, "readExternalFile: ", e) | |
"" | |
} finally { | |
fis.close() | |
} | |
} | |
override fun saveFile(name: String, content: String): Boolean { | |
val fileToSave = getFile(name) | |
val fos = FileOutputStream(fileToSave) | |
return try { | |
fos.write(content.toByteArray()) | |
fos.flush() | |
true | |
} catch (e: Exception) { | |
Log.e(TAG, "writeContent: ", e) | |
false | |
} finally { | |
fos.close() | |
} | |
} | |
private fun getFile(relativePath: String): File { | |
return File(rootFile, relativePath) | |
} | |
companion object { | |
private const val TAG = "PreQFileManager" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment