活动的屏幕截图左下方黑色代替导航按钮

时间:2017-03-23 11:10:59

标签: android screenshot

我正在xperia设备上进行测试,底部有一个触摸导航按钮。 我的代码捕获了当前活动的屏幕截图,我不想在下面包含导航按钮,所以我使用

隐藏它
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

现在,当我按下"拍摄screenShot"我可以看到NavButton隐藏,活动全屏显示音量,但保存的截图在底部留空。可能是什么问题?下面是输出图像!!

enter image description here

以上黑色空间是左侧空间。我不想让它留下

以下是代码。

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fullscreen);
    takeScreenshot= (Button) findViewById(R.id.takeScreenshot);
    Drawable icon= getResources().getDrawable( R.drawable.ic_photo_camera);
    takeScreenshot.setCompoundDrawablesWithIntrinsicBounds( icon, null, null, null );
    takeScreenshot.setOnClickListener(this);
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 46323);
    settings= (ImageButton) findViewById(R.id.settings);
    settings.setOnClickListener(this);
}

 @Override
 public void onClick(View v) {
   int click=v.getId();
    if (click==R.id.takeScreenshot){
        isStoragePermissionGranted();
       // View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
  //      RelativeLayout relativeLayout= (RelativeLayout) findViewById(R.id.relativeLayout);
        View rootView=findViewById(R.id.relativeLayout);
        takeScreenshot.setVisibility(View.VISIBLE);
        settings.setVisibility(View.VISIBLE);
        try{
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        }catch (Exception ex){
            Log.d(TAG,"Non Navigation button");
        }
        Bitmap bmp=getScreenShot(rootView);
        store(bmp);
        takeScreenshot.setVisibility(View.VISIBLE);
        settings.setVisibility(View.VISIBLE);

        Log.d(TAG, Environment.getDataDirectory().toString());
    }
    if (click==R.id.settings){

    }
}
public static Bitmap getScreenShot(View view) {
    View screenView = view.getRootView();
    screenView.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
    screenView.setDrawingCacheEnabled(false);
    return bitmap;
}
public void store(Bitmap bm){
    String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/iScreenShot";
    File dir = new File(Environment.getExternalStorageDirectory().toString()+"/iScreenShot");
    if(!dir.exists())
        dir.mkdirs();
    String tempFileName="shot";
    String extension=".png";
    int num=0;
    File file=new File(dirPath,tempFileName+num+extension);
    while (file.exists()){
        num++;
        file=new File(dirPath,tempFileName+num+extension);
    }
    try {
        FileOutputStream fOut = new FileOutputStream(file);
        // bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
        bm.compress(Bitmap.CompressFormat.PNG,100,fOut);
        fOut.flush();
        fOut.close();
        Toast.makeText(this, "Saved in gallery", Toast.LENGTH_SHORT).show();
        try {
            MediaPlayer mMediaPlayer = new MediaPlayer();
            mMediaPlayer = MediaPlayer.create(this, R.raw.shutter);
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.setLooping(false);
            mMediaPlayer.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
        MediaScannerConnection.scanFile(this, new String[] { Environment.getExternalStorageDirectory().toString()+"/iScreenShot/"+tempFileName+num+extension }, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                        Log.i("ExternalStorage", "Scanned " + path + ":");
                        Log.i("ExternalStorage", "-> uri=" + uri);
                    }
                });

    } catch (Exception e) {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

  

使用此方法隐藏状态栏以及setContentView

之前的导航栏
public void setFullScreenView() {//Hiding status/navigation bar
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        }
        setContentView(R.layout.main_activity);
    }

然后调用您的方法进行截屏