android.webkit.WebView has capturePicture() to capture the entire contents of the current WebView, but how would you do this for only a specific element on the webpage? For example, you might have many images and text elements positioned with CSS in a div.There may be many elements but I would just like to capture the div as it is rendered in the WebView.
The only idea I came up with is to load just that div only as the entire HTML of another WebView and call capturePicture. The other question how to take a Picture object to a format that can be used with the iText library (PNG, JPEG, GIF). Is there an easier way to accomplish this?
(A function of my app is to output PDF documents. )
you can use view.setDrawingCacheEnabled(true); to capture the view as image. and then you have to create a method to do that. and here is my saving method
void Save() {
if (null != view.getDrawable()) {
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
save = view.getDrawingCache();
final File myDir = new File(folder);
myDir.mkdirs();
final Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
final String fname = "StyleMe-" + n + ".png";
file = new File(myDir, fname);
if (file.exists())
file.delete();
try {
final FileOutputStream out = new FileOutputStream(file);
save.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
Toast.makeText(getApplication(), "Image Saved",
Toast.LENGTH_SHORT).show();
} catch (final Exception e) {
Toast.makeText(getApplication(),
"Something Went Wrong check if you have Enough Memory",
Toast.LENGTH_LONG).show();
}
} else {
final Toast tst = Toast.makeText(getApplication(),
"Please Select An Image First", Toast.LENGTH_LONG);
tst.setGravity(Gravity.CENTER, 0, 0);
tst.show();
}
view.setDrawingCacheEnabled(false);
}
Related
I have problem with my android code for taking screenshot while on video calling
I found several method for take screenshot on specific layout and this is what I used :
private void takeScreenshot() {
final FrameLayout container = (FrameLayout) findViewById(R.id.remote_video_view_container);
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
// create bitmap screen capture
container.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(container.getDrawingCache());
container.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
if(mPath!=null){
Toast savedToast = Toast.makeText(getApplicationContext(),
"Drawing saved to Gallery!", Toast.LENGTH_SHORT);
savedToast.show();
}
else{
Toast unsavedToast = Toast.makeText(getApplicationContext(),
"Oops! Image could not be saved.", Toast.LENGTH_SHORT);
unsavedToast.show();
}
outputStream.flush();
outputStream.close();
openScreenshot(imageFile);
} catch (Throwable e) {
// Several error may come out with file handling or DOM
e.printStackTrace();
}
}
private void openScreenshot(File imageFile) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(imageFile);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
}
i try this code for save my draw painting application that will save the drawing layout and then i try to use the same method for save video call layout but return black screen image.
i found that this method cannot use for real-time when i want to record it in video
source : How to programmatically take a screenshot in Android?
so, anyone help me what the best way to solve my problem ?
i am working on an wallpaper app in which i am receiving images from internet in GridView and Full Screen ImageView.
i want to save ImageView loaded image using its real image type, but i don't know how to get image type from ImageView.
example: if loaded image is JPEG or PNG then how do i get image type.
this is my code that i am using to save image as JPEG:
int intHeight = fullImageView.getHeight();
int intWidth = fullImageView.getWidth();
String dirname2 = "/Wallpapers HD/";
File myDir2 = new File(Environment.getExternalStorageDirectory()
.getPath() + dirname2);
myDir2.mkdirs();
String fname2 = "image" + intHeight + intWidth + ".jpeg";
File file2 = new File(myDir2, fname2);
if (file2.exists())
file2.delete();
try {
FileOutputStream out = new FileOutputStream(file2);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
Toast.makeText(_context, "failed", Toast.LENGTH_SHORT).show();
}
Perhaps you should keep track of this yourself. In the code you use to set the ImageView's drawable, you could apply a tag to the view to store that data:
String[] parts = fileName.split(".");
String extension = parts[parts.length-1];
imageView.setTag(extension);
And retrieve it from the view later:
String ext = (String) fullImageView.getTag();
I am working on Android for saving multiple image in sd card but its getting save in gallery also and specific folder also...
any one can help on this
thanks..
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
// 2
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
mImage.setImageBitmap(thumbnail);
CharSequence time2 = android.text.format.DateFormat.format(
"yymmddhhmmss", new java.util.Date());
String date1 = time2.toString();
//ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try {
thumbnail .compress(CompressFormat.JPEG, 100, new FileOutputStream(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/mypics/img" + date1 + ".png"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else
Toast.makeText(getBaseContext(), "Please take snap again",
Toast.LENGTH_LONG).show();
}
try like this::
Use this function to save your bitmap in SD card
private void SaveIamge(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
and add this permision in manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
to save in gallery just add this one:
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
If you do not want images inside the /mypics/img/ folder to be shown in the gallery. All you need to do is:
Include an empty file named .nomedia in your /mypcis/img/
directory (note the dot prefix in the filename). This will prevent
Android's media scanner from reading your media files and including
them in apps like Gallery or Music.
Note: For some devices, this only works for new folders (not those which are already scanned) So my recommendation would be to create a new folder with the .nomedia file and store your images to that. Now they will not appear in the gallery. Only in the external SD card folder.
Hi StackOverflow community,
in my application I'd like to have a button which allows the user to capture the current view.
For that reason, I've written the following method (oriented towards: Android save view to jpg or png ):
private LinearLayout container;
public void createImageOfCurrentView(View v) {
if (isExternalStoragePresent()) {
container = (LinearLayout) findViewById(R.id.container);
container.setDrawingCacheEnabled(true);
Bitmap b = container.getDrawingCache();
File dir = new File(Environment.getExternalStorageDirectory().getPath()
+ "/" + getPackageName() + "/");
dir.mkdirs();
File file = new File(dir, "image.jpg");
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
b.compress(CompressFormat.JPEG, 95, fos); // NullPointerException!
Toast.makeText(this, "The current view has been succesfully saved "
+ "as image.", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
Toast.makeText(this, "Unfortunatly an error occured and this "
+ "action failed.", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "Bacause of lacking access to the storage "
+ "of this device, the current view couldn't be saved as an image.",
Toast.LENGTH_LONG).show();
}
}
The problem is according to LogCat that there occured a NullPointerException when trying to create the jpeg. So probably the method container.getDrawingCache() is not working. On the phone the file is generated. However with the size of 0 bytes and no visible image. I would appreciate any suggestions what I have to do in order to make it work as I want.
Try it -
public static Bitmap convertViewToBitmap(View view) {
Bitmap result = null;
try {
result = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.RGB_565);
view.draw(new Canvas(result));
}catch(Exception e) {}
return result;
}
Try it -
view.setDrawingCacheEnabled(true);
Bitmap = bm = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
I am working on an application in which a user is allowed to fill up the form only once and next time when ever user views that form it should be in doc type and can be downloaded on its phone.
I do not have any idea on this link is this possible or not .And if its possible then how can it be done.Please suggest me a option or provide some useful links with code
I searched but couldn't find anything useful.
I have used code from following questions
Download and show the Thumbnail
File download on button click?
How to download a pdf file in Android?
But the point where i am lagging is i am not getting a way to convert whole screen into a doc/pdf.
Check out this code to save your screen as image.
private void saveImages() {
View v = findViewById(R.id.view_images);
v.setDrawingCacheEnabled(true);
// this is the important code :)
// Without it the view will have a dimension of 0,0 and the bitmap will
// be null
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false); // clear drawing cache
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyyMMddHHmmss");
Date date = new Date();
String name ="data"+"-"+dateFormat.format(date) + ".png";
// String imageName = "TEST" + (String) name;
File folder = new File(Environment.getExternalStorageDirectory()
+ "/.TEST");
// boolean success = false;
if (!folder.exists()) {
folder.mkdir();
}
File file = new File(folder + "/TEST" + name);
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
b.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
Log.d("Done", "Yes");
Toast.makeText(getApplicationContext(),
"Images" + name + "save in Sd card", Toast.LENGTH_SHORT)
.show();
} catch (Exception e) {
e.printStackTrace();
Log.d("Done", "No");
Toast.makeText(getApplicationContext(),
"Images in Sd card", Toast.LENGTH_SHORT).show();
}
finish();
}
There have been similar questions in StackOverflow. This should help:
Create PDF / Word (Doc) file within app
This too:
Android - creating word document