1

我将以下代码用于屏幕截图:

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

类实用程序

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

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

4

2 回答 2

1

下面是如何为任何 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);

保存位图(我的位图);

     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"));
              }
于 2016-03-22T10:26:05.600 回答
0

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

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

1. 创建一个 .nomedia 文件:

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

2. 用点前缀文件夹:

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

于 2016-03-22T10:00:20.170 回答