-1

I have a function that takes pictures from my fragment in my activity. Here is how I ask for permissions in my fragment:

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
    }
}

Then the fragment shows what the camera is looking at to add 3d models in an AR enviroment. Here is how I save the pictures:

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)
}

}

private fun 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))

}

and finally here is my manifest:

    <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>

I keep getting in my logs

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)

What am I missing? I´m using Android 29, Java 1.8, Kotlin and AndroidX

Thanks in advance

Greetings

linker85
  • 1,601
  • 5
  • 26
  • 44

1 Answers1

2

I had the same issue while trying to read from external storage on my AVD 29 Pixel, having already requested and received permissions.

Check this answer courtesy of Uriel Frankel:

Google has a new feature on Android Q: filtered view for external storage. A quick fix for that is to add this code in the AndroidManifest.xml file

<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

Update: As discussed in the comments here is a link to relevant documentation on changes to come.

Developers Documentation: Manage scoped external storage access

Found in this SO-answer from Nikos Hidalgo.

java.io.FileNotFoundException: /sdcard/testwrite.txt: open failed: EACCES (Permission denied)

bitmadao
  • 31
  • 2
  • This works, but what will happen when Google removes this property for any app that targets Android Q and are trying to save the bitmap? Is there a better solution – linker85 Nov 19 '19 at 21:37
  • You are right, changes are coming in the next release. An answer courtesy of [Nikos Hidalgo](https://stackoverflow.com/users/10300673/nikos-hidalgo) has the same temp fix but also links to the relevant documentation for what's to come [Documentation: Manage scoped external storage access](https://developer.android.com/training/data-storage/files/external-scoped#top_of_page) [SO-Answer](https://stackoverflow.com/a/58936321/12382779) – bitmadao Nov 19 '19 at 22:22