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);
Related
I have a code to take, save then share the screenshot of an Activity. My problem is I want just take the center of the activity so I add this code
private Bitmap cropBitmap(Bitmap bitmap) {
Bitmap bm = Bitmap.createBitmap(bitmap, 10, 10, 500, 500);
return bm;
}
But it still the original screenshot, please help me i try much solutions but it seems like I don't know how to apply this solutions because i'm very amateur in coding . So i hope to help me please give me the right solution dependent on my code .Thank you very very much
#Override
public void onClick(View v) {
Bitmap bitmap = takeScreenshot();
cropBitmap(bitmap);
saveBitmap(bitmap);
shareIt();
}
public Bitmap takeScreenshot() {
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
return bitmap;
}
private Bitmap cropBitmap(Bitmap bitmap) {
Bitmap bm = Bitmap.createBitmap(bitmap, 10, 10, 500, 500);
return bm;
}
private void saveBitmap(Bitmap bitmap) {
imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png"); ////File imagePath
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.CompressFormat.PNG, 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 shareIt() {
Uri myUri = Uri.fromFile(imagePath);
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("image/*");
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
String shareBody = "My highest score with screen shot";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Catch score");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
sharingIntent.putExtra(Intent.EXTRA_STREAM, myUri);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
});
displayResults();
Try like this:
Bitmap bitmap = takeScreenshot();
Bitmap bitmapCropped = cropBitmap(bitmap);
saveBitmap(bitmapCropped );
When I am taking snapshot of particular view in android. My whole view going to be blank after taking the screenshot. please check my whole code where I did wrong. I have searched so many quotes in Google but not able to solve my problem. Please someone help me.
// here is my code
fb_share_btn.setOnClickListener (new View.OnClickListener ( ) {
#Override
public void onClick(View view) {
boolean checkPermission = checkPermission();
/*Bitmap bitmap = takeScreenshot();*/
Bitmap bitmap = loadBitMapFromView(findViewById (R.id.tv_screenshot),findViewById (R.id.tv_screenshot).getWidth (),findViewById (R.id.tv_screenshot).getHeight ());
saveBitmap(bitmap);
shareIt();
}
});
// save bitmap function
public void saveBitmap(Bitmap bitmap) {
imagePath = new File (Environment.getExternalStorageDirectory ()+ "/screenshot.png");
Log.i ("Message","Testingabc:"+ imagePath);
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 Bitmap loadBitMapFromView(View v, int width, int height) {
Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas (b);
c.drawColor (Color.WHITE);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}
private void shareIt() {
Uri uri = FileProvider.getUriForFile(TimeCounter.this, BuildConfig.APPLICATION_ID + ".provider",imagePath);
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("image/*");
String shareBody = "In Tweecher, My highest score with screen shot";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Tweecher score");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
sharingIntent.setPackage("com.facebook.katana");
startActivity(sharingIntent);
}
}
use this code with AsycTask
#Override
protected void onPreExecute() {
try {
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
protected Integer doInBackground(Integer... integers) {
try {
File root = new File(Environment.getExternalStorageDirectory(), "/Screenshot/");
if (!root.exists()) {
root.mkdirs();
}
imageFile = new File(root.toString() + "/" + imageName + ".jpg");
FileOutputStream outputStream = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 75, outputStream);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}
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
Code work fine for first screenshot and keep taking same screenshot regardless of moving to another view.
How to get current screenshot?
public void saveBitmap(Bitmap bitmap) {
File imagePath = new File(Environment.getExternalStorageDirectory() + "/" + new SimpleDateFormat("yyyyMMddhhmmss'.jpg'").format(new Date()) );
FileOutputStream fos =null;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(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);
}
}
click info:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iSave:
Bitmap bitmap = null;
bitmap = takeScreenshot();
saveBitmap(bitmap);
break;
}
}
here:
public Bitmap takeScreenshot() {
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
Call rootView.setDrawingCacheEnabled(false); after taking the screen-shot. Turning it off and then on again forces it to update correctly.
public Bitmap takeScreenshot() {
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
Bitmap bitmap = rootView.getDrawingCache();
rootView.setDrawingCacheEnabled(false);
return bitmap;
}
I have ever tried to capture the current Activity and then share the screenshot. This below is how I did, take a look at them if you are still interested, and I think you would agree.
First, the get the root view of current Activity:
View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
or
View rootView = findViewById(android.R.id.content);
or
View rootView = findViewById(android.R.id.content).getRootView();
Second, get Bitmap from the root view:
public static Bitmap getScreenShot(View view) {
View screenView = view.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
return bitmap;
}
Third, store the Bitmap into the SDCard:
private final static String dir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Screenshots";
public static void store(Bitmap bm, String fileName){
File dir = new File(dir);
if(!dir.exists())
dir.mkdirs();
File file = new File(dir, fileName);
try {
FileOutputStream fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
At last, share the screenshot file:
private void shareImage(String file){
Uri uri = Uri.fromFile(file);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
intent.putExtra(android.content.Intent.EXTRA_TEXT, "");
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Share Screenshot"));
}
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.