Java me J2ME诺基亚s40内存不足异常

Java me J2ME诺基亚s40内存不足异常,java-me,out-of-memory,series-40,Java Me,Out Of Memory,Series 40,我正在尝试将2mb文件读入内存,然后将该文件发送到web服务器。但我的内存不足 FileConnection fileConn = (FileConnection)Connector.open("file:///" + pictureURI.getString(), Connector.READ); InputStream fis = fileConn.openInputStream(); long overallSize = fileConn.fileSi

我正在尝试将2mb文件读入内存,然后将该文件发送到web服务器。但我的内存不足

         FileConnection fileConn = (FileConnection)Connector.open("file:///" + pictureURI.getString(), Connector.READ);
     InputStream fis = fileConn.openInputStream();
     long overallSize = fileConn.fileSize();

     int chunkSize = 2048;
     int length = 0;
     while (length < overallSize)
     {

        byte[] data = new byte[chunkSize];
        int readAmount = fis.read(data, 0, chunkSize);
        byte[] newImageData = new byte[rawImage.length + chunkSize];
        System.arraycopy(rawImage, 0, newImageData, 0, length);
        System.arraycopy(data, 0, newImageData, length, readAmount);
        rawImage = newImageData;
        length += readAmount;

     }
       fis.close();
        fileConn.close(); 
FileConnection fileConn=(FileConnection)Connector.open(“file:///”+pictureURI.getString(),Connector.READ);
InputStream fis=fileConn.openInputStream();
long overallSize=fileConn.fileSize();
int chunkSize=2048;
整数长度=0;
while(长度<总尺寸)
{
字节[]数据=新字节[chunkSize];
int readAmount=fis.read(数据,0,chunkSize);
byte[]newImageData=新字节[rawImage.length+chunkSize];
System.arraycopy(rawImage,0,newImageData,0,长度);
System.arraycopy(数据,0,新图像数据,长度,读取量);
rawImage=新图像数据;
长度+=读取量;
}
fis.close();
fileConn.close();
500kb的文件正在上传。原因可能是什么?请解释一下

我在循环中也尝试了这个方法,但没有用,System.gc()

[编辑] 这使我走上了正轨。现在我在这里罢工

  this.progress = progress;

  HttpConnection conn = null;
  OutputStream os = null;
  InputStream s = null;
  StringBuffer responseString = new StringBuffer();

  try
  {
     System.out.println(System.getProperty("HTTPClient.dontChunkRequests"));
     conn = (HttpConnection)Connector.open(url);
     //conn.setRequestProperty("User-Agent", "Profile/MIDP-2.1 Configuration/CLDC-1.1");
     conn.setRequestMethod(HttpConnection.POST);

     // The messages
     conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=---------------------------4664151417711");
     conn.setRequestProperty("Content-Length", "355");

     os = conn.openOutputStream();

     System.out.println("file name at upload " + fileName);
     String message1 = "";
     message1 += "-----------------------------4664151417711\r\n";
     message1 += "Content-Disposition: form-data; name=\"file\"; filename=\"" + fileName + "\"\r\n";
     message1 += "Content-Type: image/gif\r\n";
     message1 += "\r\n";

     os.write(message1.getBytes());

     System.gc();

     // Send the image
     int index = 0;
     int size = 2048;
     double progdouble;
     do
     {
        progdouble = ((double)index) / ((double)rawImage.length) * 100;
        progress.setValue((int)progdouble);

        if((index+size) > rawImage.length)
        {
           size = rawImage.length - index;
        }
        os.write(rawImage, index, size);
        index += size;

        System.gc();
     } while(index < rawImage.length);

     String message2 = "\r\n-----------------------------4664151417711\r\n";
     message2 += "Content-Disposition: form-data; name=\"number\"\r\n\r\n";
     message2 += this.user_phone_number;         

     os.write(message2.getBytes());

     String message3 = "\r\n-----------------------------4664151417711\r\n";
     message3 += "Content-Disposition: form-data; name=\"uuid\"\r\n\r\n";
     message3 += this.user_uuid;         

     os.write(message3.getBytes());

     String message4 = "\r\n-----------------------------4664151417711--\r\n";  

     os.write(message4.getBytes());


     os.flush();
     os.close();

     // Read

     s = conn.openInputStream();
     int ch, i = 0, maxSize = 16384;
     while(((ch = s.read())!= -1 ) & (i++ < maxSize)) 
     {
        responseString.append((char) ch);
     }

     conn.close();
     System.out.println("response =>"+responseString.toString());



     return responseString.toString();
  }
  catch (IOException ioe)
  {
     return ioe.toString();
  }
this.progress=进度;
HttpConnection conn=null;
OutputStream os=null;
输入流s=null;
StringBuffer responseString=新建StringBuffer();
尝试
{
System.out.println(System.getProperty(“HTTPClient.dontChunkRequests”);
conn=(HttpConnection)连接器。打开(url);
//conn.setRequestProperty(“用户代理”、“配置文件/MIDP-2.1配置/CLDC-1.1”);
conn.setRequestMethod(HttpConnection.POST);
//信息
conn.setRequestProperty(“内容类型”,“多部分/表单数据;边界=------------------------------------4664151417711”);
conn.setRequestProperty(“内容长度”、“355”);
os=连接openOutputStream();
System.out.println(“上传时的文件名”+文件名);
字符串message1=“”;
message1+=“------------------------------------4664151417711\r\n”;
message1+=“内容处置:表单数据;名称=\“文件名=\”+文件名+“\”\r\n”;
message1+=“内容类型:image/gif\r\n”;
message1+=“\r\n”;
write(message1.getBytes());
gc();
//发送图像
int指数=0;
int size=2048;
双进双出;
做
{
progdouble=((双)索引)/((双)rawImage.length)*100;
progress.setValue((int)progdouble);
if((索引+大小)>rawmimage.length)
{
大小=rawImage.length-索引;
}
写操作系统(rawImage、索引、大小);
指数+=大小;
gc();
}而(索引”+responseString.toString());
返回responseString.toString();
}
捕获(ioe异常ioe)
{
返回ioe.toString();
}
现在它在这里失败了

[编辑二]

//一次读取1024字节的算法
字节b[]=新字节[chunkSize];
对于(inti=0;i

提前感谢。

您在此过程中分配了许多新阵列,这完全没有必要。每个
新字节[]
行分配一个新数组

您应该读入一个大数组,并在循环之前分配它一次。您可以很容易地做到这一点,因为您知道文件的确切大小。代码将如下所示:

FileConnection fileConn;
InputStream is;

try {
    fileConn = (FileConnection) Connector.open("file:///" + pictureURI.getString(), Connector.READ);
    is = fileConn.openInputStream();

    long overallSize = fileConn.fileSize();
    if (overallSize > Integer.MAX_VALUE) throw new IllegalArgumentException("File is too large);
    byte[] imageData = new byte[(int) overallSize];
    int chunkSize = 2048;
    int bytesReadTotal = 0;
    while (bytesRead < overallSize) {
        int bytesRead = is.read(imageData, bytesReadTotal, Math.min(imageData.length - bytesReadTotal, chunkSize));
        if (bytesRead == -1) break;
        bytesReadTotal += bytesRead;
    }
} finally {
    if (is != null) is.close();
    if (fileConn != null) fileConn.close();
}
FileConnection fileConn;
输入流为;
试一试{
fileConn=(FileConnection)Connector.open(“文件://“+pictureURI.getString(),Connector.READ”);
is=fileConn.openInputStream();
long overallSize=fileConn.fileSize();
如果(overallSize>Integer.MAX_值)抛出新的IllegalArgumentException(“文件太大”);
字节[]图像数据=新字节[(int)总体大小];
int chunkSize=2048;
int byteReadTotal=0;
while(字节读取<总体大小){
int bytesRead=is.read(imageData,bytesReadTotal,Math.min(imageData.length-bytesReadTotal,chunkSize));
如果(字节读==-1)中断;
字节读取总数+=字节读取;
}
}最后{
如果(is!=null)是.close();
如果(fileConn!=null)fileConn.close();
}

您的手机()整个应用程序的堆空间限制为2Mb。请根据这些限制设计你的应用程序。立即在内存中存储2Mb bytearray-不可能。

谢谢你的回复。我会检查这一点并尽快回复你。你走上了正确的轨道。现在我可以继续阅读文件,但在上载时会遇到相同的错误…请注意好的,在这个代码中。文件读取现在是好的,但是当我尝试上传它时,会出现同样的错误。这是一个好的读取方法吗
FileConnection fileConn;
InputStream is;

try {
    fileConn = (FileConnection) Connector.open("file:///" + pictureURI.getString(), Connector.READ);
    is = fileConn.openInputStream();

    long overallSize = fileConn.fileSize();
    if (overallSize > Integer.MAX_VALUE) throw new IllegalArgumentException("File is too large);
    byte[] imageData = new byte[(int) overallSize];
    int chunkSize = 2048;
    int bytesReadTotal = 0;
    while (bytesRead < overallSize) {
        int bytesRead = is.read(imageData, bytesReadTotal, Math.min(imageData.length - bytesReadTotal, chunkSize));
        if (bytesRead == -1) break;
        bytesReadTotal += bytesRead;
    }
} finally {
    if (is != null) is.close();
    if (fileConn != null) fileConn.close();
}