1

好的,所以我想弄清楚如何调整此代码以保存图像而不是 /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);
                }

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

感谢任何帮助、评论、源代码示例、外部链接。我提前感谢它。

4

3 回答 3

1

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

     File imageFile = new File(filename);

例如

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

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

于 2012-10-19T23:59:19.743 回答
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;
于 2012-10-23T07:50:21.823 回答
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();
            }
于 2012-10-20T04:31:57.400 回答