admin管理员组

文章数量:1315343

I am developing an app using flutterflow and need to randomly choose an image from the user's gallery without user action (in both iOS and android). Clearly being a custom function I was thinking of writing it in dart. I have already created this function and it works very well in android, but not in iOS. This is the code I wrote:

// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/backend/schema/structs/index.dart';
import '/backend/schema/enums/enums.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!

import 'dart:io';
import 'dart:math';

Future<String> getRandomImagePath() async {
  final directories = [
    Directory('/storage/emulated/0/DCIM'),
    Directory('/storage/emulated/0/Pictures'),
    Directory('/DCIM'),
    Directory('/Pictures'),
    Directory('/storage/emulated/0/DCIM/Camera'),
  ];

  List<FileSystemEntity> imageFiles = [];
  Iterable<FileSystemEntity> newDirectories = [];

//search album inside folder
  for (var directory in directories) {
    if (await directory.exists()) {
      final elements = await directory.list().toList();
      print(elements);
      newDirectories = elements.where((dir) => dir is Directory);
    }
  }

  for (var directory in newDirectories) {
    if (await directory.exists() && directory is Directory) {
      directories.add(directory);
    }
  }

//search images inside folder
  for (var directory in directories) {
    if (await directory.exists()) {
      final files = await directory.list().toList();
      print('In questa $directory io vedo questi $files');

      for (var file in files) {
        if (file is File &&
            (file.path.endsWith('.jpg') ||
                file.path.endsWith('.jpeg') ||
                file.path.endsWith('.png'))) {
          imageFiles.add(file);
        }
      }
    }
  }

  if (imageFiles.isNotEmpty) {
    final random = Random();
    final randomIndex = random.nextInt(imageFiles.length);
    return imageFiles.elementAt(randomIndex).path;
  }

  return ""; // No image found
}

What I need is:

  • option 1: modify this to make it work for both iOS and Android
  • option 2: have another function to do the same thing on iOS

I have tried various things but have not been able to find a way to make it work.

I am developing an app using flutterflow and need to randomly choose an image from the user's gallery without user action (in both iOS and android). Clearly being a custom function I was thinking of writing it in dart. I have already created this function and it works very well in android, but not in iOS. This is the code I wrote:

// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/backend/schema/structs/index.dart';
import '/backend/schema/enums/enums.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!

import 'dart:io';
import 'dart:math';

Future<String> getRandomImagePath() async {
  final directories = [
    Directory('/storage/emulated/0/DCIM'),
    Directory('/storage/emulated/0/Pictures'),
    Directory('/DCIM'),
    Directory('/Pictures'),
    Directory('/storage/emulated/0/DCIM/Camera'),
  ];

  List<FileSystemEntity> imageFiles = [];
  Iterable<FileSystemEntity> newDirectories = [];

//search album inside folder
  for (var directory in directories) {
    if (await directory.exists()) {
      final elements = await directory.list().toList();
      print(elements);
      newDirectories = elements.where((dir) => dir is Directory);
    }
  }

  for (var directory in newDirectories) {
    if (await directory.exists() && directory is Directory) {
      directories.add(directory);
    }
  }

//search images inside folder
  for (var directory in directories) {
    if (await directory.exists()) {
      final files = await directory.list().toList();
      print('In questa $directory io vedo questi $files');

      for (var file in files) {
        if (file is File &&
            (file.path.endsWith('.jpg') ||
                file.path.endsWith('.jpeg') ||
                file.path.endsWith('.png'))) {
          imageFiles.add(file);
        }
      }
    }
  }

  if (imageFiles.isNotEmpty) {
    final random = Random();
    final randomIndex = random.nextInt(imageFiles.length);
    return imageFiles.elementAt(randomIndex).path;
  }

  return ""; // No image found
}

What I need is:

  • option 1: modify this to make it work for both iOS and Android
  • option 2: have another function to do the same thing on iOS

I have tried various things but have not been able to find a way to make it work.

Share Improve this question edited Jan 31 at 10:15 nvrm22 asked Jan 30 at 9:37 nvrm22nvrm22 737 bronze badges 2
  • 1 What doesn't work in iOS? Can you provide some details? – tomerpacific Commented Jan 30 at 16:32
  • The problem is that it cannot find any images. Probably none of the paths are usable in iOS. One of the problems is that I have not found any path to the images in iOS. And if there is no path I wanted to know if anyone knew of a way to achieve the same result in android – nvrm22 Commented Jan 31 at 8:15
Add a comment  | 

1 Answer 1

Reset to default 0

I see you are using hardcoded directory paths (which are essentially Android-based)

That can answer the query as to why it works on Android and not on iOS.

For this, it is recommended to use a library that’s compatible with your platform and serves the purpose of getting your platform-specific directory. eg path

Secondly, there’s no invocation code for this function, but I do see a file picker imported, so it would be best to probe further based on your code, which you haven’t furnished yet.

本文标签: iosFlutter Get random image from galleryStack Overflow