diff --git a/android/build.gradle b/android/build.gradle index 0683f79..e5edbe8 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -22,10 +22,10 @@ rootProject.allprojects { apply plugin: 'com.android.library' android { - compileSdkVersion 30 + compileSdkVersion 33 defaultConfig { - minSdkVersion 16 + minSdkVersion 21 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } lintOptions { diff --git a/android/src/main/java/dev/jajoria/storagepath/StoragePathPlugin.java b/android/src/main/java/dev/jajoria/storagepath/StoragePathPlugin.java index bc10db7..12de48a 100644 --- a/android/src/main/java/dev/jajoria/storagepath/StoragePathPlugin.java +++ b/android/src/main/java/dev/jajoria/storagepath/StoragePathPlugin.java @@ -80,53 +80,13 @@ public void onDetachedFromActivity() { @Override public void onMethodCall(MethodCall call, final Result result) { if (call.method.equals("getImagesPath")) { - Permissions.check(activity, Manifest.permission.READ_EXTERNAL_STORAGE, null, new PermissionHandler() { - @Override - public void onGranted() { - getImagePaths(result); - } - - @Override - public void onDenied(Context context, ArrayList deniedPermissions) { - result.error("1", "Permission denied", null); - } - }); + getImagePaths(result); } else if (call.method.equals("getVideosPath")) { - Permissions.check(activity, Manifest.permission.READ_EXTERNAL_STORAGE, null, new PermissionHandler() { - @Override - public void onGranted() { - getVideoPath(result); - } - - @Override - public void onDenied(Context context, ArrayList deniedPermissions) { - result.error("1", "Permission denied", null); - } - }); + getVideoPath(result); } else if (call.method.equals("getFilesPath")) { - Permissions.check(activity, Manifest.permission.READ_EXTERNAL_STORAGE, null, new PermissionHandler() { - @Override - public void onGranted() { - getFilesPath(result); - } - - @Override - public void onDenied(Context context, ArrayList deniedPermissions) { - result.error("1", "Permission denied", null); - } - }); + getFilesPath(result); } else if (call.method.equals("getAudioPath")) { - Permissions.check(activity, Manifest.permission.READ_EXTERNAL_STORAGE, null, new PermissionHandler() { - @Override - public void onGranted() { - getAudioPath(result); - } - - @Override - public void onDenied(Context context, ArrayList deniedPermissions) { - result.error("1", "Permission denied", null); - } - }); + getAudioPath(result); } else { result.notImplemented(); } diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index 0c33efd..e2d0855 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -25,7 +25,7 @@ apply plugin: 'com.android.application' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { - compileSdkVersion 31 + compileSdkVersion 33 lintOptions { disable 'InvalidPackage' } @@ -33,7 +33,7 @@ android { defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "dev.jajoria.storagepathexample" - minSdkVersion 16 + minSdkVersion 21 targetSdkVersion 31 versionCode flutterVersionCode.toInteger() versionName flutterVersionName diff --git a/example/android/build.gradle b/example/android/build.gradle index 81840cc..541bb59 100644 --- a/example/android/build.gradle +++ b/example/android/build.gradle @@ -24,6 +24,6 @@ subprojects { project.evaluationDependsOn(':app') } -task clean(type: Delete) { +tasks.register("clean", Delete) { delete rootProject.buildDir } diff --git a/example/lib/file_model.dart b/example/lib/file_model.dart index df4da19..23f03e6 100644 --- a/example/lib/file_model.dart +++ b/example/lib/file_model.dart @@ -2,17 +2,15 @@ class FileModel { List files; String folder; - FileModel({this.files, this.folder}); + FileModel({required this.files, required this.folder}); - FileModel.fromJson(Map json) { - files = json['files'].cast(); - folder = json['folderName']; - } + factory FileModel.fromJson(Map json) => + FileModel(files: json['files'].cast(), folder: json['folderName']); Map toJson() { - final Map data = new Map(); - data['files'] = this.files; - data['folderName'] = this.folder; + final Map data = Map(); + data['files'] = files; + data['folderName'] = folder; return data; } } diff --git a/example/lib/main.dart b/example/lib/main.dart index 081fd1a..183f0a5 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -5,7 +5,9 @@ import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_storage_path/flutter_storage_path.dart'; -import 'package:storage_path_example/file_model.dart'; +import 'package:storage_path_example/utility.dart'; + +import 'file_model.dart'; void main() => runApp(MyApp()); @@ -15,8 +17,8 @@ class MyApp extends StatefulWidget { } class _MyAppState extends State { - List _files = new List(); - FileModel _selectedModel; + List _files = []; + FileModel? _selectedModel; @override void initState() { @@ -25,20 +27,26 @@ class _MyAppState extends State { } getImagesPath() async { - var imagePath = await StoragePath.imagesPath; - var images = jsonDecode(imagePath) as List; - _files = images.map((e) => FileModel.fromJson(e)).toList(); - if (_files != null && _files.length > 0) - setState(() { - _selectedModel = _files[0]; - }); + if(await Utils().requestStoragePermission()){ + var imagePath = await StoragePath.imagesPath; + debugPrint("imagePath $imagePath"); + var images = jsonDecode(imagePath!) as List; + _files = images.map((e) => FileModel.fromJson(e)).toList(); + if (_files.length > 0) + setState(() { + _selectedModel = _files[0]; + }); + } else { + debugPrint("Permission Granted False"); + } + } - Future getVideoPath() async { - String videoPath = ""; + Future getVideoPath() async { + String? videoPath = ""; try { videoPath = await StoragePath.videoPath; - var response = jsonDecode(videoPath); + var response = jsonDecode(videoPath!); print(response); } on PlatformException { videoPath = 'Failed to get path'; @@ -46,11 +54,11 @@ class _MyAppState extends State { return videoPath; } - Future getAudioPath() async { - String audioPath = ""; + Future getAudioPath() async { + String? audioPath = ""; try { audioPath = await StoragePath.audioPath; - var response = jsonDecode(audioPath); + var response = jsonDecode(audioPath!); print(response); } on PlatformException { audioPath = 'Failed to get path'; @@ -58,11 +66,11 @@ class _MyAppState extends State { return audioPath; } - Future getFilePath() async { - String filePath = ""; + Future getFilePath() async { + String? filePath = ""; try { filePath = await StoragePath.filePath; - var response = jsonDecode(filePath); + var response = jsonDecode(filePath!); print(response); } on PlatformException { filePath = 'Failed to get path'; @@ -79,20 +87,20 @@ class _MyAppState extends State { title: const Text('Plugin example app'), ), body: _selectedModel == null || - (_selectedModel != null && _selectedModel.files.length < 1) + (_selectedModel != null && _selectedModel!.files.length < 1) ? Container() : GridView.builder( - itemCount: _selectedModel.files.length, + itemCount: _selectedModel?.files.length, gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 4, crossAxisSpacing: 4, mainAxisSpacing: 4, ), itemBuilder: (_, i) { - var file = _selectedModel.files[i]; + var file = _selectedModel?.files[i]; return Container( child: Image.file( - File(file), + File(file!), fit: BoxFit.cover, ), ); diff --git a/example/lib/utility.dart b/example/lib/utility.dart new file mode 100644 index 0000000..4df8aeb --- /dev/null +++ b/example/lib/utility.dart @@ -0,0 +1,50 @@ +import 'dart:io'; + +import 'package:device_info_plus/device_info_plus.dart'; +import 'package:flutter/material.dart'; +import 'package:permission_handler/permission_handler.dart'; + +class Utils{ + + Future requestStoragePermission() async { + DeviceInfoPlugin plugin = DeviceInfoPlugin(); + + if(Platform.isAndroid) { + AndroidDeviceInfo android = await plugin.androidInfo; + if (android.version.sdkInt < 33) { + if (await Permission.storage.request().isGranted) { + debugPrint("Permission Granted True 13"); + return true; + } else if (await Permission.storage.request().isPermanentlyDenied) { + await openAppSettings(); + } else if (await Permission.storage.request().isDenied) { + debugPrint("Permission Granted False 13"); + return false; + } + } else { + if (await Permission.photos.request().isGranted ) { + debugPrint("Permission Granted True"); + return true; + } else if (await Permission.photos.request().isPermanentlyDenied) { + await openAppSettings(); + } else if (await Permission.photos.request().isDenied) { + debugPrint("Permission Granted False"); + return false; + } + } + } else if(Platform.isIOS) { + if (await Permission.storage.request().isGranted ) { + debugPrint("Permission Granted True iOS"); + return true; + } else if (await Permission.storage.request().isPermanentlyDenied) { + await openAppSettings(); + } else if (await Permission.storage.request().isDenied) { + debugPrint("Permission Granted False iOS"); + return false; + } + } + return true; + } + + +} \ No newline at end of file diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 0d6e5ac..c5a5443 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -3,15 +3,16 @@ description: Demonstrates how to use the storage_path plugin. publish_to: 'none' environment: - sdk: ">=2.10.0 <3.0.0" + sdk: ">=2.17.0 <3.0.0" dependencies: flutter: sdk: flutter + permission_handler: ^11.0.0 + device_info_plus: ^9.0.3 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. - cupertino_icons: ^0.1.2 dev_dependencies: flutter_test: diff --git a/pubspec.yaml b/pubspec.yaml index 6aad21f..8ecd9a7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,7 +4,7 @@ version: 1.0.4 homepage: https://github.com/ashish-jajoria/StoragePath environment: - sdk: '>=2.12.0 <3.0.0' + sdk: '>=2.17.0 <3.0.0' flutter: ">=1.12.0" dependencies: