T
T
Tsuzukeru2021-12-06 17:54:38
Android
Tsuzukeru, 2021-12-06 17:54:38

Is it possible to create a file in Android without saving to the device?

There is such a code. Is it possible to create a file without saving to the device?

@RequiresApi(Build.VERSION_CODES.FROYO)
    fun toFile(body: ResponseBody): File? {
        try {
            val filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).path
            val futureStudioIconFile = File(filePath + File.separator + "${body.hashCode()}.png")
            var inputStream: InputStream? = null
            var outputStream: OutputStream? = null
            try {
                val fileReader = ByteArray(4096)
                var fileSizeDownloaded: Long = 0
                inputStream = body.byteStream()
                outputStream = FileOutputStream(futureStudioIconFile)
                while (true) {
                    val read: Int = inputStream.read(fileReader)
                    if (read == -1) {
                        break
                    }
                    outputStream.write(fileReader, 0, read)
                    fileSizeDownloaded += read.toLong()
                }
                outputStream.flush()
                return futureStudioIconFile
            } catch (e: IOException) {
                return null
            } finally {
                inputStream?.close()
                outputStream?.close()
            }
        } catch (e: IOException) {
            return null
        }
    }

Answer the question

In order to leave comments, you need to log in

4 answer(s)
T
Tsudzukeru, 2021-12-19
@Tsuzukeru

Convert to ByteArray

S
Sergey Vodakov, 2021-12-06
@WaterSmith

A file is data stored on a permanent medium. Until the data is saved, it's not a file, it's data in memory.
So it's not entirely clear what you mean? If you do not save, then this is not a file.

O
Oleg, 2021-12-06
@402d

What is the question about? How to do this within the framework of the current requirements of the play?
Read about alternatives.
https://support.google.com/googleplay/android-deve...

A
alekseyHunter, 2021-12-17
@alekseyHunter

It's all too complicated somehow. If you need to directly store a 100% copy of the image in the database, convert the image to Base64 and save the resulting string in the database. And on the way back, return the picture to the View in Base64 and convert it to a Bitmap. Easy

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question