skip to Main Content

I am building auto backup app, where i can auto backup all images and videos, how to aceess device all videos and images by asking photos and videos permission, and now how to get video’s, please help me till Android 14

2

Answers


  1. Chosen as BEST ANSWER

    Thank you working ❤️❤️, can I do same using path provider?


  2. To access all images and videos from a device running up to Android 14 (API level 30) in a Flutter app, you’ll need to handle permissions and use appropriate packages. The permission_handler package will help manage permissions, and photo_manager to fetch media files. Here’s a brief overview of the steps and some code snippets to help guide you:

    1.Add Dependencies
    Add the necessary packages to your pubspec.yaml file.

    dependencies:
      flutter:
        sdk: flutter
      permission_handler: ^10.2.0
      photo_manager: ^1.3.5
    

    Then run flutter pub get to install the packages.

    2.Handle Permissions
    You need to request permission to access photos and media files.

    import 'package:permission_handler/permission_handler.dart';
    
    Future<void> requestPermission() async {
      PermissionStatus status = await Permission.photos.request();
      if (status.isGranted) {
        // Permission granted, you can access photos and media files
      } else {
        // Handle permission denial
      }
    }
    

    3.Fetch Images and Videos
    You can use the photo_manager package to fetch media files.

    import 'package:photo_manager/photo_manager.dart';
    
    Future<void> fetchImagesAndVideos() async {
      // Request permission to access photos
      await requestPermission();
    
      // Fetch all media
      List<AssetPathEntity> media = await PhotoManager.getAssetPathList(
        type: RequestType.common,
      );
    
      // Iterate over the media and separate images and videos
      for (var path in media) {
        List<AssetEntity> assets = await path.assetList;
        for (var asset in assets) {
          if (asset.type == AssetType.image) {
            // This is an image
            // Do something with the image
          } else if (asset.type == AssetType.video) {
            // This is a video
            // Do something with the video
          }
        }
      }
    }
    

    4.Handle Android Permissions in the AndroidManifest.xml
    Edit your android/app/src/main/AndroidManifest.xml to include necessary permissions:

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

    5.Handle Scoped Storage for Android 11+
    For devices running Android 11 (API level 30) and above, you’ll have to handle scoped storage. Consider reading through Android’s scoped storage documentation for more details.

    6.Build and Run
    After setting everything up, try building and running your app. Ensure that the permissions work correctly, and you are able to fetch images and videos.

    Note: Always ensure to handle permissions gracefully and inform the user why the permissions are necessary.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search