简体   繁体   中英

Get camera roll in Android Studio

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM