Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to directly open an android media folder path using this package? #4

Open
fldvlpr opened this issue Dec 27, 2024 · 0 comments
Open

Comments

@fldvlpr
Copy link

fldvlpr commented Dec 27, 2024

@mgenware

Following is my flutter code to open media folder inside flutter. But it always redirect me to device path only after then I need to select folder manually like I need to go to "Android" then select "Media" folder to get acccess to it.

What is the way to direct get access to folder for "Android" > "Media"?

import 'package:flutter/material.dart';
import 'package:saf_util/saf_util.dart';

class MediaAccessRequestScreen extends StatefulWidget {
  @override
  _MediaAccessRequestScreenState createState() =>
      _MediaAccessRequestScreenState();
}

class _MediaAccessRequestScreenState extends State<MediaAccessRequestScreen> {
  final _safUtilPlugin = SafUtil();
  var _status = '';

  String makeUriString({String path = "", bool isTreeUri = false}) {
    String uri = "Android/media/";
    String base = "content://com.android.externalstorage.documents/tree/primary%3A";
    String documentUri = "/document/primary%3A${path.replaceAll("/", "%2F").replaceAll(" ", "%20")}";
    if (isTreeUri) {
      uri = base + path.replaceAll("/", "%2F").replaceAll(" ", "%20");
    } else {
      var pathSegments = path.split("/");
      var fileName = pathSegments[pathSegments.length - 1];
      var directory = path.split("/$fileName")[0];
      uri = base +
          directory.replaceAll("/", "%2F").replaceAll(" ", "%20") +
          documentUri;
    }
    return uri;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Saf example app')),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              ElevatedButton(
                  onPressed: () async {
                    try {
                      debugPrint("makeUriString: ${makeUriString()}");
                      final dir = await _safUtilPlugin.pickDirectory(
                          writePermission: true, initialUri: makeUriString());
                      if (dir == null) {
                        return;
                      }
                      if (!context.mounted) {
                        return;
                      }

                      debugPrint('path selected : ${dir.uri}, ${dir.name}');
                      setState(() {
                        _status = 'success: ${dir.uri}, ${dir.name}';
                      });
                    } catch (err) {
                      setState(() {
                        _status = 'Error: $err';
                      });
                    }
                  },
                  child: const Text('Hello')),
              SizedBox(height: 16),
              Text(_status)
            ],
          ),
        ),
      ),
    );
  }
}

Let me know what I'm missing?

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant