如何截取屏幕截图而不将其保存在 android 中的图库中

how to take screenshot and not save it on gallery in android

我使用下面的截图代码:

 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 工具

 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;
}

如何截取屏幕截图而不将其保存在图库中?

图像文件必须存储在某个地方。我认为您的实际问题是如何从图库应用中隐藏图像文件。

要从您的图库应用中隐藏它们,有两种方法:

1.创建一个 .nomedia 文件:

在保存图像的文件夹中(由 uri 指定),您必须创建一个名为 .nomedia.

的文件

2。带点的前缀文件夹:

您可以用点重命名文件夹本身。示例:.images.

这里下面的代码是关于如何为任何 android 控件截屏的代码,指的是 ImageView 或 Layout。下面是 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);
     }
   }

现在您不想在图库中显示任何图像,那么您必须在屏幕截图保存的文件夹中创建 .nomedia 文件。请参阅下面的代码。

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

以上代码已在 saveBitmap() 方法中实现。我想这对你有帮助...

现在你想分享这张图片,然后获取图片路径并用下面的代码分享。

   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"));
              }