2

I have two activities: In the first activity I am passing the String "pathName" to a second activity. I am receiving the extra okay and I can create a File from this path name. The problem arises when I try to call .listFiles() from that file.

Code: First Activity

    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            String pathToSend = albumDirectory+albumList.get(position);
            Intent i = new Intent(getBaseContext(),GalleryImageSlider.class);
            i.putExtra("pathName", pathToSend);
            startActivity(i);
        }

Second Activity:

    List<File> imageList;

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_capture_gallery);

    Intent i = getIntent();
    filePath = i.getExtras().getString("pathName");
    albumFile = new File(filePath);

    Log.d("Fragment","File Path: " + albumFile.getAbsolutePath());
            //I can see this directory on my tablet, and I can see the files within. So the file
            path is correct

    try{
        imageList = Arrays.asList(albumFile.listFiles());

    }
    catch(NullPointerException e){
        Log.e("Error", "NPE: " + e);
    }

I understand that .listFiles returns null if there are no files to be read. But there are files, and I can see them when I look in my tablets file manager.

Anyone see a way to resolve this? Thanks before hand.

1
  • Could you post the stack trace? Commented Aug 9, 2013 at 11:16

3 Answers 3

2

Maybe the problem is because the path isn't a directory or you don't have permissions to read it. Here are some tips of methods that you should verify before call listFiles():

  1. file.canRead() - returns a bool that says if the file is readable or not.
  2. file.isDirectory() - returns a bool that says if the file is a directory or not.

Since you said that the path is correct, probably the second option isn't the solution, but the first one can be.

Sign up to request clarification or add additional context in comments.

1 Comment

Ended up referencing the file in a different way which worked. But I'll mark you correct since I am going to add these methods for error checking. Thanks
0

You could be possibly missing the PERMISSON to do so, listFiles() return null is some cases such the directory can not be found or you dont have permission to access to that directory. Check your PERMISSION in AndroidManifest.xml. Have you add the READ_EXTERNAL_STORAGE for the permission? Hope this helps.

Comments

0

When you connect your device to PC sd card is removed and you can't access files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.