1

我正在尝试创建一个应用程序屏幕截图一切顺利,但捕获的结果是一个空文件,我应该怎么做才能让我的应用程序正确地提供一个非空文件,请帮助我,谢谢。

这是我的代码:

MainActivity 类:

public class MainActivity extends Activity{
    Utilities u = new Utilities();
    Context context;
    Handler ssHandler = new Handler();
    File input = new File(u.getExternalDirectory()+"/BootManager/screenshot.bmp");
    private String ssDir = u.getExternalDirectory()+"/DCIM/screens";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context = getApplicationContext();
        File dcim = new File(u.getExternalDirectory()+"/DCIM");
        if(!dcim.exists())
            dcim.mkdir();
        File screens = new File(dcim+"/screens");
        if(!screens.exists())
            screens.mkdir();
        u.execCommand(context.getFilesDir().getAbsolutePath()+"/busybox chmod 777 /dev/graphics/fb0");
        takeScreenshot(3);
        finish();

    }

    void takeScreenshot(final int delay)
    {
        ssHandler.postDelayed(new Runnable()
        {
            public void run()
            {
                takescreenshot();
            }
        }, delay * 1000);
    }

    private void takescreenshot(){

        if(input.exists())
            input.delete();



        u.execCommand(context.getFilesDir().getAbsolutePath()+"/bootmanagerSS tryScreenshotClient");
        /*if(!input.exists()){
            @SuppressWarnings("unused")
            int i = ssl.takeScreenShot("/dev/graphics/fb0");
        }
        */


      converttoPNG();
        Toast.makeText( MainActivity.this, "screenshot_saved", Toast.LENGTH_LONG).show();

    }

    private void converttoPNG() {
        Bitmap bmp = null;
        Calendar c = Calendar.getInstance();
        int month = c.get(Calendar.MONTH) + 1;
        int minute = c.get(Calendar.MINUTE);
        String minutes = String.valueOf(minute);
            if(minutes.length() < 2){
                minutes = "0"+minutes;
              }
        String sDate = month + "-" + c.get(Calendar.DAY_OF_MONTH) + "-" + c.get(Calendar.YEAR) + "-" + c.get(Calendar.HOUR_OF_DAY) + "." + minutes;
        try {
             bmp = BitmapFactory.decodeStream(new FileInputStream(input));
            FileOutputStream fout = new FileOutputStream(ssDir+"/"+sDate+".jpg");
            bmp.compress(CompressFormat.JPEG, 100, fout);
            fout.close();


        } catch (FileNotFoundException e) {

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

            e.printStackTrace();
        }
        input.delete();
        startMediaScanner(ssDir+"/"+sDate+".jpg");

    }

    private void startMediaScanner(String addedPicture)
    {
         sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+ addedPicture)));
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
    }


}

Utilities 类中的 execCommand 函数:

public Boolean execCommand(String command) 
    {
      Process process;
        try {
            Runtime rt = Runtime.getRuntime();
            process = rt.exec("su");
            DataOutputStream os = new DataOutputStream(process.getOutputStream()); 
            os.writeBytes(command + "\n");
            os.flush();
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
            os.close();
        } catch (IOException e) {
           return false;
        } catch (InterruptedException e) {
           return false;
        }
        return true; 
    }
4

1 回答 1

0

试试这个,它可能会给你一些建议。

public class GrabIt {
private GrabIt() {
}

public static Bitmap takeScreenshot(View view) {
    assert view.getWidth() > 0 && view.getHeight() > 0;
    Bitmap.Config config = Bitmap.Config.ARGB_8888;
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), config);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);

    return bitmap;
}

}

于 2013-08-15T13:01:29.303 回答