2012-02-02 34 views
0

我试图从android内部存储器显示图像,我只有4images(图像是之前拍摄的屏幕截图),我在GridView中显示这些图像,它的工作原理,但它需要太多的时间,这是我的代码:从Android内部存储器逐字节读取图像需要太多时间

FileInputStream fis = null; 
DataOutputStream outWriter = null; 
ByteArrayOutputStream bufStream = null; 
String imageFile; 
int occurence; 
for (int i=0; i<4; i++) { 
    try { 
     occurence = i+1; 
     imageFile = "preview"+occurence+".png"; 
     fis = openFileInput(imageFile); 
     bufStream = new ByteArrayOutputStream(); 
     outWriter = new DataOutputStream(bufStream); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    int ch; 
    byte[] data = null; 
    try { 
     **while((ch = fis.read()) != -1) 
      outWriter.write(ch);** 
     outWriter.close(); 
     data = bufStream.toByteArray(); 
     bufStream.close(); 
     fis.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    favorPreviews.add(BitmapFactory.decodeByteArray(data, 0, data.length)) ; 
} 

正如你所看到的,我使用的FileInputStream读他们,而是读取文件中的循环逐字节:

while((ch = fis.read()) != -1) 
    outWriter.write(ch); 

它需要太多的时间,是否有人知道更快的方式来阅读这些图像?

回答

1

为了缩短图像尺寸,表示速度更快。它可以通过Bitmap decodeFile (String pathName, BitmapFactory.Options opts)

看看这个例子来完成:

public Bitmap getReducedBitmap(String path) { 
    BitmapFactory.Options opt = new BitmapFactory.Options(); 
    opt.inSampleSize=4; // reduced the image to 1/4 of the orignal size 
    return BitmapFactory.decodeFile(path, opt); 
} 
+0

非常感谢,我会尝试, – 2012-02-02 21:33:55

相关问题