gpt4 book ai didi

dart - 如何使用 Flutter 在 iOS 和 Android 上共享图像?

转载 作者:IT老高 更新时间:2023-10-28 12:28:57 25 4
gpt4 key购买 nike

我想使用 iOS 和 Android 中的标准共享对话框共享图像。下面的代码大部分来自https://pub.dartlang.org/packages/share我用它作为起点(下面只有 Dart 和 Objective-C)。它目前仅共享文本。

我不确定下面的图像是否是最好的方法,我将如何将图像转换为 Dart 中的字节流并在 iOS 和 Android 中处理。

Dart

static const _kShareChannel = const MethodChannel('example.test.com/share');
Future<Null> shareImage(Image image) {
assert(image != null);
return _kShareChannel.invokeMethod('shareImage', image);
}

目标-C

static NSString *const PLATFORM_CHANNEL = @"example.test.com/share";

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GeneratedPluginRegistrant registerWithRegistry:self];

FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController;

FlutterMethodChannel *shareChannel = [FlutterMethodChannel methodChannelWithName:PLATFORM_CHANNEL
binaryMessenger:controller];

[shareChannel setMethodCallHandler:^(FlutterMethodCall *call, FlutterResult result) {
if ([@"shareImage" isEqualToString:call.method]) {
[self share:call.arguments withController:[UIApplication sharedApplication].keyWindow.rootViewController];
result(nil);
} else {
result([FlutterError errorWithCode:@"UNKNOWN_METHOD"
message:@"Unknown share method called"
details:nil]);
}
}];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

- (void)share:(id)sharedItems withController:(UIViewController *)controller {
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[ sharedItems ]
applicationActivities:nil];
[controller presentViewController:activityViewController animated:YES completion:nil];
}

最佳答案

下面将允许您在 iOS 上使用 UIActivityViewController 发送文件(在此示例中特别是图像),并在 Android 上作为共享意图。

FileProvider overview (Android)

更新 pubspec.yaml 以引用本地镜像(本例中为 image.jpg)并使用 path_provider 插件来访问文件系统。 https://pub.dartlang.org/packages/path_provider

main.dart

import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Share Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Share Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

@override
Widget build(BuildContext context) {

return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
],
),
),
floatingActionButton: new FloatingActionButton(
onPressed: _shareImage,
tooltip: 'Share',
child: new Icon(Icons.share),
),
);
}

_shareImage() async {
try {
final ByteData bytes = await rootBundle.load('assets/image.jpg');
final Uint8List list = bytes.buffer.asUint8List();

final tempDir = await getTemporaryDirectory();
final file = await new File('${tempDir.path}/image.jpg').create();
file.writeAsBytesSync(list);

final channel = const MethodChannel('channel:me.albie.share/share');
channel.invokeMethod('shareFile', 'image.jpg');

} catch (e) {
print('Share error: $e');
}
}
}

AppDelegate.m

#include "AppDelegate.h"
#include "GeneratedPluginRegistrant.h"

@implementation AppDelegate

static NSString *const SHARE_CHANNEL = @"channel:me.albie.share/share";

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GeneratedPluginRegistrant registerWithRegistry:self];
FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController;

FlutterMethodChannel *shareChannel =
[FlutterMethodChannel methodChannelWithName:SHARE_CHANNEL
binaryMessenger:controller];

[shareChannel setMethodCallHandler:^(FlutterMethodCall *call, FlutterResult result) {
if ([@"shareFile" isEqualToString:call.method]) {
[self shareFile:call.arguments
withController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
}];

return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

- (void)shareFile:(id)sharedItems withController:(UIViewController *)controller {
NSMutableString *filePath = [NSMutableString stringWithString:sharedItems];
NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *imagePath = [docsPath stringByAppendingPathComponent:filePath];
NSURL *imageUrl = [NSURL fileURLWithPath:imagePath];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
UIImage *shareImage = [UIImage imageWithData:imageData];

UIActivityViewController *activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:@[ shareImage ]
applicationActivities:nil];
[controller presentViewController:activityViewController animated:YES completion:nil];
}

@end

MainActivity.java

package com.example.share;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import java.io.File;

import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

import android.support.v4.content.FileProvider;

public class MainActivity extends FlutterActivity {

private static final String SHARE_CHANNEL = "channel:me.albie.share/share";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);

new MethodChannel(this.getFlutterView(), SHARE_CHANNEL).setMethodCallHandler(new MethodChannel.MethodCallHandler() {
public final void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
if (methodCall.method.equals("shareFile")) {
shareFile((String) methodCall.arguments);
}
}
});
}

private void shareFile(String path) {
File imageFile = new File(this.getApplicationContext().getCacheDir(), path);
Uri contentUri = FileProvider.getUriForFile(this, "me.albie.share", imageFile);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpg");
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
this.startActivity(Intent.createChooser(shareIntent, "Share image using"));
}
}

AndroidManifest.xml

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="me.albie.share"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>

xml/file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
<cache-path name="images" path="/"/>
</paths>

build.gradle(应用程序)

dependencies {
...
implementation 'com.android.support:support-v4:27.1.1'
}

关于dart - 如何使用 Flutter 在 iOS 和 Android 上共享图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44181343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com