gpt4 book ai didi

android - Image_picker throws removeInvalidNode jank list 中的所有节点都超时而不是返回图像

转载 作者:行者123 更新时间:2023-12-05 00:04:50 25 4
gpt4 key购买 nike

我正在尝试使用图库或相机选择图像并使用 image_picker 显示。

当我在 android 中运行该应用程序时,我可以选择图像但不能显示。相比之下,我第一次在控制台中获得关注。

I/HwViewRootImpl(11213): removeInvalidNode all the node in jank list is out of time

如果我重复同样的操作,它会在每次按下按钮而不是打开图库或相机时给出以下信息。

I/flutter (11213): PlatformException(already_active, Image picker is already active, null)

我从搜索中找到了以下解决方案,但没有解决我的问题。

以下是我用于检索图像的代码:

import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

class CameraApp extends StatefulWidget {
@override
_CameraAppState createState() => _CameraAppState();
}

class _CameraAppState extends State<CameraApp> {
File imageFile;

@override
void initState() {

super.initState();
}

Future _getImage(int type) async {
print("Called Image Picker");
var image = await ImagePicker.pickImage(
source: type == 1 ? ImageSource.camera : ImageSource.gallery,
);

setState(() {
print("$image.path");
imageFile = image;
});
}

Future<void> retrieveLostData() async {
final LostDataResponse response = await ImagePicker.retrieveLostData();
if (response == null) {
return;
}
if (response.file != null) {
setState(() {
if (response.type == RetrieveType.image) {
imageFile = response.file;
}
});
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Image Editor"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
imageFile != null
? Image.file(
imageFile,
height: MediaQuery.of(context).size.height / 2,
)
: Text("Image editor"),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: new Text("Add Slip"),
content: Row(
children: <Widget>[
Expanded(
child: new FlatButton(
child: new Text("Camera"),
onPressed: () {
_getImage(1);
Navigator.pop(context);
},
),
),
Expanded(
child: new FlatButton(
child: new Text("Gallery"),
onPressed: () {
_getImage(2);
Navigator.pop(context);
},
),
)
],
),
);
},
);
},
tooltip: 'Pick Image',
child: Icon(Icons.camera),
),
);
}
}

最佳答案

我已经尝试了您共享的示例代码,但出于某种原因遇到了编译器问题,但与您的问题不同。因此,我尝试调试您的代码。这是固定代码:

import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

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

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

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

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

class _MyHomePageState extends State<MyHomePage> {
File imageFile;

@override
void initState() {
super.initState();
}

Future _getImage(int type) async {
print("Called Image Picker");
var image = await await ImagePicker.platform.pickImage(
source: type == 1 ? ImageSource.camera : ImageSource.gallery,
);

setState(() {
print("$image.path");
imageFile = File(image.path);
});
}

Future<void> retrieveLostData() async {
final LostData response = await ImagePicker.platform.retrieveLostData();
if (response == null) {
return;
}
if (response.file != null) {
setState(() {
if (response.type == RetrieveType.image) {
imageFile = response.file as File;
}
});
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Image Editor"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
imageFile != null
? Image.file(
imageFile,
height: MediaQuery.of(context).size.height / 2,
)
: Text("Image editor"),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: new Text("Add Slip"),
content: Row(
children: <Widget>[
Expanded(
child: new FlatButton(
child: new Text("Camera"),
onPressed: () {
_getImage(1);
Navigator.pop(context);
},
),
),
Expanded(
child: new FlatButton(
child: new Text("Gallery"),
onPressed: () {
_getImage(2);
Navigator.pop(context);
},
),
)
],
),
);
},
);
},
tooltip: 'Pick Image',
child: Icon(Icons.camera),
),
);
}
}

很少有修复是这些行:

var image = await await ImagePicker.pickImage(
source: type == 1 ? ImageSource.camera : ImageSource.gallery,
);

它在编译器中有错误,所以我将其更改为:

var image = await await ImagePicker.platform.pickImage(
source: type == 1 ? ImageSource.camera : ImageSource.gallery,
);

与这些行相同:

Future<void> retrieveLostData() async {
final LostData response = await ImagePicker.retrieveLostData();
if (response == null) {
return;
}
if (response.file != null) {
setState(() {
if (response.type == RetrieveType.image) {
imageFile = response;
}
});
}
}

固定版本:

 Future<void> retrieveLostData() async {
final LostData response = await ImagePicker.platform.retrieveLostData();
if (response == null) {
return;
}
if (response.file != null) {
setState(() {
if (response.type == RetrieveType.image) {
imageFile = response.file as File;
}
});
}
}

还有这个

setState(() {
print("$image.path");
imageFile = image;
}

为此:

 setState(() {
print("$image.path");
imageFile = File(image.path);
}

原因可能是 image_picker 的版本我正在使用。目前我正在使用 image_picker: ^0.7.4 .

这是实际输出:

enter image description here

我也遇到过一个问题,如果你在 Android API 版本 30 中运行它,你会得到这个错误:

Unhandled Exception: PlatformException(no_available_camera, No cameras available for taking pictures., null, null)

解决方法是添加 <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>this GitHub post 中提到的 list 中.

关于android - Image_picker throws removeInvalidNode jank list 中的所有节点都超时而不是返回图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61767574/

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