Header Ads

[Android] Taking screen shot and share image



First , using function take screen shot..in current activity.
using : 
private void takingScreenshot()
 {
 
      content = findViewById(R.id.layoutroot);
      Bitmap bitmap = content.getDrawingCache();
      File file = new File( Environment.getExternalStorageDirectory() +"test.png");     try 
      {
          file.createNewFile();
          FileOutputStream ostream = new FileOutputStream(file);
          bitmap.compress(CompressFormat.PNG, 100, ostream);
          ostream.close();
      } 
      catch (Exception e) 
      {
          e.printStackTrace();
      }
      Toast.makeText(GameOver.this, "Take sceen shot", Toast.LENGTH_SHORT).show();
 }
//remember: using name of image to share : like "test.png"


And then..using this function to share image....

 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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()
    + "/test.png";
  File imageFileToShare = new File(imagePath);
  Uri uri = Uri.fromFile(imageFileToShare);
  share.putExtra(Intent.EXTRA_STREAM, uri);
  startActivity(Intent.createChooser(share, "Share image to..."));
 }


You can share your URL .. follows function below ....
private void shareTextUrl() {
  Intent share = new Intent(android.content.Intent.ACTION_SEND);
  share.setType("text/plain");
  share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

  share.putExtra(Intent.EXTRA_SUBJECT, "Title Of The Post");
  share.putExtra(Intent.EXTRA_TEXT, "https://play.google.com/store/apps/developer?id=ThanhCS94");
  startActivity(Intent.createChooser(share, "Share text to..."));
 }
//Thank you !

No comments:

Powered by Blogger.