Write file in flash drive (android only)
Asked Answered
R

1

6

Is it possible to save files to a flash drive using flutter?

I'm using a pendrive with a USB c port like this: https://www.bestbuy.com/site/sandisk-ultra-dual-drive-go-1tb-usb-type-a-usb-type-c-flash-drive-black/6560692.p?skuId=6560692

I found this issue: https://github.com/flutter/flutter/issues/40504

I've tried several things, path_provider doesn't return directories correctly with getExternalStorageDirectories()

I have already added the permissions to the manifest and requested it with permission_handler

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

I also tried with file_picker await FilePicker.platform.getDirectoryPath();

But when selecting a folder on the flash drive the path it returns is just: /

I also tried using usb_serial:

final bytes = <int>[];

await yt.videos.streamsClient.get(audioStream).listen((chunk) {
  bytesReceived += chunk.length;
  bytes.addAll(chunk);

}, onDone: () async{
  MyLog.log('Done onDone');

}, onError: (error) async{
  MyLog.log('Done error: $error');

}).asFuture();

final devices = await UsbSerial.listDevices();
final port = await devices.first.create();
final openResult = await port?.open();

if ( !(openResult ?? false)) {
  MyLog.log("Failed to open");
  return;

}

port?.inputStream?.listen((Uint8List event) {
  MyLog.log('Event close');
  port.close();
});

await port?.write(Uint8List.fromList(bytes));

But it did not work

Rosecan answered 31/12, 2023 at 15:54 Comment(3)
Didn't you try 3rd parties? E.g. pub.dev/packages/sdcard_path_managerMontgolfier
It's possible on native android only via StorageAccessFramework. I can't help with flutter, but this is my solution with native Android to read and write files: #64720879. Unfortunately I didn't implement microsd/usb support because my project didn't require it, but I will be glad if this solution will help you to understand SAF.Gaussmeter
Of course, it is possible to add microsd/usb support, but it will require paying attention to the virtual path to the device, which I once decided not to waste my time on. If you implement - I will be glad to your additions via Edit button.Gaussmeter
C
1

Try using Flutter’s platform channels to communicate with native Android code (Java or Kotlin) that handles the file operations on the USB drive.

flutter:

import 'package:flutter/services.dart';

class UsbStorage {
  static const platform = const MethodChannel('com.example.app/usb');
   ~~~code
}

kotlin:

class MainActivity: FlutterActivity() {
  private val CHANNEL = "com.example.app/usb"

  override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
~~~code
}
Chine answered 5/1, 2024 at 16:19 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.