Rohit
Rohit

Reputation: 29

FileError {code: 6, message: “NO_MODIFICATION_ALLOWED_ERR”}

I am trying to remove a file from the local storage of my phone device. I have given the path and filename and both exist.

I ran file.checkDir & file.checkFile to confirm whether its getting the path and it returned true.

I tried it on multiple android devices and observed that its only getting deleted for android versions below 8. I am not aware of any plugin update for the higher android version if there is any. I tried to google it but there is nowhere mentioned regarding plugin update.

Its throwing this error:

FileError {code: 6, message: “NO_MODIFICATION_ALLOWED_ERR”}

Although I have mentioned the permissions:

android.permission.WRITE_EXTERNAL_STORAGE
android.permission.READ_EXTERNAL_STORAGE

I am not sure about what I am doing wrong here. Thanks for the help.

this.file.removeFile(path, fileName)

Expected - File should be removed from the given path

Actual - File not being removed from the given path

Upvotes: 1

Views: 1016

Answers (1)

From sdk21, if Im not mistaken, not enough to specify permission in manifets. You have to request it in runtime and check that you have it any time, when you are going to use it. Smth like that

        const val INTERET = Manifest.permission.INTERNET
        const val READ = Manifest.permission.READ_EXTERNAL_STORAGE
        const val WRITE = Manifest.permission.WRITE_EXTERNAL_STORAGE
        const val LOCATION_COARSE = Manifest.permission.ACCESS_COARSE_LOCATION
        const val LOCATION_FINE = Manifest.permission.ACCESS_FINE_LOCATION
        const val PHONE = Manifest.permission.CALL_PHONE

        fun granted(activity: Activity, vararg permission: String): Boolean {
            val list = ArrayList<String>()
            for (s in permission)
                if (ActivityCompat.checkSelfPermission(activity, s) != PackageManager.PERMISSION_GRANTED)
                    list.add(s)
            if (list.isEmpty())
                return true
            ActivityCompat.requestPermissions(activity, list.toArray(arrayOfNulls<String>(list.size)), 1)
            return false
        }

and in code check permission:

if(granted(this, READ, WHRITE)
    this.file.removeFile(path, fileName)
else 
    //do smth if you have no permission

good for you to react if user denied permission. You can do it in Activity.onRequestPermissionsResult

Upvotes: 0

Related Questions