1

I use below code for ScreenShot:

 public class startActivity{  
    Bitmap layoutBitmap = Bitmap.createBitmap(mReLayout.getWidth(),  mReLayout.getHeight(), Bitmap.Config.ARGB_4444);
    Canvas canvas = new Canvas(layoutBitmap);
    mReLayout.draw(canvas);
    Uri uri = getImageUri(getApplicationContext(), layoutBitmap);
    Intent mIntent = new Intent(CategoryActivity.this, MainActivity.class);
    mIntent.putExtra(Constant.BYTE_IMAGE, uri.toString());
    startActivity(mIntent);
}

MainActivity.class

private void setUpImageBitmap() {  
    Uri uri = Uri.parse(getIntent().getExtras().getString(Constant.BYTE_IMAGE));
    String selectedImagePath = getPath(uri);
    mImageCube.setImageBitmap(Utils.decodeSampledBitmapFromResource(selectedImagePath, 200, 200));

}

public String getPath(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    return cursor.getString(idx);
}

Class Utils

 public static Bitmap decodeSampledBitmapFromResource(String resId,
                                                     int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(resId, options);
}

public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 2;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float) height / (float) reqHeight);
        } else {
            inSampleSize = Math.round((float) width / (float) reqWidth);
        }
    }
    return inSampleSize;
}

How can I take a screenshot without saving it in the gallery?

Matthias
  • 4,481
  • 12
  • 45
  • 84
Ha Nguyen
  • 91
  • 8

2 Answers2

1

Here Below code for how to take screenshot for any android control, means ImageView or Layout. below code for ImageView.

  ImageView iv = (ImageView) findViewById(R.id.imageView1);
  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);

saveBitmap(myBitmap);

     public void saveBitmap(Bitmap bitmap) {
     String filePath = Environment.getExternalStorageDirectory()
     + File.separator + "Pictures/screenshot.png";
     String nomedia = Environment.getExternalStorageDirectory()
     + File.separator + "Pictures/.nomedia";
     File nomediaFile= new File(nomedia);
     if(!nomediaFile.exists()){
     nomediaFile.createNewFile();
       }
     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);
     }
   }

Now you dont want to show any images in gallery then you have to ceate .nomedia file in folder of screenshot save. see below code for that.

     String nomedia = Environment.getExternalStorageDirectory()
     + File.separator + "Pictures/.nomedia";
     File nomediaFile= new File(nomedia);
     if(!nomediaFile.exists()){
     nomediaFile.createNewFile();
       }

Above code already implemented in saveBitmap() method. I think help you for you this...

Now you want to share this image then get path of image and share with below code.

   public void shareImage(String path) {
              Uri myUri = Uri.parse("file://" + path);
              Intent shareIntent = new Intent();
              shareIntent.setAction(Intent.ACTION_SEND);
              shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello");
              shareIntent.putExtra(Intent.EXTRA_STREAM, myUri);
              shareIntent.setType("image/jpeg");
              shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
              startActivity(Intent.createChooser(shareIntent, "send"));
              }
Ravi Vaghela
  • 3,420
  • 2
  • 23
  • 51
0

The image files have to be stored somewhere. I think your actual question is how to hide image files from the gallery app.

To hide them from your gallery app there are two ways doing this:

1. Create a .nomedia file:

In the folder where you save your images (given by uri) you have to create a file with the name .nomedia.

2. Prefix folder with a dot:

You can rename the folder itself with a dot. Example: .images.

h0ch5tr4355
  • 2,092
  • 4
  • 28
  • 51