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

标签 android out-of-memory webp

‖我在android 1.6--android 4.0上使用google开源项目libwebp显示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;
    }

最佳答案

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

还是不行?也许在 native 代码的 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;
    }

关于android - 当我使用 libwebp 和 ndk 在 android 2.x 上显示 webp 图像时出现内存不足错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21875784/

相关文章:

java - 如何在android/java中将动画webp转换为gif?

javascript - 为 Web 压缩和调整图像大小

android - 在 Visual Studio 2015 c++ 中构建 android native 应用程序

android - 使用具有大量透明区域的 png 时内存消耗大幅上升

android - Rails 3 和 Android 移动应用程序中的自定义 HTTP header 通过 HTTPS 消失

javascript - 实习生 - 调试内存不足异常

关于 FileOutputStream 的 java OutOfMemoryError?

android - 如何知道抽屉导航是否在 Xamarin 中打开?

java - JOOQ 从大表 : good practise 取数据

ios - 解码动画Webp iOS时出错