将 BufferedInputStream 转换为文件

发布于 2024-12-04 08:37:15 字数 2074 浏览 0 评论 0原文

我正在将图像从网络加载到本地 Android 手机。我用于写入文件的代码如下

        BufferedInputStream bisMBImage=null;
        InputStream isImage = null;
        URL urlImage = null;
        URLConnection urlImageCon = null;
        try 
        {
                urlImage = new URL(imageURL); //you can write here any link
                urlImageCon = urlImage.openConnection();
                isImage = urlImageCon.getInputStream();
                bisMBImage = new BufferedInputStream(isImage);

                int dotPos = imageURL.lastIndexOf(".");
                if (dotPos > 0 )
                {
                    imageExt = imageURL.substring(dotPos,imageURL.length());    
                }

                imageFileName = PATH + "t1" + imageExt;
                File file = new File(imageFileName);
                if (file.exists())
                {
                    file.delete();
                    Log.d("FD",imageFileName + " deleted");
                }
                 ByteArrayBuffer baf = new ByteArrayBuffer(255);
                 Log.d("IMAGEWRITE", "Start to write image to Disk");
                 int current = 0;
                 try 
                 {
                    while ((current = bisMBImage.read()) != -1) 
                    {
                             baf.append((byte) current);
                    }

                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write(baf.toByteArray());
                    fos.close();    
                    Log.d("IMAGEWRITE", "Image write to Disk done");
                } 
                catch (IOException e) 
                {
                    e.printStackTrace();
                }                       

                isImage.close();
        } 
        catch (IOException e) 
        {
                Log.d("DownloadImage", "Error: " + e);
        }
        finally
        {
            isImage = null;         
            urlImageCon = null;
            urlImage = null;
        }

由于某种原因,整个写入文件需要 1 分钟。有什么办法可以优化这个吗?

I am loading a image from the web to the local android phone. The code that I have for writing to a file is as follows

        BufferedInputStream bisMBImage=null;
        InputStream isImage = null;
        URL urlImage = null;
        URLConnection urlImageCon = null;
        try 
        {
                urlImage = new URL(imageURL); //you can write here any link
                urlImageCon = urlImage.openConnection();
                isImage = urlImageCon.getInputStream();
                bisMBImage = new BufferedInputStream(isImage);

                int dotPos = imageURL.lastIndexOf(".");
                if (dotPos > 0 )
                {
                    imageExt = imageURL.substring(dotPos,imageURL.length());    
                }

                imageFileName = PATH + "t1" + imageExt;
                File file = new File(imageFileName);
                if (file.exists())
                {
                    file.delete();
                    Log.d("FD",imageFileName + " deleted");
                }
                 ByteArrayBuffer baf = new ByteArrayBuffer(255);
                 Log.d("IMAGEWRITE", "Start to write image to Disk");
                 int current = 0;
                 try 
                 {
                    while ((current = bisMBImage.read()) != -1) 
                    {
                             baf.append((byte) current);
                    }

                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write(baf.toByteArray());
                    fos.close();    
                    Log.d("IMAGEWRITE", "Image write to Disk done");
                } 
                catch (IOException e) 
                {
                    e.printStackTrace();
                }                       

                isImage.close();
        } 
        catch (IOException e) 
        {
                Log.d("DownloadImage", "Error: " + e);
        }
        finally
        {
            isImage = null;         
            urlImageCon = null;
            urlImage = null;
        }

For some reason the whole writing to a file takes 1 minute. Is there a way I can optimize this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妖妓 2024-12-11 08:37:15

您的缓冲区非常小:255 字节。您可以将其放大 1024 倍(255 KB)。这是一个可以接受的大小,这肯定会加快速度。

另外,这非常慢,因为它一一读取字节:

while ((current = bisMBImage.read()) != -1)  {
    baf.append((byte) current);
}

您应该尝试使用 read()的数组版本改为:read(byte[] buffer, int offset, int byteCount)一个同样大的数组正如我上面所描述的。

Your buffer is very small: 255 bytes. You could make it 1024 times bigger (255 kilobytes). This is an acceptable size and this would certainly speed up the thing.

Also, this is very slow as it reads the bytes one by one:

while ((current = bisMBImage.read()) != -1)  {
    baf.append((byte) current);
}

You should try using the array version of read() instead: read(byte[] buffer, int offset, int byteCount) with an array as large as what I have described above.

忱杏 2024-12-11 08:37:15

您应该使用 Android HttpClient 通过 java URL 连接获取文件。而且你的缓冲区非常小。
试试这个截图:

FileOutputStream f = new FileOutputStream(new File(root,"yourfile.dat"));

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(urlString);
HttpResponse response = httpClient.execute(request);
InputStream is = response.getEntity().getContent();

byte[] buffer = new byte[1024];
int len1 = 0;
while ( (len1 = is.read(buffer)) > 0 ) {
        f.write(buffer,0, len1);
}
f.close();

You should use the Android HttpClient for file fetching over the java URL Connection. Also your Buffer is very small.
Try this snipped:

FileOutputStream f = new FileOutputStream(new File(root,"yourfile.dat"));

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(urlString);
HttpResponse response = httpClient.execute(request);
InputStream is = response.getEntity().getContent();

byte[] buffer = new byte[1024];
int len1 = 0;
while ( (len1 = is.read(buffer)) > 0 ) {
        f.write(buffer,0, len1);
}
f.close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文