Search code examples
javaillegalstateexceptionui-threadhardware-accelerationtextureview

TextureView getBitmap java.lang.IllegalStateException


I start a thread to getBitmap through TextureView,however,I get an exception:

E/AndroidRuntime: FATAL EXCEPTION: Thread-814
java.lang.IllegalStateException: Hardware acceleration can only be used with a single UI thread.
Original thread: Thread[main,5,main]
Current thread: Thread[Thread-814,5,main]
    at android.view.HardwareRenderer$GlRenderer.checkCurrent(HardwareRenderer.java:1343)
    at android.view.HardwareRenderer$GlRenderer.validate(HardwareRenderer.java:1080)
    at android.view.TextureView.getBitmap(TextureView.java:587)
    at com.example.zmc.colorfultextproject.demo2.SecondActivity.getFrame(SecondActivity.java:153)
    at com.meitu.colorfultext.ColorfulTextView$1.run(ColorfulTextView.java:131)
    at java.lang.Thread.run(Thread.java:856)

in my code

private TextureView demo2VideoTtv;

public Bitmap getFrame() {
    //这里会造成抖动
    long start = System.currentTimeMillis();
    demo2VideoTtv.getBitmap(mFrame); // here throw exception
    Log.d("SecondActivity", "getFrame time" + (System.currentTimeMillis() - start));
    //裁剪图片
    mFrame_part = Bitmap.createBitmap(mFrame, demo2ColorfulText.getLeft(), demo2ColorfulText.getTop(), demo2ColorfulText.getWidth(), demo2ColorfulText.getHeight());
    return mFrame_part;
}

Solution

  • I'm not sure of the exact api threshold but on older devices TextureView.getBitmap() must be requested on the UI thread.

    It doesn't appear to be an issue on 21+. Last I saw it was reproducible on api 19.