У меня ошибка при запуске службы камеры из созданной мной службы определения местоположения, ошибка не отображается при запуске службы из основного действия.
Это происходит при захвате успеха, который показывает, что "приложение пропускает NullПоверхность ": Успех
Ошибка не отображается app " app pass Null Surface ": Ошибка
Этот код:
Основная активность:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@BindView(R.id.ButtonUpload) Button btnupload;
@BindView(R.id.ButtonService2) Button btnservice;
@BindView(R.id.ButtonService3) Button btnservice2;
@BindView(R.id.longitude) TextView tvlongitude;
@BindView(R.id.latitude) TextView tvlatitude;
@BindView(R.id.derajat) TextView tvderajat;
private static final int REQUEST_PERMISSIONS = 101;
private String degree;
boolean boolean_permission;
Double latitude,longitude;
Geocoder geocoder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
geocoder = new Geocoder(this, Locale.getDefault());
ButterKnife.bind(this);
btnupload.setOnClickListener(this);
btnservice.setOnClickListener(this);
btnservice2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.ButtonUpload:
startService(new Intent(MainActivity.this, CameraService.class));
break;
case R.id.ButtonService2:
if ((ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
&& (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)
&& (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED))
{
Intent intent = new Intent(getApplicationContext(), Location.class);
Toast.makeText(getApplicationContext(),"Service Berjalan",Toast.LENGTH_SHORT).show();
startService(intent);
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSIONS);
}
break;
case R.id.ButtonService3:
Intent location = new Intent(getApplicationContext(), Location.class);
stopService(location);
break;
}
}
Обслуживание камеры:
public class CameraService extends HiddenCameraService {
Bitmap bitmap;
String encodedImage;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)== PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED) {
if (HiddenCameraUtils.canOverDrawOtherApps(this)) {
CameraConfig cameraConfig = new CameraConfig()
.getBuilder(this)
.setCameraFacing(CameraFacing.REAR_FACING_CAMERA)
.setCameraResolution(CameraResolution.MEDIUM_RESOLUTION)
.setImageFormat(CameraImageFormat.FORMAT_JPEG)
.setCameraFocus(CameraFocus.AUTO)
.build();
startCamera(cameraConfig);
new android.os.Handler().postDelayed(new Runnable() {
@Override
public void run() {
Log.d("Camera","Capturing Camera");
takePicture();
}
}, 2000);
} else {
//Open settings to grant permission for "Draw other apps".
HiddenCameraUtils.openDrawOverPermissionSetting(this);
}
} else {
//TODO Ask your parent activity for providing runtime permission
Log.e("Camera", "Permission Not Available");
}
return START_NOT_STICKY;
}
@Override
public void onImageCapture(@NonNull File imageFile) {
Uri filepath = getImageContentUri(getApplicationContext(),imageFile);
try {
InputStream inputStream = getContentResolver().openInputStream(Objects.requireNonNull(filepath));
bitmap = BitmapFactory.decodeStream(inputStream);
}catch (Exception e){
e.printStackTrace();
}
StringRequest stringRequest = new StringRequest(Request.Method.POST, urlUpload, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getApplicationContext(), "Succes : " + response, Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Error : " + error.toString(), Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
String imageData = imageToString(bitmap);
params.put("image",imageData);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
stopSelf();
}
private String imageToString(Bitmap bitmap){
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,100, outputStream);
byte[] imageBytes = outputStream.toByteArray();
encodedImage = Base64.encodeToString(imageBytes,Base64.DEFAULT);
return encodedImage;
}
public static Uri getImageContentUri(Context context, File imageFile) {
String filePath = imageFile.getAbsolutePath();
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Images.Media._ID },
MediaStore.Images.Media.DATA + "=? ",
new String[] { filePath }, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
cursor.close();
return Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + id);
} else {
if (imageFile.exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
return context.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}
}
@Override
public void onCameraError(@CameraError.CameraErrorCodes int errorCode) {
switch (errorCode) {
case CameraError.ERROR_CAMERA_OPEN_FAILED:
//Camera open failed. Probably because another application
//is using the camera
Toast.makeText(this, "Error cannot open", Toast.LENGTH_LONG).show();
break;
case CameraError.ERROR_IMAGE_WRITE_FAILED:
//Image write failed. Please check if you have provided WRITE_EXTERNAL_STORAGE permission
Toast.makeText(this, "Error cannot write", Toast.LENGTH_LONG).show();
break;
case CameraError.ERROR_CAMERA_PERMISSION_NOT_AVAILABLE:
//camera permission is not available
//Ask for the camera permission before initializing it.
Toast.makeText(this, "Error cannot get Permission", Toast.LENGTH_LONG).show();
break;
case CameraError.ERROR_DOES_NOT_HAVE_OVERDRAW_PERMISSION:
//Display information dialog to the user with steps to grant "Draw over other app"
//permission for the app.
HiddenCameraUtils.openDrawOverPermissionSetting(this);
break;
case CameraError.ERROR_DOES_NOT_HAVE_FRONT_CAMERA:
Toast.makeText(this, "Error no camera", Toast.LENGTH_LONG).show();
break;
}
stopSelf();
}
}
Служба определения местоположения, которую я хочу вызвать.