Android异常-写入文件时ArrayIndexOutOfBoundsException kotlin

Android异常-写入文件时ArrayIndexOutOfBoundsException kotlin,android,kotlin,Android,Kotlin,当我的应用程序崩溃时,我收到这样的错误: java.lang.ArrayIndexOutOfBoundsException: length=4096; regionStart=0; regionLength=-1 当我试图从服务器下载照片时,我的应用程序崩溃。它在这条线上指出: do { val read = inputStream.read(buffer) outputStream.write(buffer, 0, read)//this line!!!! }while (r

当我的应用程序崩溃时,我收到这样的错误:

java.lang.ArrayIndexOutOfBoundsException: length=4096; regionStart=0; regionLength=-1
当我试图从服务器下载照片时,我的应用程序崩溃。它在这条线上指出:

do {
    val read = inputStream.read(buffer)
    outputStream.write(buffer, 0, read)//this line!!!!
}while (read!=-1)
可能是因为我从java错误地转换了while循环。在java中,我有这样的smth:

while ((read = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, read)
}
我找到了解决问题的方法,创建了自己的新
循环,但正如我所见,我做错了。为什么会发生这种情况?我如何解决这个问题

更新 下面是解决我问题的另一种方法:

while (inputStream.read(buffer) != -1) {
   outputStream.write(buffer, 0, inputStream.read(buffer))
 }

它不将照片加载到内存

问题在于循环本身

-没有更多数据时返回1

Do while{}
至少执行一次,因此如果没有数据,您将出现此错误。
而{}
首先检查条件,然后执行。在Java中使用while,在Kotlin中使用do while。

问题在于循环本身

-没有更多数据时返回1

Do while{}
至少执行一次,因此如果没有数据,您将出现此错误。
而{}
首先检查条件,然后执行。在Java中,您使用了while,而在Kotlin中,您使用了do while。

不确定您到底在打印什么。下面是我在应用程序中向文件写入文本的一种方法

  try {
          val sub = File(Environment.getExternalStorageDirectory(), "/My Documents")
               if (!sub.exists())
                   sub.mkdirs()

               val txtFile = File(sub, "mTextFile.txt")
               val writer = FileWriter(txtFile)
               writer.append("My text")  //Append whatever text you want to write to a file.
               writer.flush()
               writer.close()

           } catch (e: FileSystemException) {
               e.printStackTrace()  //Handle exception properly
           }
     }

不知道你到底在打印什么。下面是我在应用程序中向文件写入文本的一种方法

  try {
          val sub = File(Environment.getExternalStorageDirectory(), "/My Documents")
               if (!sub.exists())
                   sub.mkdirs()

               val txtFile = File(sub, "mTextFile.txt")
               val writer = FileWriter(txtFile)
               writer.append("My text")  //Append whatever text you want to write to a file.
               writer.flush()
               writer.close()

           } catch (e: FileSystemException) {
               e.printStackTrace()  //Handle exception properly
           }
     }

这种转变确实不同。
while(cond){..}
窗体在使用
read
变量之前检查终止条件,在使用
read
变量之后检查
do{..}while(cond)
。因此,当
read=-1
(以及“超出数组边界”)时,错误是使用
read
;一般来说,
while(true){val read=…;if(read<0)break;write(…);}
应该“起作用”并说明错误。转换确实不同。
while(cond){..}
窗体在使用
read
变量之前检查终止条件,在使用
read
变量之后检查
do{..}while(cond)
。因此,当
read=-1
(以及“超出数组边界”)时,错误是使用
read
;简单地说,
while(true){val read=…;if(read<0)break;write(…);}
应该“起作用”并说明错误。因此,我如何将我的java循环转换为kotlin,因为我知道我的循环是错误的?
如果(read!=-1){write}/code>不会循环,它将只检查一次。他可能不得不使用while循环,而不是do while。@P.Juni,我已经编辑了我的问题,我的照片不再下载:(从你的帖子的评论中尝试循环->
while(true){val read=…;if(read<0)break;write(…);}
你能将这个变量从评论添加到你的答案中吗,因为图像还没有加载?:(那么我如何才能将我的java循环转换为kotlin,因为我知道我的循环是错误的?
如果(read!=-1){write}
不会循环,它只会检查一次。他可能必须使用while循环而不是do while。@P.Juni,我已经编辑了我的问题,我的照片不再下载了:(从你的帖子的评论中尝试while循环)(true){val read=..;if(read<0)break;write(..);}
由于图像尚未加载,您能否将注释中的此变量添加到您的答案中(