将inputStream复制到文件并从中读取

时间:2013-01-19 13:40:35

标签: android storage inputstream filestream

我想将的实际位图复制到。之后,我想将文件输出流的内容传递给BitmapFactory.decodeStream。结果是我的图像受损了!我对这些事情的代码如下。

private void copyInStreamToFile(InputStream is) {
    byte buf[] = new byte[1024];
    int len;

    FileOutputStream fos;
    try {
        fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
        while ((len = is.read(buf)) > 0)
            fos.write(buf, 0, len);
        fos.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

private static FileInputStream getStream() throws FileNotFoundException {
    return context.openFileInput(FILENAME);
}

我在下载方法中调用了这个

Bitmap downloadBitmap(String url){         final int IO_BUFFER_SIZE = 4 * 1024;

    // AndroidHttpClient is not allowed to be used from the main thread
    final HttpClient client = AndroidHttpClient.newInstance("Android");
    final HttpGet getRequest = new HttpGet(url);

    try {
        HttpResponse response = client.execute(getRequest);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            Log.w("ImageDownloader", "Error " + statusCode
                    + " while retrieving bitmap from " + url);
            return null;
        }

        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {
                inputStream = entity.getContent();
                copyInStreamToFile(new FlushedInputStream(inputStream));

                DeviceProperties device = new DeviceProperties(activity);
                return decodeBitampFromResource(device.getDeviceHeight(),
                        device.getDeviceWidth());
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                entity.consumeContent();
            }
        }

以及进行解码的最后一个方法是:

public static Bitmap decodeBitampFromResource(int reqWidth, int reqHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    FileInputStream fis;
    InputStream is;
    Bitmap bitmap = null;
    // kanonika tha vriskei to megethos tis photo alla kapoio problima
    // iparxei me ton sixronismo
    try {
        fis = getStream();

        options.inJustDecodeBounds = true;

        BitmapFactory.decodeStream(fis, null, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);


        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        fis.close();
        fis = getStream();

        bitmap = BitmapFactory.decodeStream(fis, null, options);
        fis.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    }
        return bitmap;


}

我是否必须清除文件输出流的内容?据我所知,android开发人员openFileOutput打开文件并覆盖它的内容,如果它不存在则创建它。

0 个答案:

没有答案