The following code works.However it only takes a screenshot of whatever items are in view to the user , if the app is running on a small screen and a textview is not being shown ( have to scroll up or down) , the screenshot will not show the textview. How do I take a screenshot of the entire activity regardless of the screensize?
public Bitmap takeScreenshot() {
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
public void saveBitmap(Bitmap bitmap) {
File imagePath = new File(Environment.getExternalStorageDirectory() + "/groceryrun.png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
private void shareImage() {
Intent share = new Intent(Intent.ACTION_SEND);
// If you want to share a png image only, you can do:
// setType("image/png"); OR for jpeg: setType("image/jpeg");
share.setType("image/*");
// Make sure you put example png image named myImage.png in your
// directory
String imagePath = Environment.getExternalStorageDirectory()
+ "/groceryrun.png";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share This Deal With Your Friends!"));
}
Its because you are taking screenshot of a View who is just a kid. go for the parent
Activity.getWindow().getDecorView()
now call your codes.
let me know if it works
Related
Below code will capture the screen and store it in SD card. I want Then it will send this file via sharable apps.I want to store it in internal memory of phone instead but I am unable to do that.Please help me for the same.
WebView view = (WebView) findViewById(R.id.webView1);
#SuppressWarnings("deprecation")
Picture picture = view.capturePicture();
Bitmap b = Bitmap.createBitmap( picture.getWidth(),
picture.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas( b );
picture.draw( c );
String filePath = Environment.getExternalStorageDirectory()
+ File.separator + "score.png";
File imagePath = new File(filePath);
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
if(fos!=null)
{
b.compress(CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
}
if(imagePath.exists())
{
sendMail(filePath);
}
else
{
Log.e("fie","file doesnt exist");
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
public void sendMail(String path) {
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
//emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
// new String[] { "youremail#website.com" });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"My Score in Mock Test");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
"PFA");
emailIntent.setType("image/png");
Uri myUri = Uri.parse("file://" + path);
emailIntent.putExtra(Intent.EXTRA_STREAM, myUri);
startActivity(Intent.createChooser(emailIntent, "share score card..."));
}
I tried to achieve this with below code found on stack overflow but it is not working. This code is not showing any exception when I debug through it but file is not creating to internal memory and so sending is failing.
I want to share animated gif images that are in my drawable folder.
The code works so far, but the shared gif file is not animated. You can only see the first image of the animation. Does someone know how it could work?
Bitmap icon = BitmapFactory.decodeResource(this.getResources(),
R.drawable.animated_gif);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/gif");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.PNG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "temporary_file.gif");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f, true);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file:///sdcard/temporary_file.gif"));
startActivity(Intent.createChooser(share, "Share Image"));
Well, you are geting static bitmap from drawable. I recomend you to use GifDrawable in Glide library and this approach for sending animated gifs (in case you loaded your gif image into ImageView):
private Uri getLocalBitmapUri(ImageView imageView, String link) {
// Extract Bitmap from ImageView drawable
Drawable drawable = imageView.getDrawable();
if (drawable instanceof GifDrawable) {
try {
// Store image to default external storage directory
String fileName = link.substring(link.lastIndexOf('/') + 1, link.length());
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "shared_gif_" + System.currentTimeMillis() + ".gif");
file.getParentFile().mkdirs();
GifDrawable gifDrawable = ((GifDrawable) imageView.getDrawable());
FileOutputStream out = new FileOutputStream(file);
out.write(gifDrawable.getData());
out.close();
return Uri.fromFile(file);
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
...
Uri bmpUri = Utils.getLocalBitmapUri(gifImageView, post.media_content.get(0).file);
if (bmpUri != null) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("image/gif");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "title");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "text");
sharingIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
} else {
// ...sharing failed, handle error
}
...
Hi i have an application to take the screenshot and send to the email. When i took the screenshot second time and attach to the email, the email contains the first screenshot. I think the bitmap is not clearing. Can any one please help me for that. I am sorry for my poor english.
This is my code;
email_icon1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "email_icon clicked", Toast.LENGTH_SHORT).show();
View v1 = getWindow().getDecorView().getRootView();
// View v1 = iv.getRootView(); //even this works
// View v1 = findViewById(android.R.id.content); //this works too
// but gives only content
v1.setDrawingCacheEnabled(true);
myBitmap = v1.getDrawingCache();
saveBitmap(myBitmap);
}
});
public void saveBitmap(Bitmap bitmap) {
String filePath = Environment.getExternalStorageDirectory()
+ File.separator + "Pictures/screenshot.png";
File imagePath = new File(filePath);
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
sendMail(filePath);
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
public void sendMail(String path) {
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { "athulya#extraslice.com" });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"giMobile ScreenShot");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
"Sent from my AndroidTab");
emailIntent.setType("image/png");
Uri myUri = Uri.parse("file://" + path);
emailIntent.putExtra(Intent.EXTRA_STREAM, myUri);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
Thanks in Advance.
in order for you to recreate the bitmap from the view please follow this order
holder.setDrawingCacheEnabled(true);
Bitmap bmp = holder.getDrawingCache();
then after saving make sure you destroy the view Caches and add this to the end of your save method to completely destroy the view cache and to re-start again re-drawing the view each time you click the save method or whatever method you are using..
holder.setDrawingCacheEnabled(false);
After getting bitmap do this
v1.setDrawingCacheEnabled(true); myBitmap = v1.getDrawingCache();
v1.setDrawingCacheEnabled(false); saveBitmap(myBitmap);
I was Developing an application which will have image on image view .
My need:
What i need is When i click the button then it should store the image that exist in the image view to the sd card(emulator).
Here is how i used:(but no expected results)
Button btnWriteSDFile = (Button) findViewById(R.id.btnWriteSDFile);
btnWriteSDFile.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ImageView myImage = (ImageView) findViewById(R.id.imageView1);
BitmapDrawable drawable = (BitmapDrawable) myImage.getDrawable();
Bitmap bitmap = drawable.getBitmap();
File sdCardDirectory = Environment.getExternalStorageDirectory();
File image = new File(sdCardDirectory, "image.png");
boolean success = false;
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
Toast.makeText(getApplicationContext(), "Image saved with success",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Error during image saving", Toast.LENGTH_LONG).show();
}
}
});
In my above code i didnt get any error
It simply Shows that "save file in mnt/sd/image.png" but no images found.
It would be appreciable if some one helps me to get me out from this rid.
**
"Found out where the exact issues": My program was running perfectly
# 1st time if i click button and check in gallery means there is no
image but once i close and open the emulator then there is a image.But
i need to see the image as soon as updated how to do this any ideas?
**
Your code looks good, but in order to write something to external storage, you should have the following permission declared in the manifest:
android.permission.WRITE_EXTERNAL_STORAGE
if you use some thing like
ImageView myImage = (ImageView) findViewById(R.id.imageView1);
myImage .setDrawingCacheEnabled(true);
myImage .buildDrawingCache();
Bitmap bitmap = myImage .getDrawingCache();
File sdCardDirectory = Environment.getExternalStorageDirectory();
File image = new File(sdCardDirectory, "image.png");
boolean success = false;
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
Toast.makeText(getApplicationContext(), "Image saved with success",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Error during image saving", Toast.LENGTH_LONG).show();
}
}
});
and please set the following permission in androidManifest.xml
android.permission.WRITE_EXTERNAL_STORAGE
Try below code to take a screenshot
private static Bitmap takeScreenShot()
{
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
return bitmap;
}
For saving
private File saveBitmap(Bitmap bitmap)
{
File snapShot=null;
try
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "test.jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
} catch (Exception e)
{
e.printStackTrace();
}
return snapShot;
}
Need to give permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I am using ImageView in my android application here i show the images from webservice so i am using UrlImageViewHelper. i want to store this image into android Gallery files.
my images like:
String Images = dataExtra.get("images").toString();
System.out.println("image URL"+Images);
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(Images);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
i tried like this.. but its not working. Any one can help me how to store these Images into Android Gallery?
i got solution for this problem, Here my answer
private void saveImagesIntoGallery(){
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
// File sdCardDirectory = Environment.getExternalStorageDirectory();// its stores under sdcard not in a specific path
String sdCardDirectory = Environment.getExternalStorageDirectory().toString()+"/Pictures/";
String url = arrayForImages[i].toString();
String file = url.substring(url.lastIndexOf('/')+1);
System.out.println("PATH NAME"+sdCardDirectory);
File image = new File(sdCardDirectory, file);
boolean success = false;
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
Toast.makeText(getApplicationContext(), "Image saved with success",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Error during image saving", Toast.LENGTH_LONG).show();
}
}
Are you sure that the gallery is the best place to store images of webservice?
If you wanted to save to internal storage:
public void saveBitmap(String name, Bitmap bitmap){
if(bitmap!=null && name!=null){
FileOutputStream fos;
if(bitmap!=null){
try {
fos = openFileOutput(name, Context.MODE_PRIVATE);
bitmap.compress(CompressFormat.JPEG, 90, fos);
} catch (FileNotFoundException e) {}
}
}
}
To gallery, i have not examples here. But search a little;)