尝试显示位图时应用程序崩溃

App crashing when trying to display Bitmap

我的应用程序每次都在这一行崩溃

imageView.setImageBitmap(bitmap);

我绝对确定它来自那条线。这是我得到的错误:

10-18 22:29:31.341 20037-20144/org.anthe.robocup11 E/AndroidRuntime: FATAL EXCEPTION: pool-4-thread-1
    Process: org.anthe.robocup11, PID: 20037
    android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
        at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6603)
        at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:919)
        at android.view.View.requestLayout(View.java:18738)
        at android.view.View.requestLayout(View.java:18738)
        at android.view.View.requestLayout(View.java:18738)
        at android.view.View.requestLayout(View.java:18738)
        at android.view.View.requestLayout(View.java:18738)
        at android.view.View.requestLayout(View.java:18738)
        at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:360)
        at android.view.View.requestLayout(View.java:18738)
        at android.widget.ImageView.setImageDrawable(ImageView.java:488)
        at android.support.v7.widget.AppCompatImageView.setImageDrawable(AppCompatImageView.java:99)
        at android.widget.ImageView.setImageBitmap(ImageView.java:618)
        at android.support.v7.widget.AppCompatImageView.setImageBitmap(AppCompatImageView.java:107)
        at org.anthe.robocup11.MainActivity$SampleFrameProcessor.process(MainActivity.java:109)
        at io.fotoapparat.FotoapparatBuilder$frameProcessor.invoke(FotoapparatBuilder.kt:137)
        at io.fotoapparat.FotoapparatBuilder$frameProcessor.invoke(FotoapparatBuilder.kt:21)
        at io.fotoapparat.preview.PreviewStream.dispatchFrame(PreviewStream.kt:107)
        at io.fotoapparat.preview.PreviewStream.access$dispatchFrame(PreviewStream.kt:16)
        at io.fotoapparat.preview.PreviewStream$dispatchFrameOnBackgroundThread.run(PreviewStream.kt:92)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at java.lang.Thread.run(Thread.java:818)
0-18 22:05:53.973 19167-19178/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] dequeueBuffer: BufferQueue has been abandoned
10-18 22:05:53.986 19167-19280/org.anthe.robocup11 E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb9af6630
10-18 22:05:54.006 19167-19178/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] queueBuffer: BufferQueue has been abandoned
10-18 22:05:54.007 19167-19179/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] dequeueBuffer: BufferQueue has been abandoned
10-18 22:05:54.032 19167-19225/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] queueBuffer: BufferQueue has been abandoned
10-18 22:05:54.032 19167-19178/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] dequeueBuffer: BufferQueue has been abandoned
10-18 22:05:54.065 19167-19179/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] queueBuffer: BufferQueue has been abandoned
10-18 22:05:54.066 19167-19225/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] dequeueBuffer: BufferQueue has been abandoned
10-18 22:05:54.099 19167-19178/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] queueBuffer: BufferQueue has been abandoned
10-18 22:05:54.099 19167-19179/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] dequeueBuffer: BufferQueue has been abandoned
10-18 22:05:54.129 19167-19225/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] queueBuffer: BufferQueue has been abandoned
10-18 22:05:54.129 19167-19178/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] dequeueBuffer: BufferQueue has been abandoned
10-18 22:05:54.164 19167-19179/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] queueBuffer: BufferQueue has been abandoned
10-18 22:05:54.165 19167-19225/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] dequeueBuffer: BufferQueue has been abandoned
10-18 22:05:54.197 19167-19179/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] queueBuffer: BufferQueue has been abandoned
10-18 22:05:54.198 19167-19225/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] dequeueBuffer: BufferQueue has been abandoned
10-18 22:05:54.229 19167-19178/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] queueBuffer: BufferQueue has been abandoned
10-18 22:05:54.229 19167-19179/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] dequeueBuffer: BufferQueue has been abandoned
10-18 22:05:54.264 19167-19225/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] queueBuffer: BufferQueue has been abandoned
10-18 22:05:54.264 19167-19178/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] dequeueBuffer: BufferQueue has been abandoned
10-18 22:05:54.338 19167-19178/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] cancelBuffer: BufferQueue has been abandoned
10-18 22:05:54.338 19167-19179/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] cancelBuffer: BufferQueue has been abandoned
10-18 22:05:54.338 19167-19225/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] cancelBuffer: BufferQueue has been abandoned
10-18 22:05:54.339 19167-19178/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] cancelBuffer: BufferQueue has been abandoned
10-18 22:05:54.339 19167-19179/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] cancelBuffer: BufferQueue has been abandoned
10-18 22:05:54.339 19167-19225/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] cancelBuffer: BufferQueue has been abandoned
10-18 22:05:54.339 19167-19178/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] cancelBuffer: BufferQueue has been abandoned
10-18 22:05:54.340 19167-19179/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-19167-0] cancelBuffer: BufferQueue has been abandoned

这是我的代码:

public class MainActivity extends AppCompatActivity {

    private CameraView cameraView;
    private FocusView focusView;
    private ImageView imageView;

    private Fotoapparat fotoapparat;

    private static final String TAG = MainActivity.class.getSimpleName();


    private Fotoapparat createFotoapparat() {
        return Fotoapparat
                .with(this)
                .into(cameraView)
                .focusView(focusView)
                .previewScaleType(ScaleType.CenterCrop)
                .lensPosition(back())
                .frameProcessor(new SampleFrameProcessor())
                .logger(loggers(
                        logcat(),
                        fileLogger(this)
                ))
                .cameraErrorCallback(new CameraErrorListener() {
                    @Override
                    public void onError(@NotNull CameraException e) {
                        Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
                    }
                })
                .build();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cameraView = findViewById(R.id.cameraView);
        focusView = findViewById(R.id.focusView);
        imageView = findViewById(R.id.imageView);

        fotoapparat = createFotoapparat();
    }

    @Override
    protected void onStart() {
        super.onStart();
        fotoapparat.start();
    }

    @Override
    protected void onStop() {
        super.onStop();
        fotoapparat.stop();
    }

    private class SampleFrameProcessor implements FrameProcessor {
        @Override
        public void process(@NotNull Frame frame) {
            // Perform frame processing, if needed
            byte nv21bytearray[] = frame.getImage();
            Resolution size = frame.getSize();
            Integer height = size.height;
            Integer width = size.width;
            //Log.i(TAG, height.toString());

            YuvImage yuvImage = new YuvImage(nv21bytearray, ImageFormat.NV21, width, height, null);
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, os);
            byte[] jpegByteArray = os.toByteArray();
            Bitmap bitmap = BitmapFactory.decodeByteArray(jpegByteArray, 0, jpegByteArray.length);
            //imageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 512, 512, false));
            imageView.setImageBitmap(bitmap);
            //Log.i(TAG, getString(bitmap.getHeight()));
            //int size1 = bitmap.getPixel(20,20);
            //int rood = red(size1);
            //Log.i(TAG, String.valueOf(rood));

        }
    }
}

提前致谢。

use this way

runOnUiThread(new Runnable() { 
imageView.setImageBitmap(bitmap)
 };

你可以试试这个

MainActivity.runOnUithread(new runnable()
{
imageView.setImageBitmap(bitmap);
});

这可能有用

正在使用此代码:

runOnUiThread(new Runnable() {
            @Override
            public void run() {
                imageView.setImageBitmap(bitmap);
            }
        });

但是,当我之前这样做时,应用程序再次崩溃:

bitmap.setPixel(20, 20, Color.WHITE);

这是我遇到的错误(MainActivity.java:111 是那行代码):

10-19 07:27:23.522 25370-25483/org.anthe.robocup11 E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb9af65e8
10-19 07:27:24.430 25370-25549/org.anthe.robocup11 E/AndroidRuntime: FATAL EXCEPTION: pool-4-thread-1
    Process: org.anthe.robocup11, PID: 25370
    java.lang.IllegalStateException
        at android.graphics.Bitmap.setPixel(Bitmap.java:1490)
        at org.anthe.robocup11.MainActivity$SampleFrameProcessor$override.process(MainActivity.java:111)
        at org.anthe.robocup11.MainActivity$SampleFrameProcessor$override.access$dispatch(MainActivity.java)
        at org.anthe.robocup11.MainActivity$SampleFrameProcessor.process(MainActivity.java)
        at io.fotoapparat.FotoapparatBuilder$frameProcessor.invoke(FotoapparatBuilder.kt:137)
        at io.fotoapparat.FotoapparatBuilder$frameProcessor.invoke(FotoapparatBuilder.kt:21)
        at io.fotoapparat.preview.PreviewStream.dispatchFrame(PreviewStream.kt:107)
        at io.fotoapparat.preview.PreviewStream.access$dispatchFrame(PreviewStream.kt:16)
        at io.fotoapparat.preview.PreviewStream$dispatchFrameOnBackgroundThread.run(PreviewStream.kt:92)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at java.lang.Thread.run(Thread.java:818)
10-19 07:27:24.640 25370-25382/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] dequeueBuffer: BufferQueue has been abandoned
10-19 07:27:24.646 25370-25483/org.anthe.robocup11 E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb9be56c0
10-19 07:27:24.668 25370-25538/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] queueBuffer: BufferQueue has been abandoned
10-19 07:27:24.669 25370-25382/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] dequeueBuffer: BufferQueue has been abandoned
10-19 07:27:24.709 25370-25381/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] queueBuffer: BufferQueue has been abandoned
10-19 07:27:24.711 25370-25538/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] dequeueBuffer: BufferQueue has been abandoned
10-19 07:27:24.749 25370-25382/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] queueBuffer: BufferQueue has been abandoned
10-19 07:27:24.750 25370-25381/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] dequeueBuffer: BufferQueue has been abandoned
10-19 07:27:24.791 25370-25538/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] queueBuffer: BufferQueue has been abandoned
10-19 07:27:24.792 25370-25382/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] dequeueBuffer: BufferQueue has been abandoned
10-19 07:27:24.830 25370-25381/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] queueBuffer: BufferQueue has been abandoned
10-19 07:27:24.831 25370-25538/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] dequeueBuffer: BufferQueue has been abandoned
10-19 07:27:24.872 25370-25382/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] queueBuffer: BufferQueue has been abandoned
10-19 07:27:24.874 25370-25381/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] dequeueBuffer: BufferQueue has been abandoned
10-19 07:27:24.909 25370-25538/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] queueBuffer: BufferQueue has been abandoned
10-19 07:27:24.912 25370-25382/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] dequeueBuffer: BufferQueue has been abandoned
10-19 07:27:24.961 25370-25382/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] cancelBuffer: BufferQueue has been abandoned
10-19 07:27:24.962 25370-25381/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] cancelBuffer: BufferQueue has been abandoned
10-19 07:27:24.962 25370-25538/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] cancelBuffer: BufferQueue has been abandoned
10-19 07:27:24.962 25370-25382/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] cancelBuffer: BufferQueue has been abandoned
10-19 07:27:24.962 25370-25381/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] cancelBuffer: BufferQueue has been abandoned
10-19 07:27:24.963 25370-25538/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] cancelBuffer: BufferQueue has been abandoned
10-19 07:27:24.963 25370-25382/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] cancelBuffer: BufferQueue has been abandoned
10-19 07:27:24.963 25370-25381/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] cancelBuffer: BufferQueue has been abandoned
10-19 07:27:24.964 25370-25538/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] cancelBuffer: BufferQueue has been abandoned
10-19 07:27:24.964 25370-25382/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] cancelBuffer: BufferQueue has been abandoned
10-19 07:27:24.964 25370-25381/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] cancelBuffer: BufferQueue has been abandoned
10-19 07:27:24.964 25370-25538/org.anthe.robocup11 E/BufferQueueProducer: [SurfaceTexture-0-25370-1] cancelBuffer: BufferQueue has been abandoned