如何拍摄截图,而不是将它保存在android的图库中

时间:2016-03-22 09:46:32

标签: android bitmap screenshot take

我使用以下代码进行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;
}

如何在不将其保存在图库中的情况下截取屏幕截图?

2 个答案:

答案 0 :(得分:0)

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

要从图库应用中隐藏它们,有两种方法可以做到这一点:

<强> 1。创建.nomedia文件:

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

<强> 2。带点的前缀文件夹:

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

答案 1 :(得分:0)

下面是如何为任何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"));
              }
相关问题