Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> 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<String> 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<String> deniedPermissions) {
result.error("1", "Permission denied", null);
}
});
getAudioPath(result);
} else {
result.notImplemented();
}
Expand Down
4 changes: 2 additions & 2 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 31
compileSdkVersion 33
lintOptions {
disable 'InvalidPackage'
}

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
Expand Down
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
14 changes: 6 additions & 8 deletions example/lib/file_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ class FileModel {
List<String> files;
String folder;

FileModel({this.files, this.folder});
FileModel({required this.files, required this.folder});

FileModel.fromJson(Map<String, dynamic> json) {
files = json['files'].cast<String>();
folder = json['folderName'];
}
factory FileModel.fromJson(Map<String, dynamic> json) =>
FileModel(files: json['files'].cast<String>(), folder: json['folderName']);

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['files'] = this.files;
data['folderName'] = this.folder;
final Map<String, dynamic> data = Map<String, dynamic>();
data['files'] = files;
data['folderName'] = folder;
return data;
}
}
54 changes: 31 additions & 23 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -15,8 +17,8 @@ class MyApp extends StatefulWidget {
}

class _MyAppState extends State<MyApp> {
List<FileModel> _files = new List<FileModel>();
FileModel _selectedModel;
List<FileModel> _files = [];
FileModel? _selectedModel;

@override
void initState() {
Expand All @@ -25,44 +27,50 @@ class _MyAppState extends State<MyApp> {
}

getImagesPath() async {
var imagePath = await StoragePath.imagesPath;
var images = jsonDecode(imagePath) as List;
_files = images.map<FileModel>((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<FileModel>((e) => FileModel.fromJson(e)).toList();
if (_files.length > 0)
setState(() {
_selectedModel = _files[0];
});
} else {
debugPrint("Permission Granted False");
}

}

Future<void> getVideoPath() async {
String videoPath = "";
Future<String> 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';
}
return videoPath;
}

Future<void> getAudioPath() async {
String audioPath = "";
Future<String> 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';
}
return audioPath;
}

Future<void> getFilePath() async {
String filePath = "";
Future<String> 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';
Expand All @@ -79,20 +87,20 @@ class _MyAppState extends State<MyApp> {
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,
),
);
Expand Down
50 changes: 50 additions & 0 deletions example/lib/utility.dart
Original file line number Diff line number Diff line change
@@ -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<bool> 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;
}


}
5 changes: 3 additions & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down