Android — taking a screen shot

后端 未结 3 454
后悔当初
后悔当初 2021-01-15 18:38

I need to take a screen shot and save the screen shot. I need to do this without using any connection to PC or without unrooting the phone. I need to do this whenever a even

3条回答
  •  太阳男子
    2021-01-15 19:39

    View ve = findViewById(R.id.loyout);
            ve.setDrawingCacheEnabled(true);
            Bitmap b = ve.getDrawingCache();
    
            String extr = Environment.getExternalStorageDirectory().toString()
                    + "/SaveCapture";
            myPath = new File(extr);
    
            if (!myPath.exists()) {
                boolean result = myPath.mkdir();
                Toast.makeText(this, result + "", Toast.LENGTH_SHORT).show();
            }
            myPath = new File(extr, getString(R.string.app_name) + ".jpg");
    
            Toast.makeText(this, myPath.toString(), Toast.LENGTH_SHORT).show();
            FileOutputStream fos = null;
    
            try {
                fos = new FileOutputStream(myPath);
    
                b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                fos.flush();
                fos.close();
                MediaStore.Images.Media.insertImage(getContentResolver(), b,
                        "Screen", "screen");
    
    
            }
    
            catch (FileNotFoundException e) {
    
                Log.e("Error", e + "");
            }
    
            catch (Exception e) {
    
                Log.e("Error", e + "");
            }
    

提交回复
热议问题