【问题标题】:Read images byte by byte from android internal storage takes too much time从android内部存储逐字节读取图像需要太多时间
【发布时间】:2012-02-02 20:56:09
【问题描述】:

我正在尝试显示来自 android 内部存储的图像,我只有 4 张图像(这些图像是之前拍摄的屏幕截图),我在 GridView 中显示这些图像,它可以工作,但是需要太多时间,这是我的代码:

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);

而且这需要太多时间,有谁知道读取这些图像的更快方法吗?

【问题讨论】:

    标签: android file


    【解决方案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);
    }
    

    【讨论】:

      【解决方案2】:

      请改用BitmapFactory.decodeFile(String)。然后为您处理所有这些文件读取。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-01
        相关资源
        最近更新 更多