-1

我正在设计 Android 应用程序,它将图像文件从 SD 卡中的特定文件夹上传到 Google 驱动器。但我得到

E/AndroidRuntime( 6808): FATAL EXCEPTION: main
E/AndroidRuntime( 6808): java.lang.OutOfMemoryError error while trying to upload the files.

代码片段:

    File sdDir = Environment
              .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File yourDir = new File(sdDir, "CameraAPIDemo");
    for (File f : yourDir.listFiles()) {
        if (f.isFile()){
            String name = f.getName();
            Log.i(TAG, "name =" + name);
            String CompletePath=yourDir + "/" + name;
            //Decode the file from the folder to bitmap
            Bitmap bmp = BitmapFactory.decodeFile(CompletePath);
            saveFileToDrive(sFolderId,bmp,name);

    }

saveFileToDrive 包含将一个文件上传到谷歌驱动器的逻辑。如何进行?请帮我.....

4

1 回答 1

0

您正在将所有图像增量加载到该 for 循环中的内存中,因为我假设 saveFileToDrive 正在 UI 之外的单独线程上运行。看起来正在发生的是,您正在尝试将 SD 目录中的所有文件加载到内存中并将它们全部上传到并行驱动器(由于线程)。

一个简单的解决方案是使 saveFileToDrive 成为 AsyncTask,它会在 @Override onPostExecute() 方法(在 UI 线程上)中通知循环。您将在单独的线程上上传图片,因此 UI 不会滞后,但一次只能上传一张,因此您不应该耗尽内存。

您是否浏览过这里的文档:https ://developers.google.com/drive/android/create-file ?

于 2014-05-09T15:38:51.390 回答