尝试显示位图时应用崩溃

时间:2018-10-18 20:16:46

标签: android android-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$2$1$1.invoke(FotoapparatBuilder.kt:137)
        at io.fotoapparat.FotoapparatBuilder$frameProcessor$2$1$1.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$1.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));

        }
    }
}

谢谢。

3 个答案:

答案 0 :(得分:0)

  

使用这种方式

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

答案 1 :(得分:0)

您可以尝试

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

这可能有效

答案 2 :(得分:0)

它正在使用以下代码:

<form action="hello.php" class="js-ajax-php-json" method="post" accept-charset="utf-8">
    <input type="text" name="username" value="" placeholder="username" />
    <input type="text" name="password" value="" placeholder="password" />
    <input type="submit" name="submit" value="Submit form"  />
</form>

但是,当我在执行此操作之前,该应用再次崩溃:

$(".js-ajax-php-json").submit(function(){
    var data = {
    "action": "test"
    };
    data = $(this).serialize() + "&" + $.param(data);
    $.ajax({
    type: "POST",
    dataType: "json",
    url: "hello.php",
    data: data,
    success: function(data) {
        // manipulate response data
        // $(".the-return").html(
        // "First Value: " + data["username"] + "<br />Second Value: " + data["password"] + "<br />JSON: " + data["json"]
        // );

        alert("Form submitted successfully.\nReturned json: " + data["json"]);
    }
    });
    return false;
});

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

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