fix: captureScreen() return null

This commit is contained in:
hyb1996
2018-01-01 14:12:47 +08:00
parent 3e592370dc
commit 7145faa533

View File

@@ -13,6 +13,7 @@ import android.media.projection.MediaProjectionManager;
import android.os.Build; import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi; import android.support.annotation.RequiresApi;
import android.util.Log; import android.util.Log;
@@ -85,22 +86,20 @@ public class ScreenCapturer {
private void setImageListener(Handler handler) { private void setImageListener(Handler handler) {
mImageReader.setOnImageAvailableListener(reader -> { mImageReader.setOnImageAvailableListener(reader -> {
if (mCachedImage != null) { synchronized (mCachedImageLock) {
synchronized (mCachedImageLock) { if (mCachedImage != null) {
if (mCachedImage != null) { mCachedImage.close();
mCachedImage.close(); }
} mCachedImage = reader.acquireLatestImage();
mCachedImage = reader.acquireLatestImage(); if (mCachedImage != null) {
mImageAvailable = true; mImageAvailable = true;
mCachedImageLock.notify(); mCachedImageLock.notify();
return;
} }
} }
mCachedImage = reader.acquireLatestImage();
}, handler); }, handler);
} }
@Nullable @NonNull
public Image capture() { public Image capture() {
if (!mImageAvailable) { if (!mImageAvailable) {
waitForImageAvailable(); waitForImageAvailable();