如何在数据库存储文件中保存图像?

时间:2013-04-09 17:41:18

标签: android webview

我尝试保存网页以供离线阅读。我使用webView.capturePicture()并将其保存为存储中的.jpg文件,但有一个大问题...... 它的文件总是太大,例如:“CNN.com”的重量是7mb,当我尝试保存它时,我的应用堆大小是30mb。

对于Android原始浏览器的功能是“保存页面进行离线阅读”,当我尝试时,我感到很惊讶。它只需捕获webview并保存在存储放置文件snapshots.db中。 此文件的权重仅为2.7mb,但保存的网页为5mb。

我只是有一个问题,怎么做?

  String path = Environment.getExternalStorageDirectory().getAbsolutePath() +
                          "/Android/data/"+context.getString(R.string.app_name)+"/saved pages/";

            // Current state of the external media
            String extState = Environment.getExternalStorageState();

            // External media can be written onto
            if (extState.equals(Environment.MEDIA_MOUNTED))
            {
                // Make sure the path exists
                boolean exists = (new File(path)).exists();  
                if (!exists){ new File(path).mkdirs(); }  

                SimpleDateFormat dateFormats = new SimpleDateFormat("yyyy.MM.dd HH.mm"); 
                Date dates = new Date();
                String fileName = wv.getTitle();
                String filename = fileName.replaceAll("(\\p{Punct})","");

                file = new File(path, filename + ".jpg");

                try{
                Picture pics = wv.capturePicture();
                Bitmap  b = Bitmap.createBitmap( pics.getWidth(),
                        pics.getHeight(), Bitmap.Config.RGB_565);
                        Canvas c = new Canvas( b );

                        pics.draw( c );

                        FileOutputStream bitmapStream;
                        try {
                            bitmapStream = new FileOutputStream(file);
                            if(b!=null){
                                 b.compress(Bitmap.CompressFormat.PNG, 90, bitmapStream);

                             }
                            Toast t = Toast.makeText(getActivity(), "Page is saved", Toast.LENGTH_LONG);
                            t.show();
                        } catch (FileNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            Toast t = Toast.makeText(getActivity(), "For some reasons, page isn't saved", Toast.LENGTH_LONG);
                            t.show();
                        }
                }catch (Exception e) {
                    // TODO: handle exception
                }}}}
            );

P.S 现状,我想保存.html文件等网页,但我找不到 - 怎么样?

0 个答案:

没有答案