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 );
Related
I need help.
I've already fetched an image from the server using web-services, but I don't share this image.
I attached my code, please help me to find the error.
share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Bitmap bitmap = viewToBitmap(iv, iv.getWidth(), iv.getHeight());
Intent shareintent = new Intent(Intent.ACTION_SEND);
shareintent.setType("image/jpeg");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
File file = new File(Environment.getExternalStorageDirectory() +
File.separator + "Imagedemo.jpg");
try {
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(byteArrayOutputStream.toByteArray());
}
catch (IOException e) {
e.printStackTrace();
}
shareintent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://sdcard/Imagedemo.jpg"));
startActivity(Intent.createChooser(shareintent,"share image"));
}
});
public static Bitmap viewToBitmap(View view, int width, int height){
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
Load image using url to Image view is simple
Picasso.get().load(imageUrl).into(imageView);
To share image from imageView using share button use code below
share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
BitmapDrawable bitmapDrawable = ((BitmapDrawable) imageVIew.getDrawable());
Bitmap bitmap = bitmapDrawable .getBitmap();
String bitmapPath = Images.Media.insertImage(getContentResolver(), bitmap,"some
title", null);
Uri bitmapUri = Uri.parse(bitmapPath);
Intent shareIntent=new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");
shareIntent.putExtra(Intent.EXTRA_STREAM, bitmapUri);
startActivity(Intent.createChooser(shareIntent,"Share Image"));
}
}
Change this line
shareintent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://sdcard/Imagedemo.jpg"));
To:
shareintent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/Imagedemo.jpg"));
or better way to get file from external storage is:
new File(Environment.getExternalStorageDirectory() + "/" + "Imagedemo.jpg")
one more thing setType for jpg image is
shareintent.setType("image/jpg");
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;
}
}
Basically, I want to take a screenshot of an entire scrollView. I've tried so many methods, but couldn't find the perfect one.
I've tried following:
public void takeScreenShot() {
mbitmap = getBitmapOFRootView();
createImage(mbitmap);
}
public void createImage(Bitmap bmp) {
String path = Environment.getExternalStorageDirectory().toString() + "/screenshot.jpg";
try {
FileOutputStream outputStream = new FileOutputStream(new File(path));
bmp.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public Bitmap getBitmapOFRootView() {
mScrollView.setDrawingCacheEnabled(true);
int totalHeight = mScrollView.getChildAt(0).getHeight();
int totalWidth = mScrollView.getChildAt(0).getWidth();
mScrollView.layout(0,0, totalWidth, totalHeight);
mScrollView.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(mScrollView.getDrawingCache());
mScrollView.setDrawingCacheEnabled(false);
return b;
}
This method almost works, but it's just showing me only 2 views and a button, other than that whole screen is black:
my xml contains so many views, it's view hierarchy is something like this:
<ScrollView>
<ConstraintLayout>
<Views>
....
<Views>
</ConstraintLayout>
</ScrollView>
I've referred so many StackOverflow post, but it didn't work.
So can anybody help me with it?
Update:
Finally found a solution for it. So, it was an issue with the background, solved it by drawing canvas over it. Like below:
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return bitmap;
You should be using a canvas for the same
public static Bitmap saveBitmapFromView(View view, int width, int height) {
Bitmap bmp = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
view.layout(0, 0, view.getLayoutParams().width, view.getLayoutParams().height);
view.draw(canvas);
return bmp;
}
Taking and Sharing screenshot in android programmatically
I've searched everywhere but found this one working
takeAndShareScreenshot()
private void takeAndShareScreenshot(){
Bitmap ss = takeScreenshot();
saveBitmap(ss);
shareIt();
}
takeScreenshot()
private Bitmap takeScreenshot() {
View view = // decore view of the activity/fragment;
view.setDrawingCacheEnabled(true);
return view.getDrawingCache();
}
saveBitmap()
private void saveBitmap(Bitmap bitmap) {
// path to store screenshot and name of the file
imagePath = new File(requireContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES) + "/" + "name_of_file" + ".jpg");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
shareIt()
private void shareIt() {
try {
Uri uri = Uri.fromFile(imagePath);
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/*");
String shareBody = getString(R.string.share_body_text);
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, R.string.subject);
sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBody);
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
} catch (Exception e) {
e.printStackTrace();
}
}
Note:
In recent versions of android (>Marshmallow I guess), you may need `write
access to external directory`
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"));
}
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);