位图:错误,无法在此处访问 invalid/free 的位图

Bitmap: Error, cannot access an invalid/free'd bitmap here

我正在从 FrameLayout 获取位图。在frame布局中,增加一张gif显示和一张背景图。

但是每次保存视频都会出现这个错误。

不知道怎么解决的。我也尝试回收位图,但仍然没有解决错误。

Bitmap: Error, cannot access an invalid/free'd bitmap here!
A/vdostatus.make: runtime.cc:669] Runtime aborting...
    runtime.cc:669] All threads:
    runtime.cc:669] DALVIK THREADS (46):
    runtime.cc:669] "main" prio=10 tid=1 Runnable
    runtime.cc:669]   | group="" sCount=0 ucsCount=0 flags=0 obj=0x72285258 self=0x72f4e2014010
    runtime.cc:669]   | sysTid=30680 nice=-10 cgrp=top-app sched=0/0 handle=0x72f61713c4f8
    runtime.cc:669]   | state=R schedstat=( 59186976875 22687601168 43639 ) utm=2111 stm=3806 core=0 HZ=100
    runtime.cc:669]   | stack=0x7ffc4e974000-0x7ffc4e976000 stackSize=8188KB
    runtime.cc:669]   | held mutexes= "abort lock" "mutator lock"(shared held)
    runtime.cc:669]   native: #00 pc 000000000073cd7f  /apex/com.android.art/lib64/libart.so (art::DumpNativeStack(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, int, BacktraceMap*, char const*, art::ArtMethod*, void*, bool)+127)
    runtime.cc:669]   native: #01 pc 0000000000882570  /apex/com.android.art/lib64/libart.so (art::Thread::DumpStack(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, bool, BacktraceMap*, bool) const+368)
    runtime.cc:669]   native: #02 pc 00000000008a32fa  /apex/com.android.art/lib64/libart.so (art::DumpCheckpoint::Run(art::Thread*)+1082)
    runtime.cc:669]   native: #03 pc 000000000089c1ac  /apex/com.android.art/lib64/libart.so (art::ThreadList::RunCheckpoint(art::Closure*, art::Closure*)+220)
    runtime.cc:669]   native: #04 pc 000000000089b21d  /apex/com.android.art/lib64/libart.so (art::ThreadList::Dump(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, bool)+1213)
    runtime.cc:669]   native: #05 pc 000000000082271f  /apex/com.android.art/lib64/libart.so (art::Runtime::Abort(char const*)+2319)
    runtime.cc:669]   native: #06 pc 00000000000194ac  /apex/com.android.art/lib64/libbase.so (android::base::SetAborter(std::__1::function<void (char const*)>&&)::$_3::__invoke(char const*)+60)
    runtime.cc:669]   native: #07 pc 00000000000075f2  /system/lib64/liblog.so (__android_log_assert+370)
    runtime.cc:669]   native: #08 pc 00000000005ffb76  /system/lib64/libhwui.so (android::bitmap::toBitmap(long)+38)
    runtime.cc:669]   native: #09 pc 00000000005ffcb5  /system/lib64/libhwui.so (android::CanvasJNI::setBitmap(_JNIEnv*, _jobject*, long, long)+69)
    runtime.cc:669]   at android.graphics.Canvas.nSetBitmap(Native method)
    runtime.cc:669]   at android.graphics.Canvas.setBitmap(Canvas.java:182)
    runtime.cc:669]   at android.view.View.buildDrawingCacheImpl(View.java:21876)
    runtime.cc:669]   at android.view.View.buildDrawingCache(View.java:21776)
    runtime.cc:669]   at android.view.View.getDrawingCache(View.java:21612)
    runtime.cc:669]   at android.view.View.draw(View.java:22369)
    runtime.cc:669]   at android.view.ViewGroup.drawChild(ViewGroup.java:4528)
    runtime.cc:669]   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4289)
    runtime.cc:669]   at android.view.View.draw(View.java:22647)
    runtime.cc:669]   at android.view.View.draw(View.java:22517)
    runtime.cc:669]   at android.view.ViewGroup.drawChild(ViewGroup.java:4528)
    runtime.cc:669]   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4289)
    runtime.cc:669]   at android.view.View.draw(View.java:22515)
    runtime.cc:669]   at android.view.ViewGroup.drawChild(ViewGroup.java:4528)
    runtime.cc:669]   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4289)
    runtime.cc:669]   at android.view.View.draw(View.java:22515)
    runtime.cc:669]   at android.view.ViewGroup.drawChild(ViewGroup.java:4528)
    runtime.cc:669]   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4289)
    runtime.cc:669]   at android.view.View.draw(View.java:22515)
    runtime.cc:669]   at android.view.ViewGroup.drawChild(ViewGroup.java:4528)
    runtime.cc:669]   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4289)
    runtime.cc:669]   at android.view.View.draw(View.java:22647)
    runtime.cc:669]   at android.view.View.draw(View.java:22517)
    runtime.cc:669]   at android.view.ViewGroup.drawChild(ViewGroup.java:4528)
    runtime.cc:669]   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4289)

A/vdostatus.make: runtime.cc:669]   at android.graphics.Bitmap.recycle(Bitmap.java:366)
    runtime.cc:669]   at android.view.View.destroyDrawingCache(View.java:21643)
    runtime.cc:669]   at wa.vdostatus.maker.EditorActivity.getFrameBitmap(EditorActivity.java:3698)
    runtime.cc:669]   at wa.vdostatus.maker.EditorActivity.save(EditorActivity.java:3647)
    runtime.cc:669]   at wa.vdostatus.maker.EditorActivity.access900(EditorActivity.java:164)
    runtime.cc:669]   at wa.vdostatus.maker.EditorActivity$saveImage.doInBackground(EditorActivity.java:6186)
    runtime.cc:669]   at android.os.AsyncTask.call(AsyncTask.java:394)
    runtime.cc:669]   at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    runtime.cc:669]   at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:305)
    runtime.cc:669]   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    runtime.cc:669]   at +313)

A/libc: Fatal signal 6 (SIGABRT), code -6 (SI_TKILL) in tid 30680 (vdostatus.maker), pid 30680 (vdostatus.maker)

我的代码得到的是FrameLayout的位图吗

private Bitmap getFrameBitmap() {
        Bitmap bm = null;
        flEditor.postInvalidate();
        flEditor.setDrawingCacheEnabled(true);
        flEditor.buildDrawingCache();
        bm = Bitmap.createBitmap(flEditor.getDrawingCache());
        flEditor.destroyDrawingCache();
        bm = Utils.TrimBitmap(bm);
        bm = Utils.getResizedBitmap(bm, MyApplication.VIDEO_WIDTH, MyApplication.VIDEO_HEIGHT);
        return bm.copy(bm.getConfig(), false);
}

复制位图进行创建和操作。

destroyDrawingCache();方法被调用时,生成的位图将被回收(销毁)。

之后我们可以使用该位图的副本进行后续处理。

private Bitmap getFrameBitmap() {
        Bitmap bm = null;
        flEditor.postInvalidate();
        flEditor.setDrawingCacheEnabled(true);
        flEditor.buildDrawingCache();
        bm = Bitmap.createBitmap(flEditor.getDrawingCache());
        Bitmap bmc = bm.copy(bm.getConfig(), true); //check with both parameter, true and false
        flEditor.destroyDrawingCache();
        bmc = Utils.TrimBitmap(bmc);
        bmc = Utils.getResizedBitmap(bmc, MyApplication.VIDEO_WIDTH, MyApplication.VIDEO_HEIGHT);
        return bm.copy(bmc.getConfig(), false);
    }