0

I'm trying to create an auto-backup app in which all photos taken from the Camera will be uploaded to Cloudinary. However, I cannot seem to get my code working. What am I doing wrong?

    class UploadPhotos implements Runnable {

        @Override
        public void run() {
            File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
            File[] listOfFiles = storageDir.listFiles();

            if (listOfFiles != null) {
                System.out.println("code does not get this far");
                for (int i = 0; i < listOfFiles.length; i++) {
                    if (listOfFiles[i].isFile()) {
                        System.out.println("File " + listOfFiles[i].getName());
                        try {
                            cloudinary.uploader().upload(listOfFiles[i], ObjectUtils.emptyMap());
                            System.out.print(listOfFiles[i] + " should have uploaded");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    } else if (listOfFiles[i].isDirectory()) {
                        System.out.println("Directory " + listOfFiles[i].getName());
                    }
                }
            }
        }
    }

The storageDir path comes out as /storage/0804-0A1C/DCIM within the Android Studio emulator but listOfFiles is null (despite having taken several pictures with the Camera).

  • 1
    probably because YOUR filesystem doesn't hvae that path. – Philipp Sander Nov 26 '15 at 14:50
  • First, that's not the recommended base directory on external storage for camera apps. That would be `DIRECTORY_DCIM`. Second, there is no requirement that any camera app store photos in any particular location, let alone one you have access to. There are thousands of Android device models. These ship with hundreds of different pre-installed camera apps, and there are countless other camera apps available through distribution channels like the Play Store. They all can do whatever they want. – CommonsWare Nov 26 '15 at 15:00
  • @CommonsWare Thanks, I've corrected it to DCIM and will edit the post. However, that still results in listOfFiles being null. Surely there must be a way to find the save location for the default Camera app on a device? –  Nov 26 '15 at 15:28
  • "Surely there must be a way to find the save location for the default Camera app on a device?" -- no. BTW, all of the `DIRECTORY_` locations are base directories. Files are not necessarily stored directly in them. In the case of `DIRECTORY_DCIM`, more often than not, the camera app will set up a subdirectory. So, you will want to do a recursive scan. That won't help in your particular case (my guess: the emulator camera app doesn't actually take a picture), and is still not going to work across all possible devices, but it at least improves your current algorithm a bit. – CommonsWare Nov 26 '15 at 15:52
  • @CommonsWare Then how would, say, the Facebook app, find images when someone wishes to make a post (such as http://www.insidefacebook.com/wp-content/uploads/2011/06/Android-Upload-Video.jpg)? Android Studio does indeed take a picture via webcam, and it persists a restart so the image must be saved somewhere. –  Nov 26 '15 at 18:00
  • The Facebook app would use `ACTION_GET_CONTENT` or `ACTION_OPEN_DOCUMENT`. Or, possibly, the Facebook app would query the `MediaStore`. – CommonsWare Nov 26 '15 at 18:04
  • @CommonsWare True, thank you. –  Nov 27 '15 at 03:16

1 Answers1

0

Using Android Device Monitor, I found that the storage location for Camera images in my Android Studio is "/storage/0804-0A1C/DCIM/Camera".

However, I am unable to read from this directory despite recursive checks (because of permissions?). To avoid going off-topic, I've posted a new question linked below:

Camera storage perms in Android Studio

Community
  • 1
  • 1