开发者社区> 问答> 正文

安卓手机程序 ,提示内存溢出?报错

安卓手机端程序第一次上传多图时候,没有错误,然后返回修改重新上传,出现错误报错信息,内存溢出

<p style="font-size:13.3333px;">
	<br>
</p>
<p style="font-size:13.3333px;">
	<br>
</p>
<p style="font-size:13.3333px;">
	<span style="font-family:微软雅黑, 'MS Sans Serif', sans-serif;font-size:16px;line-height:normal;background-color:#FFEDC4;">下面是上传图片</span>
</p>
<p style="font-size:13.3333px;">
	<span style="font-family:微软雅黑, 'MS Sans Serif', sans-serif;font-size:16px;line-height:normal;background-color:#FFEDC4;">try {</span>
</p>
<p style="font-size:13.3333px;">
	<span style="font-family:微软雅黑, 'MS Sans Serif', sans-serif;font-size:16px;line-height:normal;background-color:#FFEDC4;">URL url = new URL(uploadUrl);</span><br>

HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(true);// 允许使用缓存
httpURLConnection.setRequestMethod("POST");
// 设置Http请求头
httpURLConnection
.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Cookie", "JSESSIONID="
+ jsession);// 要加个试试
// 必须在Content-Type 请求头中指定分界符中的任意字符串
httpURLConnection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);

// 定义数据写入流,准备上传文件,,这个tmd就是点了以后就上传,,呵呵,,要保存起来,!!!
DataOutputStream dos = new DataOutputStream(httpURLConnection.getOutputStream());
dos.writeBytes(twoHyphens + boundary + end);
// 设置与上传文件相关的信息
dos.writeBytes("Content-Disposition: form-data; name="file"; filename=""
+ filename.substring(filename.lastIndexOf("/") + 1)
+ """ + end);
dos.writeBytes(end);

FileInputStream fis = new FileInputStream(filename);


byte[] buffer = new byte[8192]; // 8k
int count = 0;
// 读取文件夹内容,并写入OutputStream对象
while ((count = fis.read(buffer)) != -1) {
dos.write(buffer, 0, count);
}
fis.close();
dos.writeBytes(end);
dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
dos.flush();
// 开始读取从服务器传过来的信息
InputStream is = httpURLConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
String result = br.readLine() + ";";
pathname += result;


dos.close();
is.close();
} catch (Exception e) {
}


下面是从服务器下载图片

try {
URL url = new URL(uploadUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(true);// 允许使用缓存
httpURLConnection.setRequestMethod("POST");
// 设置Http请求头
httpURLConnection
.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Cookie", "JSESSIONID="
+ jsession);// 要加个试试
// 必须在Content-Type 请求头中指定分界符中的任意字符串
httpURLConnection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);


// 定义数据写入流,准备上传文件,,这个tmd就是点了以后就上传,,呵呵,,要保存起来,!!!
DataOutputStream dos = new DataOutputStream(httpURLConnection.getOutputStream());
dos.writeBytes(twoHyphens + boundary + end);
// 设置与上传文件相关的信息
dos.writeBytes("Content-Disposition: form-data; name="file"; filename=""
+ filename.substring(filename.lastIndexOf("/") + 1)
+ """ + end);
dos.writeBytes(end);


FileInputStream fis = new FileInputStream(filename);




byte[] buffer = new byte[8192]; // 8k
int count = 0;
// 读取文件夹内容,并写入OutputStream对象
while ((count = fis.read(buffer)) != -1) {
dos.write(buffer, 0, count);
}
fis.close();
dos.writeBytes(end);
dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
dos.flush();
// 开始读取从服务器传过来的信息
InputStream is = httpURLConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
String result = br.readLine() + ";";
pathname += result;




dos.close();
is.close();
} catch (Exception e) {
}


展开
收起
爱吃鱼的程序员 2020-06-09 13:52:35 526 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    请问有高人可以指导吗?读文件写输出流时,循环体里也要加flush,否则会因为输出流的内存缓冲区溢出而出错。第一次上传没有出现错误,然后返回来修改,在上传就出现错误了

    这个问题应该和往服务端读流写流没什么关系,看你的日志报错信息,你的图片貌似解码需要的内存大于4m,你查查你是否每次上传时,gridview的getview方法都在解码图片,这样的做法消耗内存资源很大。

    第一次上传,没有出现错误,然后返回来修改,在上传就出现错误了

    2020-06-09 13:52:51
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云服务器ECS内存增强型实例re6全新发布 立即下载
手机卫士性能优化方案 立即下载
Tangram 立即下载

相关实验场景

更多