fix bug
This commit is contained in:
parent
dcb98da660
commit
a90ef44f6c
|
@ -7,10 +7,8 @@
|
|||
<uses-feature android:name="android.hardware.camera.any" />
|
||||
<uses-permission android:name="android.permission.CAMERA" />
|
||||
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
|
||||
android:maxSdkVersion="28" />
|
||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
|
||||
android:maxSdkVersion="28" />
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
|
||||
<application
|
||||
|
|
|
@ -13,6 +13,7 @@ import android.location.LocationListener
|
|||
import android.location.LocationManager
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.os.Looper
|
||||
import android.provider.MediaStore
|
||||
import android.util.Log
|
||||
import android.widget.Button
|
||||
|
@ -39,12 +40,9 @@ import okhttp3.OkHttpClient
|
|||
import okhttp3.Request
|
||||
import okhttp3.RequestBody.Companion.asRequestBody
|
||||
import okhttp3.Response
|
||||
import java.io.BufferedInputStream
|
||||
import java.io.BufferedOutputStream
|
||||
import okio.IOException as OKIOException
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Locale
|
||||
|
@ -60,12 +58,11 @@ class MainActivity : AppCompatActivity() {
|
|||
private var imageCapture: ImageCapture? = null
|
||||
private lateinit var planText: EditText
|
||||
private var isMonitoring = false
|
||||
private var photoFilePath: String? = null
|
||||
private var locationManager: LocationManager? = null
|
||||
private lateinit var lat: String
|
||||
private lateinit var lon: String
|
||||
private lateinit var currentImg:String
|
||||
private lateinit var context:Context
|
||||
private lateinit var timer: Timer
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
|
@ -88,54 +85,65 @@ class MainActivity : AppCompatActivity() {
|
|||
startMonitoring()
|
||||
}
|
||||
}
|
||||
checkPermissions()
|
||||
if (!checkPermissions()) {
|
||||
Log.e(TAG, "no permission")
|
||||
return
|
||||
}
|
||||
getLocation()
|
||||
locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
||||
}
|
||||
|
||||
private fun updateWayCallback(file: File):Callback{
|
||||
return object:Callback{
|
||||
override fun onFailure(call: Call, e: IOException) {
|
||||
override fun onFailure(call: Call, e: OKIOException) {
|
||||
file.delete()
|
||||
println("Fail")
|
||||
Log.d(TAG, "send way to server failed ${e}")
|
||||
}
|
||||
|
||||
override fun onResponse(call: Call, response: Response) {
|
||||
println(response.body.toString())
|
||||
file.delete()
|
||||
with(response) {
|
||||
|
||||
val s = body?.byteString()
|
||||
Log.d(TAG, "send way to server succeed ${s}")
|
||||
body?.close()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
private fun updateWay(img:InputStream){
|
||||
Log.d(TAG, "$img.jpg")
|
||||
val client = OkHttpClient()
|
||||
|
||||
var builder: MultipartBody.Builder = MultipartBody.Builder()
|
||||
.setType(MultipartBody.FORM)
|
||||
.addFormDataPart("lat", lat)
|
||||
.addFormDataPart("lon", lon)
|
||||
val dst = File("${context.externalCacheDir}/tmp")
|
||||
copyfile(img, dst )
|
||||
val n = copyFile(img, dst )
|
||||
if(n == 0L) {
|
||||
return
|
||||
}
|
||||
Log.d(TAG, "copy photo return ${n}")
|
||||
val requestFile = dst.asRequestBody(MEDIA_TYPE_FILE)
|
||||
builder = builder.addFormDataPart("file", "1.jpg", requestFile)
|
||||
val multipartBody: MultipartBody = builder.build()
|
||||
val url = "http://travebing.bing89.com/api/v1/plans/" + planText.text + "/ways"
|
||||
Log.d(TAG, "send way to ${url}")
|
||||
val request = Request.Builder()
|
||||
.url("http://travebing.bing89.com/api/v1/plans/" + planText.text + "/ways")
|
||||
.url(url)
|
||||
.post(multipartBody)
|
||||
.build()
|
||||
|
||||
val call: Call = client.newCall(request)
|
||||
call.enqueue(updateWayCallback(dst))
|
||||
}
|
||||
private fun startMonitoring() {
|
||||
isMonitoring = true
|
||||
startButton.text = "休息"
|
||||
val timer = Timer()
|
||||
timer = Timer()
|
||||
val task = object : TimerTask() {
|
||||
override fun run() {
|
||||
// 每隔 1 秒钟执行一次此方法
|
||||
// 在这里写下你需要执行的代码
|
||||
// getLocation()
|
||||
takePhoto()
|
||||
}
|
||||
}
|
||||
|
@ -148,8 +156,10 @@ class MainActivity : AppCompatActivity() {
|
|||
private fun stopMonitoring() {
|
||||
isMonitoring = false
|
||||
startButton.text = "出发"
|
||||
timer.cancel()
|
||||
}
|
||||
|
||||
@Deprecated("Deprecated in Java")
|
||||
@RequiresPermission(
|
||||
anyOf = [permission.ACCESS_COARSE_LOCATION,
|
||||
permission.ACCESS_FINE_LOCATION,
|
||||
|
@ -181,7 +191,6 @@ class MainActivity : AppCompatActivity() {
|
|||
permission.CAMERA,
|
||||
permission.WRITE_EXTERNAL_STORAGE,
|
||||
permission.READ_EXTERNAL_STORAGE,
|
||||
permission.ACCESS_FINE_LOCATION
|
||||
)
|
||||
|
||||
for (permission in requiredPermissions) {
|
||||
|
@ -195,7 +204,7 @@ class MainActivity : AppCompatActivity() {
|
|||
requiredPermissions,
|
||||
PERMISSION_REQUEST_CODE
|
||||
)
|
||||
return false
|
||||
// return false
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -217,18 +226,6 @@ class MainActivity : AppCompatActivity() {
|
|||
|
||||
private fun getLocation() {
|
||||
val locMan = getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
||||
val checkCameraPermission =
|
||||
ContextCompat.checkSelfPermission(this, permission.ACCESS_FINE_LOCATION)
|
||||
val checkCallPhonePermission =
|
||||
ContextCompat.checkSelfPermission(this, permission.ACCESS_COARSE_LOCATION)
|
||||
if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED || checkCameraPermission != PackageManager.PERMISSION_GRANTED) {
|
||||
ActivityCompat.requestPermissions(
|
||||
this, arrayOf(
|
||||
permission.ACCESS_COARSE_LOCATION,
|
||||
permission.ACCESS_FINE_LOCATION
|
||||
), 22
|
||||
)
|
||||
}
|
||||
try {
|
||||
// Request location updates
|
||||
val providers = locMan.getProviders(false)
|
||||
|
@ -242,7 +239,9 @@ class MainActivity : AppCompatActivity() {
|
|||
provider = LocationManager.PASSIVE_PROVIDER
|
||||
}
|
||||
}
|
||||
// Looper.prepare()
|
||||
Toast.makeText(baseContext, "使用" + provider + "定位", Toast.LENGTH_SHORT).show()
|
||||
// Looper.loop()
|
||||
Log.d(TAG, "==================>>>> get location from gps")
|
||||
locMan.requestLocationUpdates(provider, 10000L, 100f, locationListener)
|
||||
} catch (ex: SecurityException) {
|
||||
|
@ -296,13 +295,19 @@ class MainActivity : AppCompatActivity() {
|
|||
/**
|
||||
* 单个文件复制
|
||||
*/
|
||||
fun copyfile(srcFile: InputStream, destFile: File) {
|
||||
private fun copyFile(srcFile: InputStream, destFile: File):Long {
|
||||
val fos = FileOutputStream(destFile)
|
||||
srcFile.copyTo(fos)
|
||||
val n = srcFile.copyTo(fos)
|
||||
try {
|
||||
fos.close()
|
||||
srcFile.close()
|
||||
}catch (e: java.io.IOException){
|
||||
Log.d(TAG, "================>>>>> copy failed")
|
||||
}
|
||||
|
||||
|
||||
return n
|
||||
}
|
||||
private fun takePhoto(): String {
|
||||
// Get a stable reference of the modifiable image capture use case
|
||||
val imageCapture = imageCapture ?: return ""
|
||||
|
@ -338,7 +343,9 @@ class MainActivity : AppCompatActivity() {
|
|||
override fun onImageSaved(output: ImageCapture.OutputFileResults) {
|
||||
previewImageView.setImageURI(output.savedUri)
|
||||
val msg = "--------- Photo capture succeeded: ${output.savedUri}"
|
||||
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
|
||||
// Looper.prepare()
|
||||
// Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
|
||||
// Looper.loop()
|
||||
Log.d(TAG, msg)
|
||||
val resolve = context.contentResolver
|
||||
val uri = Uri.withAppendedPath(output.savedUri, "")
|
||||
|
|
Loading…
Reference in New Issue