Sagar Aghara
Sagar Aghara

Reputation: 640

String[] array not working properly

i have a Problem with String[] array.I refer about this from Here & Here

But still, i have not found any solution from the link.

What I want:

I am developing a Custom Camera App.I have a Folder in which I am saving a Captured images.also, i have 1 ImageView. when my App Launched the Image [or Bitmap] of Folder is set into that ImageView (Always set the First image of the folder into ImageView).

Following is my Code.

private void loadImageInImageView() {
        Uri[] mUrls;
        String[] mFiles = new String[0];

        File file = new File(android.os.Environment.getExternalStorageDirectory(), "CameraApp/Images");

        File[] imageList = file.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File file, String name) {
                return ((name.endsWith(".jpg")) || (name.endsWith(".png")) || (name.endsWith(".mp4")));
            }
        });

        if (mFiles.length >= 0) {
            Toast.makeText(this, "No Captured Images", Toast.LENGTH_SHORT).show();
        } else {
            mFiles = new String[imageList.length];

            for (int i = 0; i < imageList.length; i++) {
                mFiles[i] = imageList[i].getAbsolutePath();
            }
            mUrls = new Uri[mFiles.length];

            for (int i = 0; i < mFiles.length; i++) {
                mUrls[i] = Uri.parse(mFiles[i]);
                imgBtnThumbnail.setImageURI(mUrls[i]);
            }
        }
    }

in Above code [when Folder is Empty] :

  1. When i set if (mFiles.length > 0)

    it shows me an Error

Error: Attempt to get length of null array

  1. When i set if (mFiles.length >= 0)

    Then it shows me same as Above Error.

  2. When i set if (mFiles == null)

    it is also not Working because I have initialized String[] in above code.

    String[] mFiles = new String[0]; Hence it also not work.

When I have some Images then it working Fine.because it executed the else part. what should I do?Whenever my Folder is Empty then it Shows me a Toast.otherwise my else code will be executed.

Any help will be Highly appreciated.

Upvotes: 0

Views: 140

Answers (4)

Ethan Choi
Ethan Choi

Reputation: 2399

If the function you want works when the size of the imageList is greater than zero, try below code.

private void loadImageInImageView() {

    File file = new File(android.os.Environment.getExternalStorageDirectory(), "CameraApp/Images");

    File[] imageList = file.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File file, String name) {
            return ((name.endsWith(".jpg")) || (name.endsWith(".png")) || (name.endsWith(".mp4")));
        }
    });

    // conditional operator [ ? : ]
    // value = (experssion) ? value if true : value if false
    // ref : https://www.tutorialspoint.com/java/java_basic_operators.htm
    int imgLength = imageList == null ? 0 : imageList.length;

    if(imgLength > 0)
    {
        String[] mFiles =  new String[imgLength];
        Uri[] mUrls = new Uri[imgLength];

        //merge for condition
        for (int i = 0; i < imgLength; i++) {
            mFiles[i] = imageList[i].getAbsolutePath();
            mUrls[i] = Uri.parse(mFiles[i]);
            imgBtnThumbnail.setImageURI(mUrls[i]);
        }
    }
    else
    {
        Toast.makeText(this, "No Captured Images", Toast.LENGTH_SHORT).show();
    }
}

Upvotes: 1

Kiran Benny Joseph
Kiran Benny Joseph

Reputation: 6813

private void loadImageInImageView() {
        Uri[] mUrls;
        File[] imageList

        File file = new File(android.os.Environment.getExternalStorageDirectory(), "CameraApp/Images");

        imageList = file.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File file, String name) {
                return ((name.endsWith(".jpg")) || (name.endsWith(".png")) || (name.endsWith(".mp4")));
            }
        });

        if (imagelist==null && imageList.length == 0) {
            Toast.makeText(this, "No Captured Images", Toast.LENGTH_SHORT).show();
        } else {

            for (int i = 0; i < imageList.length; i++) {

                imgBtnThumbnail.setImageURI(Uri.parse(imageList[i].getAbsolutePath()););
            }
        }
    }

try this; but your image changes continuously because of for loop;

Upvotes: 0

faranjit
faranjit

Reputation: 1627

File[] imageList = file.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File file, String name) {
            return ((name.endsWith(".jpg")) || (name.endsWith(".png")) || (name.endsWith(".mp4")));
        }
    });

if (imageList.length = 0) {
        Toast.makeText(this, "No Captured Images", Toast.LENGTH_SHORT).show();
    } else {
        String[] mFiles = new String[imageList.length];

        for (int i = 0; i < imageList.length; i++) {
            mFiles[i] = imageList[i].getAbsolutePath();
        }
        mUrls = new Uri[mFiles.length];

        for (int i = 0; i < mFiles.length; i++) {
            mUrls[i] = Uri.parse(mFiles[i]);
            imgBtnThumbnail.setImageURI(mUrls[i]);
        }
    }

Upvotes: 2

reaven
reaven

Reputation: 91

Do not initialize mFiles. Initialize it in the moment when you add some data. And for checking if it isn't empty use this:

if (mFiles != null && mFiles.length > 0) {
    ...
}

Upvotes: 0

Related Questions