根据要求,应用程序的用户单击设备摄像头中的图像,然后将其上传到服务器。当我在Lollipop及更高版本上测试我的APK时一切正常,但是当我在Lollipop以下(特别是在KitKat 4.4.4 API 19上)测试我的APK时,应用程序崩溃,但异常java.io.FileNotFoundException: /: open failed: EISDIR (Is a directory)
。
在将intent
传递到Camera
之前,我先创建一个File
并将其传递给Uri
。此外,在创建File
时,我还要保存文件路径在某些变量中。
稍后在onActivityResult()
上,我使用路径(保存在变量中)创建一个新文件,然后使用多部分改型将其传递到服务器。
这就是我将Intent
传递给Camera
Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
takePictureIntent.resolveActivity(packageManager)?.also {
val photoFile = try {
createImageFile()
} catch (e: Exception) {
null
}
Log.e("file tag", photoFile!!.absolutePath)
photoFile?.also {
val photoUri = FileProvider.getUriForFile(this@PrintActivity, "$packageName.fileprovider", it)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri)
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
takePictureIntent.clipData = ClipData.newRawUri("", photoUri)
takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
}
startActivityForResult(takePictureIntent, REQUEST_CAMERA)
}
}
}
createImageFile()
方法如下:
@Throws(IOException::class)
private fun createImageFile(): File {
val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(Date())
val storageDir = getExternalFilesDir("Allocation")
return File.createTempFile("JPEG_${timeStamp}_", ".jpg", storageDir).apply {
printImageFilePath = absolutePath
}
}
这是我提供的file_paths
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="Android/data/$packageName/files/Allocation" />
</paths>
这就是我在Provider
标签下的AndroidManifest
中声明application
的方式
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="$packageName.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
这是方法OnActivityResult()
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CAMERA && resultCode == Activity.RESULT_OK) {
try {
val printedImageFile = File(printImageFilePath)
doImageUpload(printedImageFile)
} catch (e: Throwable) {
e.stackTrace
}
}
}
这是我的doImageUpload()
方法
private fun doImageUpload(printedImageFile: File?) {
if (networkUtil.isConnected()) {
try {
launch(UI) {
val response = dataManager.uploadImage(printedImageFile)
Log.v("ImageUploadSuccess", response.body())
}catch(e:Exception){
}
}
这是我的dataManager.uploadImage(printedImageFile)
方法
suspend fun uploadImage(printImageFile: File?): Response<String> {
val printImageBody: RequestBody
val printImagePart: MultipartBody.Part
printImageBody =
RequestBody.create(MediaType.parse("multipart/formdata"),printImageFile)
printImagePart = MultipartBody.Part.createFormData("my_file1", printImageFile.name, printImageBody)
restService.uploadImage(printImagePart).await()
}
最后这是我改进的多部分接口方法,
@Multipart
@POST
fun uploadImage(@Part myFile2: MultipartBody.Part?): Deferred<Response<String>>
这是我收到的崩溃日志
E/AndroidRuntime: FATAL EXCEPTION: main
Process: $packageName, PID: 24339
java.io.FileNotFoundException: /: open failed: EISDIR (Is a directory)
at libcore.io.IoBridge.open(IoBridge.java:409)
at java.io.FileInputStream.<init>(FileInputStream.java:78)
at okio.Okio.source(Okio.java:168)
at okhttp3.RequestBody$3.writeTo(RequestBody.java:119)
at okhttp3.MultipartBody.writeOrCountBytes(MultipartBody.java:173)
at okhttp3.MultipartBody.writeTo(MultipartBody.java:114)
at com.readystatesoftware.chuck.ChuckInterceptor.intercept(ChuckInterceptor.java:154)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:254)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:200)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: libcore.io.ErrnoException: open failed: EISDIR (Is a directory)
at libcore.io.IoBridge.open(IoBridge.java:398)
at java.io.FileInputStream.<init>(FileInputStream.java:78)
at okio.Okio.source(Okio.java:168)
at okhttp3.RequestBody$3.writeTo(RequestBody.java:119)
at okhttp3.MultipartBody.writeOrCountBytes(MultipartBody.java:173)
at okhttp3.MultipartBody.writeTo(MultipartBody.java:114)
at com.readystatesoftware.chuck.ChuckInterceptor.intercept(ChuckInterceptor.java:154)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:254)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:200)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
答案 0 :(得分:0)
经过大量的反复试验,并在我的上级和this的帮助下,我终于解决了相应的问题。
问题是,当我习惯从后置摄像头捕获照片时,Android系统正在杀死我各自的Activity
类,以节省资源。当相机关闭并返回我的Activity
时,由于用于保存所捕获图像的文件路径的实例变量为空,正在重新创建Activity
。结果,该应用程序曾经崩溃。
因此,为了保留实例变量,我在onSaveInstanceState(outState: Bundle?)
类(触发了PrintActivity
的{{1}}类中使用了Activity
方法,如下: / p>
Camera Intent
然后在override fun onSaveInstanceState(outState: Bundle?) {
super.onSaveInstanceState(outState)
outState?.putString(Constants.PRINT_IMAGE_FILE_PATH,printImageFilePath)
// printImageFilePath is the path of the image file ,
// Constants.PRINT_IMAGE_FILE_PATH is my key for the bundle
}
内的PrintActivity
中,我像这样重新分配实例变量,
onCreate(savedInstanceState: Bundle?)