简体   繁体   English

SD卡写入失败?

[英]SD card writing fails?

Can someone please tell me where I'm going wrong with this piece of code? 有人可以告诉我这段代码在哪里出问题吗? Basically I want the app to download a .png file, display it to the user and also save it to the sd card for future reference. 基本上,我希望该应用程序下载.png文件,将其显示给用户,还将其保存到SD卡中,以备将来参考。 It seems to be all going fine until the while ((len = webstream.read(buffer)) > 0) part; while ((len = webstream.read(buffer)) > 0)部分之前,一切似乎都很好。 as the debugger doesn't reach the breakpoint I've put in here, and I have a directory of empty .png files. 因为调试器未达到我在此处输入的断点,所以我有一个空的.png文件目录。 I have the WRITE_EXTERNAL_STORAGE permission declared in my Manifest. 我在清单中声明了WRITE_EXTERNAL_STORAGE权限。

protected class DownloadTask extends AsyncTask<String, Integer, Bitmap> {

    @Override
    protected Bitmap doInBackground(String... string) {

        Bitmap d = null;

        try {

            DefaultHttpClient dhc = new DefaultHttpClient();
            HttpGet request = new HttpGet(HTTP_BASE + string[0] + ".png");
            HttpResponse response = dhc.execute(request);
            BufferedInputStream webstream = new BufferedInputStream(response.getEntity().getContent());
            d = BitmapFactory.decodeStream(webstream);
            writeToSd(string[0], webstream, d);

        }
        catch (Exception ex) { ex.printStackTrace(); }

        return d;

    }

    private void writeToSd(String string, BufferedInputStream webstream, Bitmap d) {

        try {

            webstream.mark(3);
            webstream.reset();
            File f = new File(Environment.getExternalStorageDirectory() + SD_DIR);
            f.mkdirs();
            File f2 = new File(f, string + ".png");
            f2.createNewFile();
            BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(f2));

            int len;
            byte[] buffer = new byte[1024];
            while ((len = webstream.read(buffer)) > 0) {

                fos.write(buffer, 0, len);

            }

            webstream.close();
            //fos.flush();
            fos.close();

        }
        catch (Exception ex) {

            ex.printStackTrace();

        }


    }

    protected void onPostExecute(Bitmap result) {

        if (result != null) {
            iv.setImageBitmap(result);
            vs.setDisplayedChild(1);
        }

    }

};

I don't know for sure, but it seems likely that there is something wrong with the re-reading of the InputStream, even though it's buffered. 我不确定,但是重新读取InputStream似乎有问题,即使它已缓冲。 An image is quite a bit of data to hold in memory. 图像是要保存在内存中的大量数据。 Rather than doing 而不是做

Fetch Image, Display Image, Store Image 获取图像,显示图像,存储图像

you could try to 你可以尝试

Fetch Image, Store Image, Display Image 获取图像,存储图像,显示图像

This way you can write a full-sized image to disk (without having to buffer the stream and consume a lot of memory). 这样,您可以将完整大小的映像写入磁盘(而不必缓冲流并占用大量内存)。 Then, you can simply load the image from the sd card at the lowest sampling rate that works for your application, thus saving having a full-sized image ever in memory. 然后,您可以简单地以适用于您的应用程序的最低采样率从sd卡加载图像,从而节省了内存中存储的全尺寸图像。 Also, doing it this way would likely eliminate any issues you may or may not be having with the bufferedInputStream - I find them to be pretty finicky. 此外,以这种方式执行此操作可能会消除您在bufferedInputStream中可能遇到或可能没有的任何问题-我发现它们相当挑剔。

Unless you want the original stream bytes, you can write the bitmap out to disk using Bitmap.compress(format, quality, stream) . 除非您想要原始的流字节,否则可以使用Bitmap.compress(format, quality, stream)将位图写出到磁盘。

However, Hamy's answer is probably best from a memory conservation point of view. 但是,从内存保护的角度来看,Hamy的答案可能是最好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM