【问题标题】:Android Image Capture does not save fileAndroid Image Capture 不保存文件
【发布时间】:2018-03-07 23:46:18
【问题描述】:

我正在关注https://developer.android.com/training/camera/photobasics.html的示例

我让它工作了一段时间,然后开始增强代码,现在拍照后,它不再存储在文件系统中。

private fun dispatchTakePictureIntent() {
    val imageCaptureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
    val componentName = imageCaptureIntent.resolveActivity(packageManager)
    // Ensure that there's a camera activity to handle the intent
    if (componentName == null) {
        Log.e("Ztuph", "componentName == null, cannot takePictureIntent.resolveActivity(packageManager))")
    }
    else try {
        val photoFile = createImageFile()
        val authority = "com.ztuph.android.FileProvider"
        try {
            photoUri = FileProvider.getUriForFile(this, authority, photoFile)
            Log.d("Ztuph", "1 photoUri = $photoUri")
            imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri)
            startActivityForResult(imageCaptureIntent, RequestCode.IMAGE_CAPTURE.ordinal)
            Log.d("Ztuph", "startActivityForResult(imageCaptureIntent, RequestCode.IMAGE_CAPTURE.ordinal)")
        }
        catch (illegalArgumentException: IllegalArgumentException) {
            // TODO
        }
    }
    catch (ioException: IOException) {
        // TODO Error occurred while creating the File
        Log.e("Ztuph", "can't create file", ioException)
    }
}

override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {

    when (requestCode){
        RequestCode.IMAGE_CAPTURE.ordinal -> when (resultCode){
            Activity.RESULT_CANCELED -> {
                Log.d(TAG, "IMAGE_CAPTURE result canceled")
            }
            Activity.RESULT_OK -> {
                Log.d(TAG, "onActivityResult: requestCode = IMAGE_CAPTURE, resultCode = RESULT_OK")
                if (intent == null) {
                    Log.e(TAG, "intent == null")

                    val mediaScanIntent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
                    mediaScanIntent.data = photoUri
                    sendBroadcast(mediaScanIntent)
                    Log.d(TAG, "broadcast Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE) with $photoUri")
                }
                else {
                    if (photoUri == null) Log.e(TAG, "photoUri == null")
                    else Log.d(TAG, "2 photoUri = $photoUri")

                    val mediaScanIntent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
                    mediaScanIntent.data = photoUri
                    sendBroadcast(mediaScanIntent)
                    Log.d(TAG, "broadcast Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE) with $photoUri")
                }
            }
            else -> {
                Log.e(TAG, "Cannot handle resultCode = $resultCode")
            }
        }
        else -> {
            Log.e(TAG, "Cannot handle requestCode = $requestCode")
        }
    }
}

Logcat 中似乎没有任何内容表明存在问题。

它在模拟器和真实设备(Galaxy Note 8)上的运行方式也不同。在真实设备上,它会创建目录“SD Card > Android > data > com.ztuph.android > files > Pictures”,但没有任何内容存储在那里。此外,onActivityResult 中的参数意图为空。

在模拟 (Nexus 5X API 27) 上,我无法判断是否正在创建目录(文件应用程序太原始),但不再有文件。昨天,我的应用程序正在创建 image.jpg 文件,而今天不是。另外,在模拟器上,onActivityResult 中的参数intent 不为空。

我的清单看起来像

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ztuph.android">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="aia-compat-api-min-version"
            android:value="1" />
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.ztuph.android.FileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
    </application>

    <uses-feature android:name="android.hardware.camera" android:required="true" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

是否有人有任何故障排除提示或其他想法,为什么图像捕获会停止写入文件系统?

【问题讨论】:

    标签: android android-intent kotlin


    【解决方案1】:

    Logcat 中似乎没有任何内容表明存在问题。

    很可能确实如此,但错误可能来自相机应用,因为它无法写入您想要的位置。将FLAG_GRANT_WRITE_URI_PERMISSION 添加到您的Intent

    它在模拟器和真实设备(Galaxy Note 8)上的运行也不同。

    在大约 10,000 种 Android 设备型号中预装了数百个相机应用。用户可能会安装数百个其他相机应用程序。每当您调用第三方应用时,很容易得到不同的结果。

    另外,onActivityResult 中的参数意图为空。

    应该是null

    我无法判断目录是否正在创建(文件应用太原始)

    在 Android Studio 3.0+ 中使用设备文件资源管理器。

    【讨论】:

    • imageCaptureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION) 成功了 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多