android应用程序编写问题
下面是从网络下载图像后写入文件的代码。但不知何故,它在写入文件流时卡住了。一点也不例外。它只是停留在 while 循环中,然后显示强制关闭弹出窗口。这种情况十分之二或三。
private void saveImage(String fullPath) {
File imgFile = new File(fullPath);
try {
if(imgFile.exists()) imgFile.delete();
URL url = new URL(this.fileURL);
URLConnection connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)connection;
InputStream in = httpConnection.getInputStream();
FileOutputStream fos = new FileOutputStream(imgFile);
int n = 0;
while((n = in.read()) != -1) {
fos.write(n);
}
fos.flush();
fos.close();
in.close();
}
catch(IOException e) {
imgFile.delete();
Log.d("IOException Thrown", e.toString());
}
}
当我检查写入的文件时,它总是在写入到4576字节时卡住。 (原始图像大小超过150k) 有人可以帮助我解决这个问题吗?
Below is the code to write to a file after downloading an image from the web. But somehow, it's just stuck while writing to File Stream. No Exception at all. It just stays on while loop then displays force close popup. it happens 2 or 3 out of 10.
private void saveImage(String fullPath) {
File imgFile = new File(fullPath);
try {
if(imgFile.exists()) imgFile.delete();
URL url = new URL(this.fileURL);
URLConnection connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)connection;
InputStream in = httpConnection.getInputStream();
FileOutputStream fos = new FileOutputStream(imgFile);
int n = 0;
while((n = in.read()) != -1) {
fos.write(n);
}
fos.flush();
fos.close();
in.close();
}
catch(IOException e) {
imgFile.delete();
Log.d("IOException Thrown", e.toString());
}
}
When I check the file written, it's always stuck when it writes till 4576byte. (Original image size is over 150k)
Is there anybody who can help me on this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你好 Juniano 回答你的第二个问题,是的,你可以使用 while 循环,但你还需要两件事。
1) 创建一个 ByteArrayBuffer 来存储您的 InputStream
2) 使用存储的字节创建图像。
约热西斯
Hi Juniano answering your second question, yes, you can use the while loop but you need 2 things more.
1) create a ByteArrayBuffer to store your InputStream
2) create the image with the stored Bytes.
Jorgesys