0

I have a flutter application that fetches images from internal storage, the permission for read/write are already handled using storage_path plugin in flutter.

Also write permission is given at AndroidManifest.xml file

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

The methodchannel code looks like this: filename example: /storage/emulated/0/Pictures/Screenshots/Screenshot_20201116-224238_Pixel_Launcher.png

    private val CHANNEL = "com.rs.gallery/gallery"

    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
            if (call.method == "Delete") {
                val file = call.argument<String>("file")
                Log.d("method channel", file)
                val f = File(file)
                if (f.exists()) {
                    val res: Boolean = f.delete()

                    Log.d(file, res.toString())
                } else {
                    Log.d(file, "NOT FOUND")
                }
            }
        }
    }

The output of the delete method is false and unable to delete the file. What am I missing? Or is the permission for deleting file from internal storage to be handled separately?

suranju
  • 25
  • 7
  • does your app asks for permission? – Yadu Nov 17 '20 at 07:47
  • the storage_path plugin asks only for read permission. I then wrote the write permission in the manifest. but still does not work. – suranju Nov 17 '20 at 09:09
  • even if you have them in your manifest, you have to ask the permission, the permission pop ups? remember, use permission handler plugin on the pub – Yadu Nov 17 '20 at 09:22
  • I had tried the permission_handler plugin (https://pub.dev/packages/permission_handler). There is option for only "storage" permission, which is the same as the permission requested by storage_path plugin. – suranju Nov 17 '20 at 09:28
  • storage path asks the user for permission? – Yadu Nov 17 '20 at 09:37
  • The storage_path plugin asks only for read permission of media files. I just tried the permission_handler plugin with request to storage permission, it shows permission is already granted, and does not prompt for permission, but still unable to delete file. (FYI: I am testing on physical device on Android 11). – suranju Nov 17 '20 at 09:51
  • storage permission is both read/write, why are you doing file operations on native when you can do the same in flutter – Yadu Nov 17 '20 at 09:55
  • ok, I try flutter and get back. – suranju Nov 17 '20 at 10:05
  • I get this error when I perform file.delete() `Unhandled Exception: FileSystemException: Cannot delete file, path = '/storage/emulated/0/Pictures/Screenshots/Screenshot_20201116-224238_Pixel_Launcher.png' (OS Error: No such file or directory, errno = 2)` – suranju Nov 17 '20 at 10:15
  • no file exist at the path, may be path is wrong!! – Yadu Nov 17 '20 at 10:16
  • I get the same error with all the files. – suranju Nov 17 '20 at 10:18
  • if it was permission problem the Exception would have been different your paths are wrong, try to list the files in `/storage/emulated/0/Pictures/Screenshots/` – Yadu Nov 17 '20 at 10:22
  • Here is the summary. On android 11, the images are displayed but unable to delete. On android 10, the images are NOT displayed, says permission denied. Compile SDK 29, min SDK 21 – suranju Nov 17 '20 at 10:34

0 Answers0