This commit is contained in:
kun1h 2024-02-04 17:48:53 +08:00
parent dcb98da660
commit a90ef44f6c
2 changed files with 46 additions and 41 deletions

View File

@ -7,10 +7,8 @@
<uses-feature android:name="android.hardware.camera.any" /> <uses-feature android:name="android.hardware.camera.any" />
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
android:maxSdkVersion="28" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application <application

View File

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