Search code examples
javaandroidimageandroid-sdcardwebpage-screenshot

Create Specific Location for Webview captured JPG image


Ok so im trying to figure out how i can tweak this code to be able to save the image instead of /sdcard i want to save it somewhere like /sdcard/folder/Screenshot_xxxxx.jpg ,

Here is the code below.

  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);
                }

So my main goal is to modify code to where i can save this in a specific location, such as the apps own folder on the sdcard.

Thanks for any help, comments, source code examples, external links. I appreciate it in advance.


Solution

  • This Resolved my issue

     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;