2011-05-31 105 views
0

我需要建立从SD卡图像的位图,以便通过打开的inputStream为越来越像我得到字节数据的URI后下面的代码如何从android中的SD卡图像创建位图?

如下

public byte[] getBytesFromFile(InputStream is) 
{ 
    byte[] data = null; 
    ByteArrayOutputStream buffer=null; 
    try 
    { 
     buffer = new ByteArrayOutputStream(); 

     int nRead; 
     data = new byte[16384]; 
     while ((nRead = is.read(data, 0, data.length)) != -1) 
     { 
      buffer.write(data, 0, nRead); 
     } 

     buffer.flush(); 

     return buffer.toByteArray(); 
    } catch (IOException e) 
    { 
     return null; 
    } 
    finally 
    { 
     if(data!=null) 
      data = null; 
     if(is!=null) 
      try { 
       is.close(); 
      } catch (Exception e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      if(buffer!=null) 
      { 
       try { 
        buffer.close(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       buffer = null; 
      } 
      System.gc(); 
    } 
在此之后我只是创建数据的位图

byte[] data = getBytesFromFile(is); 
Bitmap bm= BitmapFactory.decodeByteArray(data, 0, data.length); 

但它给我outofmemory错误。许多人指导我检查内存泄漏,但是,这是我的应用程序的第一步,我的意思是通过意图过滤器,我从图库菜单选项“共享”开始我的应用程序,它调用我的应用程序与图像uri .. PLZ指南我的家伙,如果我错了...也有这种例外是有高分辨率图像(大小excoreds 1MB),但任何它应该创建位图的设备...

回答

1

尝试使用BitmapFactory.decodeStream()方法,而不是首先将bytearray加载到内存中。另请参阅question以获取有关加载位图的更多信息。

+0

BitmapFactory.decodeStream()确实帮助了我,但是如果在上面给出的链接(问题)中找到解决方案,则会严重降低图像质量....使用BitmapFactory.Options.inSampleSize会降低图像质量 – zulfiqar 2011-06-01 06:29:06