为什么我无法将位图另存为从Android的Fragment生成的图片?

问题描述 投票:0回答:1

我有一个功能可以从我的活动片段中拍摄照片。这是我在片段中请求权限的方式:

class WritingArFragment: ArFragment() {
    override fun getAdditionalPermissions(): Array<String?> {
        val additionalPermissions = super.getAdditionalPermissions()
        val permissionLength = additionalPermissions?.size ?: 0
        val permissions = arrayOfNulls<String>(permissionLength + 1)
        permissions[0] = Manifest.permission.WRITE_EXTERNAL_STORAGE
        if (permissionLength > 0) {
            System.arraycopy(additionalPermissions!!, 0, permissions, 1, additionalPermissions.size)
        }
        return permissions
    }
}

然后,该片段显示了相机正在考虑在AR环境中添加3d模型的内容。这是我保存图片的方式:

private fun generateFilename(): String {
    val date = SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault()).format(Date())
    return Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES).toString() + File.separator + "Sceneform/" + date + "_screenshot.jpg"
}

@Throws(IOException::class)
private fun saveBitmapToDisk(bitmap: Bitmap, filename: String) {
val out = File(filename)
if (!out.parentFile.exists()) {
    out.parentFile.mkdirs()
}
try {
    FileOutputStream(filename).use { outputStream ->
        ByteArrayOutputStream().use { outputData ->
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputData)
            outputData.writeTo(outputStream)
            outputStream.flush()
            outputStream.close()
        }
    }
} catch (ex: IOException) {
    throw IOException("Failed to save bitmap to disk", ex)
}

}

[私人乐趣takePicture(){val filename = generateFilename()val view = arFragment.arSceneView

// Create a bitmap the size of the scene view.
val bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
        Bitmap.Config.ARGB_8888)

// Create a handler thread to offload the processing of the image.
val handlerThread = HandlerThread("PixelCopier")
handlerThread.start()
// Make the request to copy.
PixelCopy.request(view, bitmap, { copyResult ->
    if (copyResult == PixelCopy.SUCCESS) {
        try {
            saveBitmapToDisk(bitmap, filename)
            val snackbar = Snackbar.make(findViewById(android.R.id.content),
                    "Photo saved", Snackbar.LENGTH_LONG)
            snackbar.setAction("Open in Photos") { v ->
                val photoFile = File(filename)

                val photoURI = FileProvider.getUriForFile(this.getApplication(),
                        "$packageName.example.secondar.name.provider",
                        photoFile)
                val intent = Intent(Intent.ACTION_VIEW, photoURI)
                intent.setDataAndType(photoURI, "image/*")
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                startActivity(intent)
            }
            snackbar.show()
        } catch (e: IOException) {
            e.printStackTrace()
            val toast = Toast.makeText(this.getApplication(), e.toString(),
                    Toast.LENGTH_LONG)
            toast.show()
        }
    } else {
        val toast = Toast.makeText(this.getApplication(),
                "Failed to copyPixels: $copyResult", Toast.LENGTH_LONG)
        toast.show()
    }
    handlerThread.quitSafely()
}, Handler(handlerThread.looper))

}

最后是我的清单:

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:usesCleartextTraffic="true"
    android:theme="@style/AppTheme">
    <meta-data android:name="com.google.ar.core" android:value="required" />
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="${applicationId}.example.secondar.name.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/paths"/>
    </provider>
</application>

我一直在记录日志

2019-11-19 14:05:04.503 5383-5553/com.example.secondar W/System.err: java.io.IOException: Failed to save bitmap to disk
2019-11-19 14:05:04.503 5383-5553/com.example.secondar W/System.err:     at com.example.secondar.MainActivity.saveBitmapToDisk(MainActivity.kt:238)
2019-11-19 14:05:04.504 5383-5553/com.example.secondar W/System.err:     at com.example.secondar.MainActivity.access$saveBitmapToDisk(MainActivity.kt:45)
2019-11-19 14:05:04.504 5383-5553/com.example.secondar W/System.err:     at com.example.secondar.MainActivity$takePicture$1.onPixelCopyFinished(MainActivity.kt:258)
2019-11-19 14:05:04.504 5383-5553/com.example.secondar W/System.err:     at android.view.PixelCopy$1.run(PixelCopy.java:191)
2019-11-19 14:05:04.504 5383-5553/com.example.secondar W/System.err:     at android.os.Handler.handleCallback(Handler.java:883)
2019-11-19 14:05:04.504 5383-5553/com.example.secondar W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:100)
2019-11-19 14:05:04.504 5383-5553/com.example.secondar W/System.err:     at android.os.Looper.loop(Looper.java:214)
2019-11-19 14:05:04.504 5383-5553/com.example.secondar W/System.err:     at android.os.HandlerThread.run(HandlerThread.java:67)
2019-11-19 14:05:04.504 5383-5553/com.example.secondar W/System.err: Caused by: java.io.FileNotFoundException: /storage/emulated/0/Pictures/Sceneform/20191119140504_screenshot.jpg: open failed: EACCES (Permission denied)
2019-11-19 14:05:04.505 5383-5553/com.example.secondar W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:496)
2019-11-19 14:05:04.505 5383-5553/com.example.secondar W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:235)
2019-11-19 14:05:04.505 5383-5553/com.example.secondar W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:125)
2019-11-19 14:05:04.505 5383-5553/com.example.secondar W/System.err:     at com.example.secondar.MainActivity.saveBitmapToDisk(MainActivity.kt:229)
2019-11-19 14:05:04.505 5383-5553/com.example.secondar W/System.err:    ... 7 more
2019-11-19 14:05:04.505 5383-5553/com.example.secondar W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)

我想念的是什么?我正在使用Android 29,Java 1.8,Kotlin和AndroidX

提前感谢

问候

android kotlin androidx arcore sceneform
1个回答
0
投票

尝试并已经获得许可并尝试从我的AVD 29 Pixel上的外部存储读取数据时,我遇到了同样的问题。

Uriel Frankel确认此答案:

Google在Android Q:外部存储的过滤视图中具有一项新功能。快速解决方案是将此代码添加到AndroidManifest.xml文件中

<manifest ... >
    <!-- This attribute is "false" by default on apps targeting Android Q. -->
    <application android:requestLegacyExternalStorage="true" ... >
     ...
    </application>
</manifest>

Exception 'open failed: EACCES (Permission denied)' on Android

© www.soinside.com 2019 - 2024. All rights reserved.