我正在研究一个项目,该项目应该拍照并将其保存在服务器上。但是过一会儿,我想重新安装该应用程序。它不会出现在屏幕上,也不会弹出。它只是没有,但终端说安装成功。
除了onCreate方法外,我还很害怕将所有内容都注释掉,并且它也不会安装应用程序
<code>
package com.example.testpicture;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CameraMetadata;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.media.ImageReader;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Size;
import android.util.SparseIntArray;
import android.view.Surface;
import android.view.TextureView;
import android.widget.Button;
import android.widget.Toast;
import java.util.Arrays;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class MainActivity extends AppCompatActivity {
private Button button;
private TextureView textureView;
private static final SparseIntArray orientation = new SparseIntArray();
private CameraDevice cameraDevice;
private String cameraID;
private Size dimension;
private CameraCaptureSession cameraCaptureSession;
private CaptureRequest captureRequest;
private CaptureRequest.Builder captureRequestBuilder;
private ImageReader imageReader;
private Handler mBackgroundHandler;
private HandlerThread handlerThread;
static {
orientation.append(Surface.ROTATION_0,90);
orientation.append(Surface.ROTATION_90,0);
orientation.append(Surface.ROTATION_180,270);
orientation.append(Surface.ROTATION_270,180);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_picture);
textureView = (TextureView) findViewById(R.id.texture);
//textureView.setSurfaceTextureListener(textureListener);
//findViewById(R.id.takePic).setOnClickListener(v -> takePic());
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 101){
if(grantResults[0] == PackageManager.PERMISSION_DENIED){
Toast.makeText(getApplicationContext(),"Bitte zugriff gewähren", Toast.LENGTH_LONG).show();
//finish();
}
}
}
TextureView.SurfaceTextureListener textureListener = new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
try {
openCamera();
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
};
CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull CameraDevice camera) {
cameraDevice = camera;
try {
createCamerPreview();
}catch (CameraAccessException e){
e.printStackTrace();
}
}
@Override
public void onDisconnected(@NonNull CameraDevice camera) {
cameraDevice.close();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onError(@NonNull CameraDevice camera, int error) {
cameraDevice.close();
cameraDevice = null;
}
};
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void createCamerPreview() throws CameraAccessException {
SurfaceTexture texture = textureView.getSurfaceTexture();
texture.setDefaultBufferSize(dimension.getWidth(),dimension.getHeight());
Surface surface = new Surface(texture);
captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
captureRequestBuilder.addTarget(surface);
cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
if (cameraDevice == null) {
return;
}
cameraCaptureSession = session;
try {
updatePreview();
}catch (CameraAccessException e){
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession session) {
Toast.makeText(getApplicationContext(), "Configuration Changed", Toast.LENGTH_LONG).show();
}
},null);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void updatePreview() throws CameraAccessException {
if(cameraDevice==null){
return;
}
captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
cameraCaptureSession.setRepeatingRequest(captureRequestBuilder.build(),null,mBackgroundHandler);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void openCamera() throws CameraAccessException {
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
cameraID = manager.getCameraIdList()[0];
CameraCharacteristics cc = manager.getCameraCharacteristics(cameraID);
StreamConfigurationMap map = cc.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
dimension = map.getOutputSizes(SurfaceTexture.class)[0];
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},101);
return;
}
manager.openCamera(cameraID, stateCallback, null);
}
private void takePic() {
//CameraStateHandler csh = new CameraStateHandler(getActivity(),this);
}
@Override
protected void onResume() {
super.onResume();
startBackgroundThread();
if (textureView.isAvailable()){
try {
openCamera();
}catch (CameraAccessException e){
e.printStackTrace();
}
}else{
textureView.setSurfaceTextureListener(textureListener);
}
}
private void startBackgroundThread(){
handlerThread = new HandlerThread("Camera Background");
handlerThread.start();
mBackgroundHandler = new Handler(handlerThread.getLooper());
}
@Override
protected void onPause(){
try {
stopBackgroundThread();
}catch (InterruptedException e){
e.printStackTrace();
}
super.onPause();
}
protected void stopBackgroundThread() throws InterruptedException {
handlerThread.quitSafely();
handlerThread.join();
handlerThread = null;
mBackgroundHandler = null;
}
}
</code>