Android Convert current screen to bitmap

一笑奈何 提交于 2021-02-15 06:01:15

问题


I would like to convert a screen of my app and convert it into a Bitmap. I already know how to convert a view into a bitmap, but that's not what I want as my screen is made up of many fragments. Thus I'd like to be able to take the screen and convert it into a bitmap... Help is highly appreciated thanks.


回答1:


You could use something like this and pass the layout as a view ;)

public Bitmap takeScreenShot(View view) {
        // configuramos para que la view almacene la cache en una imagen
        view.setDrawingCacheEnabled(true);
        view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
        view.buildDrawingCache();

        if(view.getDrawingCache() == null) return null; // Verificamos antes de que no sea null

        // utilizamos esa cache, para crear el bitmap que tendra la imagen de la view actual
        Bitmap snapshot = Bitmap.createBitmap(view.getDrawingCache());
        view.setDrawingCacheEnabled(false);
        view.destroyDrawingCache();

        return snapshot;
    }



回答2:


View is you root layout:-

View v = view.getRootView();
v.setDrawingCacheEnabled(true);
Bitmap b = v.getDrawingCache();
String extr = Environment.getExternalStorageDirectory().toString();
File myPath = new File(extr, getString(R.string.free_tiket)+".jpg");
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) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

see link for more info :-

http://code.google.com/p/android-screenshot-library/wiki/DeveloperGuide

Android - How to take screenshot programatically




回答3:


View rootView = findViewById(R.id.relative_facebook).getRootView();
        rootView.setDrawingCacheEnabled(true);
        return rootView.getDrawingCache();


来源:https://stackoverflow.com/questions/23294489/android-convert-current-screen-to-bitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!