dalvikvm-heap增长堆(frag case)同时上传视频android

时间:2013-03-28 07:31:08

标签: android

我正面临着从4x设备上传视频的问题。我知道只有15秒的vedio recodring将在samsung S2中创建一个30 MB的文件但是当我试图在php服务器上传视频时它首先显示msg喜欢      D / dalvikvm(7638):GC_FOR_ALLOC释放263K,9%免费12838K / 14087K,暂停13ms,总计13ms      I / dalvikvm-heap(7638):将堆(frag case)增长到14.481MB,用于1048592字节分配      D / dalvikvm(7638):GC_FOR_ALLOC释放< 1K,9%free 13861K / 15175K,暂停22ms,总共22ms      D / dalvikvm(7638):GC_CONCURRENT释放< 1K,9%free 13862K / 15175K,暂停12ms + 2ms,总计27ms

I know it is the memory managment concept and device memory dependent but I want a real solution for it as I am stuck here from quite a few days.
Below is my code to call and upload the video to server.

public void newmethod( String Imageurl ) throws ClientProtocolException, IOException
{
    byte[] data ; 
    int bytenumber,bufferSize,bytesRead;
    int maxBufferSize = 1*1024*1024;
      DataOutputStream dos = null;
     //File sourceFile =  searchForFileInExternalStorage("video.3gp");

     Log.e("in the method the path", ""+Imageurl);

      FileInputStream fileInputStream = new FileInputStream(Imageurl);
      bytenumber = fileInputStream.available();
      Log.e("in the method the the size of the file is", ""+bytenumber);
     // dos = new DataOutputStream(conn.getOutputStream());
      bufferSize = Math.min(bytenumber, maxBufferSize);
      data = new byte[bufferSize];
     //ProgressDialog pr = new ProgressDialog(getApplicationContext());
    // pr.addContentView(getCurrentFocus(), null);
       // read file and write it into form...
       bytesRead = fileInputStream.read(data, 0, bufferSize);

       while (bytesRead > 0) {
       // dos.write(data, 0, bufferSize);
        bytenumber = fileInputStream.available();
         bufferSize = Math.min(bytenumber, maxBufferSize);
        bytesRead = fileInputStream.read(data, 0, bufferSize);
        }

     // pr.show();


    HttpClient httpClient = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost("http://67.52.165.116/Kakz/mobiles/uploadvideo");
    ByteArrayBody bab = new ByteArrayBody( data, Imageurl);
    MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    reqEntity.addPart("user_video", bab);

    FormBodyPart bodyPart=new FormBodyPart("title", new StringBody(title));
    reqEntity.addPart(bodyPart);
    bodyPart=new FormBodyPart("desc", new StringBody(description));
    reqEntity.addPart(bodyPart);
    bodyPart=new FormBodyPart("source", new StringBody("android"));
    reqEntity.addPart(bodyPart); 
    postRequest.setEntity(reqEntity);
    final HttpResponse response = httpClient.execute(postRequest);
    // FileInputStream fileInputStream = new FileInputStream(response.getEntity().getContent());


    runOnUiThread(new Runnable() {
         public void run() {

    //stuff that updates ui

             BufferedReader in = null;
            try {
                in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

            } catch (IllegalStateException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
                String line = null;

    try {
        while((line = in.readLine()) != null) {
            System.out.println(line);

            titleText.setText("");
            descriptionText.setText("");
          //  Toast.makeText(getApplicationContext(), "Video Uploaded successfully", Toast.LENGTH_LONG).show();

            if (line.equalsIgnoreCase("Its too Heavy")) {
            //  pr.dismiss();
                // titleText.setText("");
                //descriptionText.setText("");
                Toast.makeText(getApplicationContext(), "Video size is too heavy", Toast.LENGTH_LONG).show();

            }

            else if (line.equalsIgnoreCase("Error")) {
            //  pr.dismiss();
                // titleText.setText("");
                //descriptionText.setText("");
                Toast.makeText(getApplicationContext(), "Error uploading video", Toast.LENGTH_LONG).show();


            }

            else  if (line.equalsIgnoreCase("Uploaded")) {
                //pr.dismiss();
                 //titleText.setText("");
                //descriptionText.setText("");
                Toast.makeText(getApplicationContext(), "Video uploaded successfully", Toast.LENGTH_LONG).show();

            }

        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
         }
});

Please let me know that how I should manage the code to get it right.
Any help would be appreciated ASAP`enter code here`

2 个答案:

答案 0 :(得分:1)

  

我正面临着从4x设备上传视频的问题。

这不是问题。当您进行非常高成本的操作时,这是正常的行为。你的堆的大小(隐式确定)在一个时刻是不够的,所以它需要长大。你应该如何写入清单

是同等的行为
android:largeHeap="true"

此属性不起作用。当堆需要更大的尺寸时,它会自己成长 - 当它需要时。 当您没有太多内存时,通常会显示此消息。

  

GC_FOR_ALLOC释放263K,9%免费12838K / 14087K,暂停13ms,总计   13ms I / dalvikvm-heap(7638)

这里你只有9%的内存和堆需要长大。通常它会在ALLOC小于10%时发生,如果您使用Thread,则无关紧要,一旦它是高成本操作。当您的堆大小达到一个“关键点”时,出于安全原因以及VM,应用程序将自动停止。

一种可能的解决方案是,您需要将文件拆分为较小的块并逐个上传。

答案 1 :(得分:0)

我不知道回答我自己的问题是一个好主意。但是我仍然想为那些像我一样面临同样问题的人提供经验,因为在搜索了很多东西后我才知道这个问题没有特定的解决方案,因为它依赖于设备和大量的内存消耗过程。 然后我寻找其他选项,比如在上传前压缩视频,但我也没有获得任何开源库。所以现在视频上传并不像我想象的那么简单。 :)