Android Open Source - slf4android Make Screen Shot Async Task






From Project

Back to project page slf4android.

License

The source code is released under:

MIT License

If you think the Android project slf4android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package pl.brightinventions.slf4android;
//from ww w  .j  a  v  a  2 s.com
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.view.View;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.CountDownLatch;

public class MakeScreenShotAsyncTask extends AsyncTask<Context, Void, File> {
    private static final Logger LOG = LoggerFactory.getLogger(MakeScreenShotAsyncTask.class.getSimpleName());

    @Override
    protected File doInBackground(Context... params) {
        Context context = params[0];
        File result = null;
        if (context instanceof Activity) {
            final Activity activity = (Activity) context;
            final CountDownLatch latch = new CountDownLatch(1);
            final View activityContent = activity.findViewById(android.R.id.content);
            final Bitmap[] bitmaps = new Bitmap[1];
            if (activityContent == null) {
                LOG.warn("Activity content view is null");
                latch.countDown();
            } else {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            boolean wasEnabled = activityContent.isDrawingCacheEnabled();
                            if (!wasEnabled) {
                                activityContent.setDrawingCacheEnabled(true);
                            }
                            LOG.debug("Will now make screen shot of {}", activityContent);
                            if (activityContent.getMeasuredWidth() == 0 && activityContent.getMeasuredHeight() == 0) {
                                activityContent.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                                activityContent.layout(0, 0, activityContent.getMeasuredWidth(), activityContent.getMeasuredHeight());
                            }
                            activityContent.buildDrawingCache();
                            Bitmap drawingCache = activityContent.getDrawingCache();
                            boolean gotBitmap = drawingCache != null;

                            LOG.debug("Got drawing cache from root view {}", gotBitmap);
                            if (gotBitmap) {
                                bitmaps[0] = Bitmap.createBitmap(drawingCache);
                                drawingCache.recycle();
                            }
                            if (!wasEnabled) {
                                activityContent.setDrawingCacheEnabled(false);
                            }
                            latch.countDown();
                        } catch (Exception e) {
                            LOG.warn("Failed to take screen shot", e);
                        }
                    }
                });
            }
            try {
                latch.await();
                if (bitmaps[0] != null) {
                    Bitmap screenShot = bitmaps[0];
                    File cacheDir = context.getExternalCacheDir();
                    if (cacheDir == null) {
                        cacheDir = context.getCacheDir();
                    }
                    if (cacheDir != null) {
                        File bitmapFile = new File(cacheDir, "slf4android_screen.png");
                        if (bitmapFile.exists()) {
                            bitmapFile.delete();
                        }
                        OutputStream out = new FileOutputStream(bitmapFile);
                        screenShot.compress(Bitmap.CompressFormat.PNG, 100, out);
                        out.flush();
                        out.close();
                        screenShot.recycle();
                        result = bitmapFile;
                    } else {
                        LOG.warn("Failed to find a directory for screen shot");
                    }
                }
            } catch (InterruptedException e) {
                LOG.warn("Interrupted while waiting for screen shot", e);
            } catch (IOException e) {
                LOG.warn("Failed to create bitmap screen shot file", e);
            }

        }
        return result;
    }
}




Java Source Code List

com.squareup.seismic.ShakeDetector.java
org.slf4j.impl.StaticLoggerBinder.java
pl.brightinventions.slf4android.ActivityStateListener.java
pl.brightinventions.slf4android.AndroidLoggerAdapter.java
pl.brightinventions.slf4android.AndroidLoggerFactory.java
pl.brightinventions.slf4android.AtLeastFilter.java
pl.brightinventions.slf4android.ConstLoggerValueSupplier.java
pl.brightinventions.slf4android.DateValueSupplier.java
pl.brightinventions.slf4android.Disposable.java
pl.brightinventions.slf4android.EmailErrorReport.java
pl.brightinventions.slf4android.FileHandlerExpose.java
pl.brightinventions.slf4android.FileLogHandlerConfiguration.java
pl.brightinventions.slf4android.FileLogHandler.java
pl.brightinventions.slf4android.HandlerFormatterCompiler.java
pl.brightinventions.slf4android.LevelValueSupplier.java
pl.brightinventions.slf4android.ListLogRecordFormatter.java
pl.brightinventions.slf4android.Lists.java
pl.brightinventions.slf4android.LogLevel.java
pl.brightinventions.slf4android.LogRecordFormatterUtilFormatterAdapter.java
pl.brightinventions.slf4android.LogRecordFormatter.java
pl.brightinventions.slf4android.LogRecord.java
pl.brightinventions.slf4android.LogcatHandler.java
pl.brightinventions.slf4android.LoggerConfiguration.java
pl.brightinventions.slf4android.LoggerNameValueSupplier.java
pl.brightinventions.slf4android.LoggerPatternConfiguration.java
pl.brightinventions.slf4android.LoggerPatternValueSupplier.java
pl.brightinventions.slf4android.LoggerPattern.java
pl.brightinventions.slf4android.MakeScreenShotAsyncTask.java
pl.brightinventions.slf4android.MessageValueSupplier.java
pl.brightinventions.slf4android.NotifyDeveloperDialogDisplayActivity.java
pl.brightinventions.slf4android.NotifyDeveloperHandler.java
pl.brightinventions.slf4android.ReadLogcatEntriesAsyncTask.java
pl.brightinventions.slf4android.ThreadValueSupplier.java
pl.brightinventions.slf4android.androidTest.FileHandlerTests.java
pl.brightinventions.slf4android.androidTest.LoggerAdapterTests.java
pl.brightinventions.slf4android.androidTest.NotifyDeveloperHandlerTests.java
pl.brightinventions.slf4android.androidTest.TestActivity.java
pl.brightinventions.slf4android.roboelectric.HandlerFormatterCompilerTests.java
pl.brightinventions.slf4android.roboelectric.MessageValueSupplierTests.java
pl.brightinventions.slf4android.roboelectric.RoboelectricTest.java
pl.brightinventions.slf4android.roboelectric.RobolectricTestRunner.java