user1681497
user1681497

Reputation: 1

File object to byte array - android development

I need to convert this File object to byte array:

File directory=new File(Environment.getExternalStorageDirectory() + "");

(I need only the names of folders and files on SDcard.)

I have already tried this:

 byte[] send=null;
            FileInputStream fis;
            try {
                fis = new FileInputStream(directory);

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int readBytes = 0;
            while(readBytes != -1)
            {

                    readBytes = fis.read(buffer);

                if(readBytes > 0)
                {
                    bos.write(buffer, 0, readBytes);
                }
                else 
                    break;
            }
            byte[] fileData = bos.toByteArray();
            send=fileData;

But it returns this error: java.io.FileNotFoundException: /mnt/sdcard (Is a directory)

Upvotes: 0

Views: 1340

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502206

You're trying to load the directory as if it were a file. It's not. What would you expect the contents of the byte array to be?

If you want to find the list of files in a directory, use File.listFiles().

Upvotes: 1

Related Questions