Android:java.lang.OutOfMemoryError无法分配360318346字节的分配,其中包含5802592个可用字节和87MB,直到OOM
我正在处理从服务器到客户端的传输文件示例。当我尝试传输大文件(示例中我有500mb视频文件)时,它显示错误Android:java.lang.OutOfMemoryError未能分配360318346字节分配,5802592个空闲字节和87MB,直到OOM。请帮我解决这个错误 服务器端的一些和平的代码Android:java.lang.OutOfMemoryError无法分配360318346字节的分配,其中包含5802592个可用字节和87MB,直到OOM,java,android,sockets,Java,Android,Sockets,我正在处理从服务器到客户端的传输文件示例。当我尝试传输大文件(示例中我有500mb视频文件)时,它显示错误Android:java.lang.OutOfMemoryError未能分配360318346字节分配,5802592个空闲字节和87MB,直到OOM。请帮我解决这个错误 服务器端的一些和平的代码 public class FileTxThread extends Thread { Socket socket; FileTxThread(Socket socket){
public class FileTxThread extends Thread {
Socket socket;
FileTxThread(Socket socket){
this.socket= socket;
}
@Override
public void run() {
File file = new File(Environment.getExternalStorageDirectory(),"RAHASYA.mp4");
byte[] bytes = new byte[(int) file.length()];
BufferedInputStream bis;
try {
bis = new BufferedInputStream(new FileInputStream(file));
bis.read(bytes, 0, bytes.length);
OutputStream os = socket.getOutputStream();
os.write(bytes, 0, bytes.length);
os.flush();
// socket.close();
final String sentMsg = "File sent to: " + socket.getInetAddress();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,sentMsg,Toast.LENGTH_LONG).show();
}});
private class ClientRxThread extends Thread {
String dstAddress;
int dstPort;
ClientRxThread(String address, int port) {
dstAddress = address;
dstPort = port;
}
@Override
public void run() {
Socket socket = null;
try {
socket = new Socket(dstAddress, dstPort);
out.println("Sockt "+socket.getInetAddress());
File file = new File(Environment.getExternalStorageDirectory(),"RAHASYA.mp4");
byte[] bytes = new byte[1024];
InputStream is = socket.getInputStream();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
while (true) {
int bytesRead = is.read(bytes);
if (bytesRead < 0) break;
bos.write(bytes, 0, bytesRead);
// Now it loops around to read some more.
}
bos.close();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,"Finished",Toast.LENGTH_LONG).show();
}});
客户端需要一些代码
public class FileTxThread extends Thread {
Socket socket;
FileTxThread(Socket socket){
this.socket= socket;
}
@Override
public void run() {
File file = new File(Environment.getExternalStorageDirectory(),"RAHASYA.mp4");
byte[] bytes = new byte[(int) file.length()];
BufferedInputStream bis;
try {
bis = new BufferedInputStream(new FileInputStream(file));
bis.read(bytes, 0, bytes.length);
OutputStream os = socket.getOutputStream();
os.write(bytes, 0, bytes.length);
os.flush();
// socket.close();
final String sentMsg = "File sent to: " + socket.getInetAddress();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,sentMsg,Toast.LENGTH_LONG).show();
}});
private class ClientRxThread extends Thread {
String dstAddress;
int dstPort;
ClientRxThread(String address, int port) {
dstAddress = address;
dstPort = port;
}
@Override
public void run() {
Socket socket = null;
try {
socket = new Socket(dstAddress, dstPort);
out.println("Sockt "+socket.getInetAddress());
File file = new File(Environment.getExternalStorageDirectory(),"RAHASYA.mp4");
byte[] bytes = new byte[1024];
InputStream is = socket.getInputStream();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
while (true) {
int bytesRead = is.read(bytes);
if (bytesRead < 0) break;
bos.write(bytes, 0, bytesRead);
// Now it loops around to read some more.
}
bos.close();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,"Finished",Toast.LENGTH_LONG).show();
}});
私有类ClientRxThread扩展线程{
字符串地址;
国际数据传输端口;
ClientRxThread(字符串地址,int端口){
地址=地址;
dstPort=端口;
}
@凌驾
公开募捐{
套接字=空;
试一试{
套接字=新的套接字(dstAddress,dstPort);
out.println(“Sockt”+socket.getInetAddress());
File File=新文件(Environment.getExternalStorageDirectory(),“RAHASYA.mp4”);
字节[]字节=新字节[1024];
InputStream=socket.getInputStream();
BufferedOutputStream bos=新的BufferedOutputStream(新文件输出流(文件));
while(true){
int bytesRead=is.read(字节);
如果(字节数<0)中断;
写入(字节,0,字节读取);
//现在它循环阅读更多内容。
}
bos.close();
MainActivity.this.runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(MainActivity.this,“Finished”,Toast.LENGTH_LONG.show();
}});
服务器端的字节数组太大,请将其改小,然后重试?供参考:
File file = new File(Environment.getExternalStorageDirectory(),"RAHASYA.mp4");
byte[] bytes = new byte[2048];
BufferedInputStream bis;
try {
bis = new BufferedInputStream(new FileInputStream(file));
OutputStream os = socket.getOutputStream();
int read = bis.read(bytes, 0, bytes.length);
while (read!=-1){
os.write(bytes, 0, read);
os.flush();
read = bis.read(bytes, 0, bytes.length);
}
//...and so on
你的问题是线路
byte[]bytes=新字节[(int)file.length()];
您正在创建一个与整个文件大小相同的数组。您需要一次读取一点。是的,先生,但请给我一些如何读取文件的提示,我确实尝试过,但我不知道它是对是错,因为它不起作用,请帮助。谢谢先生。在客户端执行您正在执行的操作,但在服务器端执行(将读取更改为写入obv)。如何更改。我是初学者,我不知道服务器端代码的更改位置。请帮助我。谢谢!更改此代码:byte[]bytes=new byte[(int)file.length();请告诉我这一行需要添加哪些代码byte[]bytes=new byte[(int)file.length()。谢谢!