質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Flutter

Flutterは、iOSとAndroidのアプリを同じコードで開発するためのフレームワークです。オープンソースで開発言語はDart。双方のプラットフォームにおける高度な実行パフォーマンスと開発効率を提供することを目的としています。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Dart

Dartは、Googleによって開発されたJavaScriptの代替となることを目的に作られた、ウェブ向けのプログラミング言語である。

Q&A

解決済

1回答

835閲覧

flutterでAndroid端末の画像を表示する時のエラーを解消したい

t_flutter

総合スコア7

Flutter

Flutterは、iOSとAndroidのアプリを同じコードで開発するためのフレームワークです。オープンソースで開発言語はDart。双方のプラットフォームにおける高度な実行パフォーマンスと開発効率を提供することを目的としています。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Dart

Dartは、Googleによって開発されたJavaScriptの代替となることを目的に作られた、ウェブ向けのプログラミング言語である。

0グッド

0クリップ

投稿2023/04/07 14:46

実現したいこと

  • click hereボタンを押すとAndroid端末の画像フォルダ(Photos)内の最新の画像を1枚アプリ内で表示する。
  • assetフォルダでもWeb上の画像でもなく、端末の画像を表示したい。

前提

  • アプリ起動時はassetフォルダ内の画像を読み込んで表示する。
  • assetフォルダには初期表示用の画像を1枚保存している。
  • click hereボタンを押すと端末のフォルダのパスを取得して、その中の先頭の画像を表示する。
  • 端末(emulator)のフォルダにはwebカメラで撮影した画像が保存されている。
  • 端末のフォルダ内の画像パスを取得するところまでは出来ていることを確認済み。(printで取得したパスをターミナルに出力して確認した。)
  • flutter_storage_pathパッケージを使用して、端末の画像フォルダの構成をjson形式で取得している。それをListに変換?してImage.asset(Listの要素)で画像を表示している。
  • Android emulator(APIレベル30)を使用
  • 以下のプログラムで端末画像の表示に成功していたが何度か繰り返すうちにエラーが発生するようになった。

発生している問題・エラーメッセージ

パスは取得出来ていますが画像が読み込めていないようです。

Image provider: AssetImage(bundle: null, name: "/storage/emulated/0/Pictures/IMG_20230406_233341.jpg") Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#b4f18(), name: "/storage/emulated/0/Pictures/IMG_20230406_233341.jpg", scale: 1.0)

該当のソースコード

main.dart

import 'package:flutter/material.dart'; import 'package:flutter_storage_path/flutter_storage_path.dart'; import 'dart:convert'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: const MyHomePage(title: 'Flutter Demo Home Page'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title}); final String title; @override State<MyHomePage> createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { //グローバル変数として定義 List<dynamic> images = [ { "files": [ "assets/images/4847156604249137743.2e2510548ecf1bb9a0cec09c3ac590cc.23020602.jpg" //"/storage/emulated/0/Pictures/IMG_20230404_235901.jpg" ], "folderName": "Pictures" } ]; String? imagePath = ''; getImagesPath() async { imagePath = await StoragePath.imagesPath; var getimages = jsonDecode(imagePath!) as List; print(imagePath); print(getimages); setState(() { images = List.of(getimages); //参照渡しではなく値のコピー(deep copy) }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ OutlinedButton( onPressed: () { getImagesPath(); }, child: const Text('click here'), ), Image.asset(images[0]["files"]![0]), /* Image.file( File(images[0]["files"]![0]), fit: BoxFit.contain, ) */ ], ), ), ); } }

pubspec.yaml

name: flutter_get_image_test description: A new Flutter project. # The following line prevents the package from being accidentally published to # pub.dev using `flutter pub publish`. This is preferred for private packages. publish_to: 'none' # Remove this line if you wish to publish to pub.dev # The following defines the version and build number for your application. # A version number is three numbers separated by dots, like 1.2.43 # followed by an optional build number separated by a +. # Both the version and the builder number may be overridden in flutter # build by specifying --build-name and --build-number, respectively. # In Android, build-name is used as versionName while build-number used as versionCode. # Read more about Android versioning at https://developer.android.com/studio/publish/versioning # In iOS, build-name is used as CFBundleShortVersionString while build-number is used as CFBundleVersion. # Read more about iOS versioning at # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. version: 1.0.0+1 environment: sdk: '>=2.18.2 <3.0.0' # Dependencies specify other packages that your package needs in order to work. # To automatically upgrade your package dependencies to the latest versions # consider running `flutter pub upgrade --major-versions`. Alternatively, # dependencies can be manually updated by changing the version numbers below to # the latest version available on pub.dev. To see which dependencies have newer # versions available, run `flutter pub outdated`. dependencies: flutter: sdk: flutter flutter_storage_path: ^1.0.4 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.2 dev_dependencies: flutter_test: sdk: flutter # The "flutter_lints" package below contains a set of recommended lints to # encourage good coding practices. The lint set provided by the package is # activated in the `analysis_options.yaml` file located at the root of your # package. See that file for information about deactivating specific lint # rules and activating additional ones. flutter_lints: ^2.0.0 # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec # The following section is specific to Flutter packages. flutter: # The following line ensures that the Material Icons font is # included with your application, so that you can use the icons in # the material Icons class. uses-material-design: true # To add assets to your application, add an assets section, like this: assets: - assets/images/ # - images/a_dot_ham.jpeg # An image asset can refer to one or more resolution-specific "variants", see # https://flutter.dev/assets-and-images/#resolution-aware # For details regarding adding assets from package dependencies, see # https://flutter.dev/assets-and-images/#from-packages # To add custom fonts to your application, add a fonts section here, # in this "flutter" section. Each entry in this list should have a # "family" key with the font family name, and a "fonts" key with a # list giving the asset and other descriptors for the font. For # example: # fonts: # - family: Schyler # fonts: # - asset: fonts/Schyler-Regular.ttf # - asset: fonts/Schyler-Italic.ttf # style: italic # - family: Trajan Pro # fonts: # - asset: fonts/TrajanPro.ttf # - asset: fonts/TrajanPro_Bold.ttf # weight: 700 # # For details regarding fonts from package dependencies, # see https://flutter.dev/custom-fonts/#from-packages

試したこと

初心者なりに調べてみましたがassetフォルダに配置した画像を表示する時のエラーについての対処法ばかりで参考になる情報を見つけることができませんでした。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

Image.assetではなくImage.fileを使う。

投稿2023/04/08 12:40

t_flutter

総合スコア7

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問