当我尝试将gif uri放入文件中以上传到Firebase存储时,FileInputStream无法正常工作

问题描述

我正在尝试将选定的gif from giphy dialog fragment上传到Firebase存储。供参考,我使用的是giphy API

问题是输入/输出不起作用,我的应用仅加载了空的filenameGif而没有输入的网址。

 override fun onGifSelected(media: Media,searchTerm: String?,selectedContentType: GPHContentType){

 gif_url = image!!.gifUrl

 fileNameGif = Environment.getExternalStorageDirectory().absolutePath + File.separator + "/gif"

        if (fileNameGif == null) {
            return;
        }
        try {
            val output = FileOutputStream(fileNameGif)
            val input = FileInputStream(gif_url)
            val inputChannel: FileChannel = input.getChannel()
            val outputChannel: FileChannel = output.getChannel()
            inputChannel.transferTo(0,inputChannel.size(),outputChannel)
            output.close()
            input.close()
            Toast.makeText(this@ChatActivity,"gif selected",Toast.LENGTH_SHORT).show()
        } catch (e: FileNotFoundException) {
            e.printStackTrace()
        } catch (e: IOException) {
            e.printStackTrace()
        }  
}

在这里我尝试上传gif,但是上传为空

 val gif = Uri.fromFile(File(fileNameGif))

  val filenamePath = "Gif Images/" + firebaseUser!!.uid + System.currentTimeMillis()
                            val storageReference = FirebaseStorage.getInstance().getReference(filenamePath)
                            //upload image
                            storageReference.putFile(gif!!)
                                    .addOnSuccessListener { t


似乎正在发生的事情是我的代码只承认了这一部分:

 fileNameGif = Environment.getExternalStorageDirectory().absolutePath + File.separator + "/gif"

并忽略此:

  if (fileNameGif == null) {
            return;
        }
        try {
            val output = FileOutputStream(fileNameGif)
            val input = FileInputStream(gif_url)
            val inputChannel: FileChannel = input.getChannel()
            val outputChannel: FileChannel = output.getChannel()
            inputChannel.transferTo(0,Toast.LENGTH_SHORT).show()
        } catch (e: FileNotFoundException) {
            e.printStackTrace()
        } catch (e: IOException) {
            e.printStackTrace()
        }  

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...