Create Specific Location for Webview captured JPG image
-
13-07-2021 - |
Question
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 3
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;
OTHER TIPS
Update this line to include the desired path:
File imageFile = new File(filename);
e.g.
File imageFile = new File("/sdcard/folder/"+filename);
Please make sure the file drive is mounted before executing the code.
try this code: To store images in special folder and images with different names
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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow