当我使用libwebp和ndk在android 2.x上显示webp图像时出现内存错误

时间:2014-02-19 09:13:29

标签: android out-of-memory webp

我使用谷歌开源项目libwebp在android 1.6 - android 4.0上显示webp图像,并在android 4.0或更高版本上直接解码webp,我发现在某些Android手机如appo中,我会得到一个outofmemory运行libwebp lib时出错,出错的代码是:int[] pixels = new int[decoded.length / 4]; 任何人都可以建议我如何避免这种情况?这是我的代码:

public static Bitmap getBitmapFromURL(String src) {        
          HttpURLConnection connection = null;        
          Bitmap bmp = null;        
          try {            
               connection = (HttpURLConnection) 
               new URL(src).openConnection();               
               connection.setRequestMethod("GET");           
               connection.setUseCaches(false);            
               connection.setDoInput(true);            
               connection.setDoOutput(true);           
               connection.setRequestProperty("Content-Type", 
                   "image/webp");           
               connection.connect();
          //Send request             
               DataOutputStream wr = new DataOutputStream (                               connection.getOutputStream ());              
               wr.writeBytes ("");             
               wr.flush ();             
               wr.close ();
              //Get Response                  
               BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buf = new byte[4096];           
             while(true) {               
                  String n = rd.readLine();                
                  if(n == null)break;                
                      baos.write(n.getBytes(), 0, n.getByte().length);                     
                  baos.write('\n');            
           }
            byte data[] = baos.toByteArray();                   
 // From the lib's exemple             
            int[] width = new int[] { 0 };           
            int[] height = new int[] { 0 };
            int test = libwebp.WebPGetInfo(data, data.length, width, height); 
// test = 0 ! ! !
            byte[] decoded = libwebp.WebPDecodeARGB(data, data.length, width, height); 

#######################out of memory error is always here         
            int[] pixels = new int[decoded.length / 4];  
####################### 
            ByteBuffer.wrap(decoded).asIntBuffer().get(pixels);
            bmp = Bitmap.createBitmap(pixels, width[0], height[0],Bitmap.Config.ARGB_8888);        
             } catch (IOException e) {           
            e.printStackTrace();        }
        return bmp;
    }

2 个答案:

答案 0 :(得分:1)

在创建位图之前尝试使用System.gc()。这将迫使Android中的垃圾收集。这可能会释放足够的内存。如果您不断出现内存错误,请尝试在代码中查找内存泄漏并进行修复。

仍然无法使用?也许在本机代码中使用C / C ++进行尝试。在这里,您可以有效地管理您的记忆(如果您熟悉C / C ++)。

尝试这样的事情:

public static ByteArrayOutputStream getBytesFromURL(String src) {        
      HttpURLConnection connection = null;        
      ByteArrayOutputStream baos = new ByteArrayOutputStream();       
      try {            
           connection = (HttpURLConnection) 
           new URL(src).openConnection();               
           connection.setRequestMethod("GET");           
           connection.setUseCaches(false);            
           connection.setDoInput(true);            
           connection.setDoOutput(true);           
           connection.setRequestProperty("Content-Type", 
               "image/webp");           
           connection.connect();
      //Send request             
           DataOutputStream wr = new DataOutputStream (                               connection.getOutputStream ());              
           wr.writeBytes ("");             
           wr.flush ();             
           wr.close ();
          //Get Response                  
           BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));    
           while(true) {               
              String n = rd.readLine();                
              if(n == null)break;                
                  baos.write(n.getBytes(), 0, n.getByte().length);                     
              baos.write('\n');            
           }
           connection.close();
        }catch(Exception q){} //Your exception catching here
           return baos;
}

public static Bitmap createBitmap(ByteArrayOutputStream baos){
            System.gc();
            byte data[] = baos.toByteArray();                   


// From the lib's exemple             
            int[] width = new int[] { 0 };           
            int[] height = new int[] { 0 };
            int test = libwebp.WebPGetInfo(data, data.length, width, height); 
// test = 0 ! ! !
            byte[] decoded = libwebp.WebPDecodeARGB(data, data.length, width, height);        
            int[] pixels = new int[decoded.length / 4];  
            ByteBuffer.wrap(decoded).asIntBuffer().get(pixels);
            bmp = Bitmap.createBitmap(pixels, width[0], height[0],Bitmap.Config.ARGB_8888);        
        return bmp;
    }

答案 1 :(得分:0)

在应用程序标记内的manifest.xml文件中添加属性android:largeHeap =“true”。如果你在android中使用大量的位图,那么使用缓存概念。如果你不使用它,也不要忘记回收位图,至少。基本上System.gc()方法不适用于位图。