0

I have added support to my app for storing jpg and png but am a bit confused about how i'd get it to work for video and gif files. My app is written in dart/flutter but is using kotlin for the android 10+ file storage since dart/flutter has no support for it. On the flutter side I am getting the file with a http get request and then sending the response body to the kotlin part of the app as a byte array. The first function receives the byte array and converts it to a bitmap which is then written to disc using the second function. How would I add support for video and gif files? The video files I'm writing will most likely be webm and mp4

else if (call.method == "writeImage"){
                var imageData = call.argument<ByteArray>("imageData");
                val fileName = call.argument<String>("fileName");
                val  mediaType = call.argument<String>("mediaType");
                val  fileExt = call.argument<String>("fileExt");
                var bmp = imageData?.size?.let { BitmapFactory.decodeByteArray(imageData,0, it) };

                if (bmp != null && imageData != null && mediaType != null && fileExt != null && fileName != null){
                    print("writing file");
                    writeImage(bmp,fileName,mediaType,fileExt);
                    result.success(fileName);
                } else {
                    print("a value is null");
                    result.success(null);
                }

            }
@Throws(IOException::class)
    private fun writeImage(bitmap: Bitmap, name: String, mediaType: String, fileExt: String) {
        val fos: OutputStream?
        val resolver = contentResolver
        val contentValues = ContentValues()
        contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, "$name.$fileExt")
        contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "$mediaType/$fileExt")
        contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + "/LoliSnatcher/")
        val imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
        if (imageUri != null){
            fos = resolver.openOutputStream(imageUri);
            if (fileExt.toUpperCase() == "PNG"){
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            } else if (fileExt.toUpperCase() == "JPG" || fileExt.toUpperCase() == "JPEG") {
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos)
            }
            Objects.requireNonNull(fos)?.close()
        }
    }

NO_ob
  • 93
  • 9
  • `The first function receives the byte array and converts it to a bitmap` That does not look like a good approach as why would you construct a bitmap out of the bytes of a file where you can save the content of the byte array directly to file? Write them directly to that OutputStream. In this way the type of content does not matter either as they are just the bytes of a file. Be it jpg, giff or pdf. – blackapps Feb 02 '21 at 14:20
  • I got the function from another post here I assumed bitmap was required for the media store thing. How do I add the bytearray to the outputstream fos? – NO_ob Feb 02 '21 at 14:38
  • fos.write (imageData); ? – blackapps Feb 02 '21 at 14:41
  • Okay thankyou it works for gif but not for video. I get "java.lang.IllegalArgumentException: MIME type video/mp4 cannot be inserted into content://media/external/images/media; expected MIME type under image/*" I tried changing the uri to "Environment.DIRECTORY_MOVIES" and also changed the resolver to "MediaStore.Video.Media.EXTERNAL_CONTENT_URI" but that doesn't work – NO_ob Feb 02 '21 at 15:02
  • Well i had expected problems and your attack of that problem looks ok.Try more. At the moment i cannot test or try your code, sorry. Mimetype "video/*" – blackapps Feb 02 '21 at 15:10
  • Read this: https://stackoverflow.com/a/66366102/9917404 – Thoriya Prahalad Feb 25 '21 at 09:58

1 Answers1

0

Fixed with

@Throws(IOException::class)
    private fun writeImage(fileBytes: ByteArray, name: String, mediaType: String, fileExt: String) {
        val fos: OutputStream?
        val resolver = contentResolver
        val contentValues = ContentValues()
        val imageUri: Uri?
        if(mediaType == "image"){
            contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, "$name.$fileExt")
            contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "$mediaType/$fileExt")
            contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + "/LoliSnatcher/")
            imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
        } else {
            contentValues.put(MediaStore.Video.Media.DISPLAY_NAME, "$name.$fileExt")
            contentValues.put(MediaStore.Video.Media.MIME_TYPE, "$mediaType/$fileExt")
            contentValues.put(MediaStore.Video.Media.RELATIVE_PATH, Environment.DIRECTORY_MOVIES + "/LoliSnatcher/")
            imageUri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentValues)
        }

        if (imageUri != null){
            fos = resolver.openOutputStream(imageUri);
            fos?.write(fileBytes);
            Objects.requireNonNull(fos)?.close()
        }
    }
NO_ob
  • 93
  • 9