Question

I have a requirement to generate a bitmap out of an EditText and then perform some manipulations on it. My main concern is not to call View.buildDrawingCache() method on the UI thread and possibly block it, especially when talking about large screens (i.e. Nexus 10) since the EditText will occupy about 80% of the available screen size.

I execute Runnables inside a ThreadPoolExecutor, those will inflate dummy views on a worker thread and set all the required attributes to them, then simply call buildDrawingCache() & getDrawingCache() to generate a bitmap.

This works perfect on some devices yet recently I have encountered a few devices that crash with the following message:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

I understand why this happens, as some phones must have modified implementation for EditText that creates a Handler and thus requires Looper.prepare() to be called first.

From what I've read online there is no issue with calling Looper.prepare() inside a worker thread though some stated it is highly unrecommended yet I could not find a reason for that.

Other than that, most posts related to this issue state you are not supposed to inflate views inside a background thread, probably due to the following from Android's official documentation (Processes and Threads):

"Do not access the Android UI toolkit from outside the UI thread"
  • What is the recommended approach to dealing with this problem?

  • Is there any harm in calling build/get drawingcache from the main thread? (performance-wise)

  • Will calling Looper.prepare() inside my worker thread solve this problem?

EDIT

Just to elaborate on my specific requirement, I have a user-interface consisting of an ImageView and a custom EditText on top of it, the EditText can change it's font and color according to the user selection, it can be zoomed in/out using "pinch to zoom" gesture and can also be dragged around to allow the user to reposition it on top of the image.

Eventually what I do is create a dummy view inside my worker thread using the exact same values (width, height, position) it currently has on the UI and then generate it's drawingcache, the original image's bitmap is decoded again from a local file.

Once the two bitmaps are ready I merge them into a single bitmap for future use.

So to put it simple, is there anything wrong with executing the following code (from within a background thread):

Call Looper.prepare() Create a new view with application context, call measure() & layout() manually and then build+get drawingcache from it, i.e.:

Looper.prepare();

EditText view = new EditText(appContext);

view.setText("some text");
view.setLayoutParams(layoutParams);

view.measure(
        View.MeasureSpec.makeMeasureSpec(targetWidth, View.MeasureSpec.EXACTLY),
        View.MeasureSpec.makeMeasureSpec(targetHeight, View.MeasureSpec.EXACTLY));

view.layout(0, 0, targetWidth, targetHeight);

view.buildDrawingCache();

Bitmap bitmap = view.getDrawingCache();

How does this apply to the restriction with not accessing the Android UI toolkit from outside the UI thread, what could possibly go wrong?

Was it helpful?

Solution

In your case, you can do it of course, but be carefull only reading values from UI data, to avoid synchronizations bug.

Also you should not recreate the EditText from the background thread, it will be more efficient to directly access the already existant one instead:

Looper.prepare();
myEditText.setDrawingCacheEnabled(true);
Bitmap bitmap = myEditText.getDrawingCache();

If your question is : why it is not recommanded by android guidelines, here is a good SO answer to your question.

OTHER TIPS

Calling View.buildDrawingCache() calls Bitmap.nativeCreate which can be a large allocation, so yes, it can be potentially harmful to run on main thread. I don't see a problem with calling Looper.prepare() in your background thread. However, it's unclear what you are trying to achieve and there may be a better solution to your problem.

The reason you are not supposed to the UI toolkit from other threads is that it is not written to be thread safe it is written under the assumption that only one thread runs it. This means it's really hard to tell what can go wrong, the bad effects, if any, will mostly happen in an un-repeatable due to specific timing of threads. Your description of what you are trying to do it not too clear. In your case, I would just allocate a large bitmap, and draw text into it. Why are you using the EditText in the first place ? It seems like a kind of a hack, and hacks tend to break eventually.

Why View.buildDrawingCache()? What about using View.draw(Canvas canvas) to manually render to a Canvas backed by a Bitmap? Method seems simple enough to not cause problems on background threads.

EditText edit = (EditText)findViewById(R.id.edit);
edit.buildDrawingCache();
ImageView img = (ImageView)findViewById(R.id.test);
img.setImageBitmap(edit.getDrawingCache());

Lalit when you try to build the cache in the onCreate method, the drawing hasn't happened yet so the drawingCache should have nothing. Either put the buildDrawingChache method in the onClick method. Or use the following code in onCreate.

ViewTreeObserver vto = editText.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() {
        editText.buildDrawingCache();
        } 
});

I also encountered this error a few times already:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

my solution:

new Thread(new Runnable(){
  @Override
  public void run(){
    //add implementations that DOES NOT AFFECT the UI here

    new Handler(Looper.getMainLooper()).post(new Runnable() {       
      @Override
      public void run(){
         //manage your edittext and Other UIs here
      }
    });
  }
}).start();

just create a handler inside your worker thread to apply data changes to your UI

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top