6

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.

Adifyr
  • 2,499
  • 6
  • 38
  • 59
  • 1
    So a screenshot? https://code.google.com/p/android-screenshot-library/ – shkschneider Apr 25 '14 at 13:26
  • you see, I don't want the user to know that a screenshot has been taken. Just capture the screen load it into a bitmap and then set the bitmap to an image view – Adifyr Apr 25 '14 at 13:30

3 Answers3

10

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;
    }
jackgris
  • 486
  • 2
  • 11
  • 64
  • ok, what does get drawing cache do? I want to capture the screen not just a single view. My screen is fragmented, there are many views that are distributed across xml layout files, merged together in one view – Adifyr Apr 25 '14 at 13:35
  • 2
    If you pass the layout root or main, it takes a snapshot of what's on the screen ;) – jackgris Apr 25 '14 at 13:37
  • What does get drawing cache do? You need read this [getDrawingCache](http://developer.android.com/reference/android/view/View.html#getDrawingCache%28boolean%29) – jackgris Apr 25 '14 at 13:49
0

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

Community
  • 1
  • 1
duggu
  • 37,191
  • 12
  • 114
  • 111
  • perhaps you didn't read my above comment. I don't want to actually take a screenshot. Just take the screen convert it into bitmap and set it as the `ImageBitmap` of a ImageView. – Adifyr Apr 25 '14 at 13:37
0
View rootView = findViewById(R.id.relative_facebook).getRootView();
        rootView.setDrawingCacheEnabled(true);
        return rootView.getDrawingCache();