-1

I am designing Android Application which will upload image files from particular folder in SD card to Google drive. But i am getting

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

Code snippet:

    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);

    }

The saveFileToDrive contains logic to upload one file to google drive. How to proceed? Please help me.....

Onik
  • 19,396
  • 14
  • 68
  • 91

1 Answers1

0

You're loading incrementally loading all of the images into memory in that for loop, as I'm assuming saveFileToDrive is running on a separate thread off the UI. What it would appear is happening is that you're trying to load all files in the SD directory into memory and upload them all to Drive in parallel (due to the threads).

A simple solution would be to make saveFileToDrive an AsyncTask that would notify the loop in the @Override onPostExecute() method (on the UI thread). You'd be uploading pictures on a separate thread so the UI won't lag, but would only be uploading one at a time so you shouldn't run out of memory.

Have you gone through the documentation here: https://developers.google.com/drive/android/create-file ?

stewjacks
  • 471
  • 6
  • 12