簡體   English   中英

flutter image_downloader 0.19.2 下載完成時崩潰

[英]flutter image_downloader 0.19.2 crash when finished downloading

我正在使用 image_downloader 0.19.2鏈接將 JPEG 圖像下載到外部 memory 中的圖片/子文件夾。

它開始下載,有時突然應用程序崩潰,有時它可以正常工作。

我無法讀取任何異常或錯誤!

我正在使用 6 到 10 的 android 版本在 3 台設備和模擬器上測試該應用程序。

Launching lib\main.dart on SM N960F in profile mode...
Running Gradle task 'assembleProfile'...
√ Built build\app\outputs\apk\profile\app-profile.apk (11.7MB).
Installing build\app\outputs\apk\app.apk...
Debug service listening on ws://127.0.0.1:53603/Z0HTCq4Xk7c=/ws
D/ViewRootImpl@9967f53[MainActivity]( 5951): ViewPostIme pointer 0
D/ViewRootImpl@9967f53[MainActivity]( 5951): ViewPostIme pointer 1
D/ViewRootImpl@9967f53[MainActivity]( 5951): ViewPostIme pointer 0
D/ViewRootImpl@9967f53[MainActivity]( 5951): ViewPostIme pointer 1
D/image_downloader( 5951): RequestResult(id=5626, remoteUri=https://pixabay.com/get/57e6d344495ab108f5d084609629317e1638dde5514c704c7d2879d59749c25e_1280.jpg, localUri=null, mediaType=null, totalSize=-1, title=, description=)
D/image_downloader( 5951): 0
D/image_downloader( 5951): RequestResult(id=5626, remoteUri=https://pixabay.com/get/57e6d344495ab108f5d084609629317e1638dde5514c704c7d2879d59749c25e_1280.jpg, localUri=null, mediaType=null, totalSize=-1, title=, description=)
D/image_downloader( 5951): 0
D/image_downloader( 5951): RequestResult(id=5626, remoteUri=https://pixabay.com/get/57e6d344495ab108f5d084609629317e1638dde5514c704c7d2879d59749c25e_1280.jpg, localUri=file:///storage/emulated/0/Pictures/AwesomeWallpapers/awesomeWallpaper-7.jpg, mediaType=image/jpeg, totalSize=172875, title=awesomeWallpaper-7.jpg, description=)
D/image_downloader( 5951): 0
D/image_downloader( 5951): RequestResult(id=5626, remoteUri=https://pixabay.com/get/57e6d344495ab108f5d084609629317e1638dde5514c704c7d2879d59749c25e_1280.jpg, localUri=file:///storage/emulated/0/Pictures/AwesomeWallpapers/awesomeWallpaper-7.jpg, mediaType=image/jpeg, totalSize=172875, title=awesomeWallpaper-7.jpg, description=)
D/image_downloader( 5951): 0
D/image_downloader( 5951): RequestResult(id=5626, remoteUri=https://pixabay.com/get/57e6d344495ab108f5d084609629317e1638dde5514c704c7d2879d59749c25e_1280.jpg, localUri=file:///storage/emulated/0/Pictures/AwesomeWallpapers/awesomeWallpaper-7.jpg, mediaType=image/jpeg, totalSize=172875, title=awesomeWallpaper-7.jpg, description=)
D/image_downloader( 5951): 100
D/image_downloader( 5951): RequestResult(id=5626, remoteUri=https://pixabay.com/get/57e6d344495ab108f5d084609629317e1638dde5514c704c7d2879d59749c25e_1280.jpg, localUri=file:///storage/emulated/0/Pictures/AwesomeWallpapers/awesomeWallpaper-7.jpg, mediaType=image/jpeg, totalSize=172875, title=awesomeWallpaper-7.jpg, description=)
D/AndroidRuntime( 5951): Shutting down VM
E/AndroidRuntime( 5951): FATAL EXCEPTION: main
E/AndroidRuntime( 5951): Process: com.example.awesomewallpapers, PID: 5951
E/AndroidRuntime( 5951): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.DOWNLOAD_COMPLETE flg=0x10 pkg=com.example.awesomewallpapers (has extras) } in com.ko2ic.imagedownloader.Downloader$execute$1@7807408
E/AndroidRuntime( 5951):    at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1656)
E/AndroidRuntime( 5951):    at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(Unknown Source:2)
E/AndroidRuntime( 5951):    at android.os.Handler.handleCallback(Handler.java:883)
E/AndroidRuntime( 5951):    at android.os.Handler.dispatchMessage(Handler.java:100)
E/AndroidRuntime( 5951):    at android.os.Looper.loop(Looper.java:237)
E/AndroidRuntime( 5951):    at android.app.ActivityThread.main(ActivityThread.java:7811)
E/AndroidRuntime( 5951):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 5951):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
E/AndroidRuntime( 5951):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1076)
E/AndroidRuntime( 5951): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
E/AndroidRuntime( 5951):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:515)
E/AndroidRuntime( 5951):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:138)
E/AndroidRuntime( 5951):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:52)
E/AndroidRuntime( 5951):    at android.database.CursorWrapper.getString(CursorWrapper.java:141)
E/AndroidRuntime( 5951):    at com.ko2ic.imagedownloader.ImageDownloaderPlugin$CallbackImpl.saveToDatabase(ImageDownloaderPlugin.kt:348)
E/AndroidRuntime( 5951):    at com.ko2ic.imagedownloader.ImageDownloaderPlugin$CallbackImpl.access$saveToDatabase(ImageDownloaderPlugin.kt:200)
E/AndroidRuntime( 5951):    at com.ko2ic.imagedownloader.ImageDownloaderPlugin$CallbackImpl$granted$3.invoke(ImageDownloaderPlugin.kt:301)
E/AndroidRuntime( 5951):    at com.ko2ic.imagedownloader.ImageDownloaderPlugin$CallbackImpl$granted$3.invoke(ImageDownloaderPlugin.kt:200)
E/AndroidRuntime( 5951):    at com.ko2ic.imagedownloader.Downloader.resolveDownloadStatus(Downloader.kt:171)
E/AndroidRuntime( 5951):    at com.ko2ic.imagedownloader.Downloader.access$resolveDownloadStatus(Downloader.kt:14)
E/AndroidRuntime( 5951):    at com.ko2ic.imagedownloader.Downloader$execute$1.onReceive(Downloader.kt:33)
E/AndroidRuntime( 5951):    at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1646)
E/AndroidRuntime( 5951):    ... 8 more
I/Process ( 5951): Sending signal. PID: 5951 SIG: 9
Lost connection to device.

這是我的代碼:

  Future<bool> downloadImage(BuildContext context, url, String user, String id) async {
    Toast.show("Downloading...", context,
        duration: Toast.LENGTH_SHORT, gravity: Toast.BOTTOM);

    var imageId = await ImageDownloader.downloadImage(url,
            destination: AndroidDestinationType.directoryPictures
              ..subDirectory("AwesomeWallpapers/$user$id.jpg"))
        .catchError((error) {
      if (error is PlatformException) {
        var path = "";
        if (error.code == "404") {
          print("Not Found Error.");
        } else if (error.code == "unsupported_file") {
          print("UnSupported FIle Error.");
          path = error.details["unsupported_file_path"];
        }
      }
      return false;
    });

    if (imageId != null) {
      openImage(imageId);
      return true;
    }
  }

  void openImage(var imageId) async {
    var path = await ImageDownloader.findPath(imageId);
    await ImageDownloader.open(path).catchError((error) {
      if (error is PlatformException) {
        if (error.code == "preview_error") {
          print(error.message);
        }
      }
    });
  }
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.awesomewallpapers">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="ANDROID.PERMISSION.READ_EXTERNAL_STORAGE"/>
    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
    <application
        android:name="io.flutter.app.FlutterApplication"
        android:label="awesomewallpapers"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

更新我剛剛注意到下載照片和遞歸調用一些代碼有很大的延遲,這些行在調試中一直顯示

D/image_downloader(25908): RequestResult(id=5643, remoteUri=https://pixabay.com/get/57e6d344495ab108f5d084609629317e1638dde5514c704c7d2879d59e4dc35c_1280.jpg, localUri=null, mediaType=image/jpeg, totalSize=-1, title=, description=)
D/image_downloader(25908): 0
D/image_downloader(25908): RequestResult(id=5643, remoteUri=https://pixabay.com/get/57e6d344495ab108f5d084609629317e1638dde5514c704c7d2879d59e4dc35c_1280.jpg, localUri=null, mediaType=image/jpeg, totalSize=-1, title=, description=)
D/image_downloader(25908): 0
D/image_downloader(25908): RequestResult(id=5643, remoteUri=https://pixabay.com/get/57e6d344495ab108f5d084609629317e1638dde5514c704c7d2879d59e4dc35c_1280.jpg, localUri=null, mediaType=image/jpeg, totalSize=-1, title=, description=)
D/image_downloader(25908): 0
D/image_downloader(25908): RequestResult(id=5643, remoteUri=https://pixabay.com/get/57e6d344495ab108f5d084609629317e1638dde5514c704c7d2879d59e4dc35c_1280.jpg, localUri=null, mediaType=image/jpeg, totalSize=-1, title=, description=)

之后應用程序崩潰但沒有下載一張照片,幾秒鍾后照片開始一張一張地顯示在目錄中!

我認為是因為版本不匹配。 嘗試在 android 清單文件中添加對外部存儲的傳統支持。

<application
    android:name= ...
    android:label= ...
    android:icon= ...
    ...
    android:requestLegacyExternalStorage="true">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM