为Webview捕获的JPG图像创建特定位置

时间:2012-10-19 23:40:51

标签: java android image android-sdcard webpage-screenshot

好的,所以即时通讯试图找出我如何调整此代码以便能够保存图像而不是/ sdcard我想将其保存在某处,如/sdcard/folder/Screenshot_xxxxx.jpg,

以下是代码。

  case R.id.settings_capture:
                item.setIcon(R.drawable.capture);                   
                //Resize the webview to the height of the webpage
                int pageHeight = web.getContentHeight();
                LayoutParams browserParams = web.getLayoutParams();
                web.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, pageHeight));

                //Capture the webview as a bitmap
                web.setDrawingCacheEnabled(true);
                Bitmap bitmap = Bitmap.createBitmap(web.getDrawingCache());
                web.setDrawingCacheEnabled(false);

                //Create the filename to use
                String randomFilenamepart = String.valueOf(new SecureRandom().nextInt(1000000));
                String filename = Environment.getExternalStorageDirectory().toString() + "/ScreenShot_" + randomFilenamepart + ".jpg";

                File imageFile = new File(filename);
                //Stream the file out to external storage as a JPEG
                OutputStream fout = null;
                try {
                    fout = new FileOutputStream(imageFile);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout);
                    fout.flush();
                    fout.close();
                    Toast.makeText(WebViewClientDemoActivity.this, "Screen Capture Saved!\n\nImage Saved at location : /sdcard\n\nSaved As: ScreenShot_xxxxx.jpg", Toast.LENGTH_LONG).show();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    Toast.makeText(WebViewClientDemoActivity.this, "Problem with Capturing Image or Location to Store Image", Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    web.setLayoutParams(browserParams);
                }

所以我的主要目标是将代码修改到我可以将其保存在特定位置的位置,例如SD卡上的应用程序自己的文件夹。

感谢您提供任何帮助,评论,源代码示例,外部链接。我提前感激。

3 个答案:

答案 0 :(得分:1)

更新此行以包含所需的路径:

     File imageFile = new File(filename);

e.g。

      File imageFile = new File("/sdcard/folder/"+filename);

请在执行代码之前确保已安装文件驱动器。

答案 1 :(得分:1)

尝试此代码:将图像存储在特殊文件夹和具有不同名称的图像

            String root = Environment.getExternalStorageDirectory()
                    .toString();
            File myDir = new File(root + "/image_folder_name");
            myDir.mkdirs();
            Random generator = new Random();
            int n = 10000;
            n = generator.nextInt(n);
            String fname = "Image-" + n + ".jpg";
            file = new File(myDir, fname);

            Log.i(TAG, "" + file);

            if (file.exists())
                file.delete();
            try {
                FileOutputStream out = new FileOutputStream(file);
                bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
                out.flush();
                out.close();

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

答案 2 :(得分:1)

这解决了我的问题

 case R.id.settings_capture:
                item.setIcon(R.drawable.capture);                   
                //Resize the webview to the height of the webpage
                int pageHeight = web.getContentHeight();
                LayoutParams browserParams = web.getLayoutParams();
                web.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, pageHeight));

                //Capture the webview as a bitmap
                web.setDrawingCacheEnabled(true);
                Bitmap bitmap = Bitmap.createBitmap(web.getDrawingCache());
                web.setDrawingCacheEnabled(false);

               // final DateFormat DF = new SimpleDateFormat("yyyyMMdd_HHmmss_SSS");
                String out = new SimpleDateFormat("EEE_MMM_dd_yyyy hh:mm.s'.jpg'").format(new Date());

                //Create the filename to use
                String target_filename  = "FolderName-" + (out);
                        //+ ".jpg";
                try {
                    File targetDir = new File(Environment.getExternalStorageDirectory(), "/Enlighten");
                if (!targetDir.exists()){   
                    targetDir.mkdirs(); }
                    File file = new File(targetDir, target_filename);
                    FileOutputStream fos = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                    fos.flush();
                    fos.close();
                    Toast.makeText(WebViewClientDemoActivity.this, "Capture Saved!\n\nImage Stored @ /sdcard/Location", Toast.LENGTH_LONG).show();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    Toast.makeText(WebViewClientDemoActivity.this, "Problem Storing Image To: /sdcard/Location", Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    web.setLayoutParams(browserParams);
                }
                return true;