android和ios共享照片,如何使用Flutter在iOS和Android上共享图像?

小编典典

以下内容可让您UIActivityViewController在iOS上使用文件(在此示例中为图像)发送邮件,并在Android上作为共享意向文件。

更新pubspec.yaml以引用您的图像(如果是本地的话)(在此示例中为image.jpg),并使用该path_provider插件来访问文件系统。https://pub.dartlang.org/packages/path_provider

主镖

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 {

@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: [

],

),

),

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

android:name="android.support.v4.content.FileProvider"

android:authorities="me.albie.share"

android:exported="false"

android:grantUriPermissions="true">

android:name="android.support.FILE_PROVIDER_PATHS"

android:resource="@xml/file_paths" />

xml / file_paths.xml

build.gradle(应用程序)

dependencies {

...

implementation 'com.android.support:support-v4:27.1.1'

}

2020-08-13

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值