Reputation: 33
The function getExternalStorageDirectory() and ApplicationDocumentDirectory() gives the same directory to me that is inside the android folder. I like to create a folder in storage/emulated/0 , i tried explicitly creating it with Directory('storage/emulated/0').create(); then it says you have no permission. And i gave all the possible permissions and still gets the same result. Is there any way to create a folder in storage/emulated/0 ?
NB: _getPermission returns a bool according to the permission status. This code works well in an emulator but it is not working in an actual device.
takePhoto(BuildContext context) async {
if ( await _getPermission(Permission.manageExternalStorage)){
final image =await picker.pickImage(source: ImageSource.camera);
var newPath = "storage/emulated/0/takephotosaver";
var directory = Directory(newPath).create();
File fileIMage = File(image!.path);
var imageName = DateTime.now().toString();
fileIMage.copy("$newPath/$imageName.jpg");
Navigator.push(context, MaterialPageRoute(builder: (context)=>MyPreview(fileIMage)));
}
} }
Edit :- This code is working in android version 9 and below but not working in android 10 and 11. Is there a way to display the image taken by my camera in a separate folder?
Upvotes: 3
Views: 1430
Reputation: 1216
Things have changed after android 10. You shouldn't create folders in storage/emulated/0
now. Google is strict about that, and your app may get rejected if you publish your app in Google PlayStore. If you still want to do that then add this permission in your AndroidManifest.xml
.
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
And to get the permission from the user do this,
if (await Permission.manageExternalStorage.request().isGranted) {...}
Edit:
Whenever you create a new file the device can't automatically find it. You'd have to manually tell the device to refresh for the files. Before you'd have to refresh all the files in the device for it to get updated which was very inefficient, but now you could just send the path of the file that you want to get updated. You can use the media_scanner plugin to do so.
Or If you wanna do it yourself with kotlin then here's the code,
private fun broadcastFileUpdate(path: String){
context.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.fromFile(File(path))))
}
Upvotes: 3