当尝试从Flutter在Android上共享屏幕截图时出错

y3bcpkx1  于 2023-05-12  发布在  Android
关注(0)|答案(1)|浏览(126)

在我的代码,当我试图按下按钮“采取截图和共享”在iOS -我成功地可以保存和共享截图。但是在android中我得到下一个错误:

D/EGL_emulation( 6505): app_time_stats: avg=13107.49ms min=1.30ms max=380030.59ms count=29
E/MethodChannel#plugins.flutter.io/share( 6505): Failed to handle method call
E/MethodChannel#plugins.flutter.io/share( 6505): java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.swize_frontend/cache/share/customerPhotos.png
E/MethodChannel#plugins.flutter.io/share( 6505):        at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:825)
E/MethodChannel#plugins.flutter.io/share( 6505):        at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:450)
E/MethodChannel#plugins.flutter.io/share( 6505):        at io.flutter.plugins.share.Share.getUrisForPaths(Share.java:128)
E/MethodChannel#plugins.flutter.io/share( 6505):        at io.flutter.plugins.share.Share.shareFiles(Share.java:69)
E/MethodChannel#plugins.flutter.io/share( 6505):        at io.flutter.plugins.share.MethodCallHandler.onMethodCall(MethodCallHandler.java:42)
E/MethodChannel#plugins.flutter.io/share( 6505):        at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:258)
E/MethodChannel#plugins.flutter.io/share( 6505):        at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/MethodChannel#plugins.flutter.io/share( 6505):        at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322)
E/MethodChannel#plugins.flutter.io/share( 6505):        at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/MethodChannel#plugins.flutter.io/share( 6505):        at android.os.Handler.handleCallback(Handler.java:942)
E/MethodChannel#plugins.flutter.io/share( 6505):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/MethodChannel#plugins.flutter.io/share( 6505):        at android.os.Looper.loopOnce(Looper.java:201)
E/MethodChannel#plugins.flutter.io/share( 6505):        at android.os.Looper.loop(Looper.java:288)
E/MethodChannel#plugins.flutter.io/share( 6505):        at android.app.ActivityThread.main(ActivityThread.java:7898)
E/MethodChannel#plugins.flutter.io/share( 6505):        at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#plugins.flutter.io/share( 6505):        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
E/MethodChannel#plugins.flutter.io/share( 6505):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
E/flutter ( 6505): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(error, Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.swize_frontend/cache/share/customerPhotos.png, null, java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.swize_frontend/cache/share/customerPhotos.png
E/flutter ( 6505):      at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:825)
E/flutter ( 6505):      at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:450)
E/flutter ( 6505):      at io.flutter.plugins.share.Share.getUrisForPaths(Share.java:128)
E/flutter ( 6505):      at io.flutter.plugins.share.Share.shareFiles(Share.java:69)
E/flutter ( 6505):      at io.flutter.plugins.share.MethodCallHandler.onMethodCall(MethodCallHandler.java:42)
E/flutter ( 6505):      at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:258)
E/flutter ( 6505):      at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/flutter ( 6505):      at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322)
E/flutter ( 6505):      at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/flutter ( 6505):      at android.os.Handler.handleCallback(Handler.java:942)
E/flutter ( 6505):      at android.os.Handler.dispatchMessage(Handler.java:99)
E/flutter ( 6505):      at android.os.Looper.loopOnce(Looper.java:201)
E/flutter ( 6505):      at android.os.Looper.loop(Looper.java:288)
E/flutter ( 6505):      at android.app.ActivityThread.main(ActivityThread.java:7898)
E/flutter ( 6505):      at java.lang.reflect.Method.invoke(Native Method)
E/flutter ( 6505):      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
E/flutter ( 6505):      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
E/flutter ( 6505): )
E/flutter ( 6505): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:653:7)
E/flutter ( 6505): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:315:18)
E/flutter ( 6505): <asynchronous suspension>
E/flutter ( 6505): #2      _MyAppState._takeScreenshot (package:swize_frontend/main.dart:56:5)
E/flutter ( 6505): <asynchronous suspension>
E/flutter ( 6505):

我尝试在主文件夹中添加androidManifest的权限:

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

但对我没用我不确定是否是权限的问题,或者我指定了错误的权限...但如果同样的代码没有在iPhone中导致错误,那么也许它真的在它们中?
这是完整的代码:

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:screenshot/screenshot.dart';
import 'package:share/share.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _screenshotController = ScreenshotController();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            body: Center(
                child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
          Screenshot(
            controller: _screenshotController,
            child: Card(
                child: Padding(
              padding: EdgeInsets.all(16),
              child: Column(
                children: [
                  Image.asset('assets/images/customerPhotos.png'),
                  Text(
                    'Code Passionately',
                    style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
                  ),
                ],
              ),
            )),
          ),
          TextButton(
            child: Text('Take Screenshot and Share'),
            onPressed: _takeScreenshot,
          )
        ]))));
  }

  void _takeScreenshot() async {
    final uint8List = await _screenshotController.capture();
    String tempPath = (await getTemporaryDirectory()).path;
    File file = File('$tempPath/customerPhotos.png');
    await file.writeAsBytes(uint8List as List<int>);
    await Share.shareFiles([file.path]);
  }
}
pepwfjgg

pepwfjgg1#

我改变lib 'share' which 'share_plus',现在共享工作:

import 'dart:io';
import 'package:file_selector/file_selector.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:screenshot/screenshot.dart';
import 'package:share_plus/share_plus.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _screenshotController = ScreenshotController();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            body: Center(
                child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
          Screenshot(
            controller: _screenshotController,
            child: Card(
                child: Padding(
              padding: EdgeInsets.all(16),
              child: Column(
                children: [
                  Image.asset('assets/images/customerPhotos.png'),
                  Text(
                    'Code Passionately',
                    style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
                  ),
                ],
              ),
            )),
          ),
          TextButton(
            child: Text('Take Screenshot and Share'),
            onPressed: _takeScreenshot,
          )
        ]))));
  }

  void _takeScreenshot() async {
    final uint8List = await _screenshotController.capture();
    String tempPath = (await getTemporaryDirectory()).path;
    File file = File('$tempPath/customerPhotos.png');
    await file.writeAsBytes(uint8List as List<int>);
    final xFile = XFile(file.path);
    await Share.shareXFiles([xFile]);
  }
}

但现在我有了另一个问题:在Android中,我无法选择save into galary选项。如何添加此?enter image description here

相关问题