在我的应用中(在模拟器中运行),我没有使用任何互联网连接(并且甚至没有获得许可),但是收到错误(搜索谷歌声称是因为没有发布互联网资源):
A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks. java.lang.Throwable: Explicit termination method 'close' not called
我的代码很简单:
public void onResume() {
super.onResume();
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) mCamera = Camera.open(0);
else mCamera = Camera.open();
}
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_crime_camera, container, false);
//Hide progress first
mProgressContainer = (View)v.findViewById(R.id.crime_camera_progressContainer);
mProgressContainer.setVisibility(View.INVISIBLE);
Button takePicture = (Button)v.findViewById(R.id.crime_camera_takePictureButton);
takePicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if ( mCamera != null) mCamera.takePicture(mShutterCallback, null,mPictureCallback );
}
});
mSurfaceView = (SurfaceView)v.findViewById(R.id.crime_camera_surfaceView);
final SurfaceHolder holder = mSurfaceView.getHolder();
//Required only for pre-s.0 devices
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try
{
//Tell camera this is where to draw preview
if ( mCamera != null) mCamera.setPreviewDisplay(holder);
}
catch (IOException e)
{
Log.e(TAG, "Error setting up preview display", e);
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int w, int h) {
if (mCamera == null ) return;
//Surface changed size, need to tell camera
Camera.Parameters parameters = mCamera.getParameters();
Camera.Size s = getBestSupportedSize( parameters.getSupportedPreviewSizes(), w, h );
parameters.setPreviewSize(s.width, s.height);
//Saving picture size
s = getBestSupportedSize(parameters.getSupportedPictureSizes(), w, h);
parameters.setPictureSize(s.width, s.height);
//Tell camera the params
mCamera.setParameters(parameters);
try
{
mCamera.startPreview();
}
catch (Exception e)
{
Log.e(TAG, "Could not start preview", e);
mCamera.release();
mCamera = null;
}
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
if (mCamera != null) mCamera.stopPreview();
}
});
return v;
}
在绘制片段时崩溃。为什么会这样?我如何解决它? (将我的计算机连接到互联网修复了错误,但我需要能够在没有互联网连接的情况下编程/运行emuator。)
答案 0 :(得分:1)
您可以关注屏幕短片
1.打开Android虚拟设备管理器
2.编辑您的Android模拟器
3.设置Back Camera =(Emulated)如屏幕短显示。 4. cammera看起来像这样。 我希望它能帮助你摆脱这个问题
答案 1 :(得分:1)
将此内容包含在AndroidManifest文件中
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
另外,请将相机与stoppreview一起发布。您的错误主要是因为您尚未释放资源。