0

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.

8
  • 1
    You are initializing mFiles array of size 0. String[] mFiles = new String[0] Commented Feb 3, 2017 at 6:07
  • Yes...i did it bcz...i read about array initialization should start with 0. Commented Feb 3, 2017 at 6:09
  • ^ :D. It's the size of array. If you initialize array with new String[100], it will start from 0 and will end at 99. Commented Feb 3, 2017 at 6:11
  • 3
    if (mFiles.length >= 0) { - you're checking something strange. Length of an array is always non-negative. However, in this case it's even stranger because you check mFiles's size, and mFiles had no way of changing after you've created it. I think you wanted to check imageList.length == 0 Commented Feb 3, 2017 at 6:12
  • Just let it null . Check if null then show toast else do something with image Commented Feb 3, 2017 at 6:13

4 Answers 4

2
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]);
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

here what i use.....if (imageList.length = 0)...i think here == .in if()
@faranjit..Thanks Man also your Answer is working Properly....but i can not marked 2 Question..thats why vote up.
1

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

7 Comments

Ethan Choi....your answer is very very Cool and works very Fine...like a works Like a Charm...thanks You saved my Time.
can you tell me the meaning of this Line..... int imgLength = imageList == null ? 0 : imageList.length;
and what if i used single for() condition.instead of 2 for() condition...because of both are same..?
int imgLength = imageList == null ? 0 : imageList.length is meaning if imageList is null then imgLength is zero else image.length
then i can put the both condition code into single one..?
|
0

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

2 Comments

@reaven...i did as you say.....but when i am not initialize mFiles..Android Studio given me Red Line above mFiles....and said Variable mFiles might not have been initialized.
So initialize it with null first: String[] mFiles = null; It won't show red line that way.
0
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;

3 Comments

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.devkrushna.camerademo/com.devkrushna.camerademo.MainActivity}: java.lang.NullPointerException: Attempt to get length of null array.............shown me error
Probably your imageList is null
check it. I have edittedt it. but your imagelist is null @Sagar Aghara

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.