2

I would like to obtain the byte array of a JPEG image without using the following method:

    bitmap = BitmapFactory.decodeFile("/sdcard/photo.jpg");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 50, baos);
    byte[] data = baos.toByteArray();

Is there anyway to do this?

Zerhinne
  • 1,987
  • 2
  • 22
  • 43

2 Answers2

3

Any reason not to just load the file itself as a normal FileInputStream etc? (Personally I like Guava's Files.toByteArray() as a simple way of loading a file, but I don't know the status of Guava on Android.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • May I know how do I achieve that? – Zerhinne Sep 13 '11 at 08:37
  • 3
    @androidnoob: Do you know how to read from a file at all? I would strongly suggest reading a book on core Java (collections, streams etc) before trying to go much further, to be honest. I'm not trying to be rude, but it's really important to have a good grasp of the basics of a platform before trying anything serious, and a Q&A site like StackOverflow isn't the best way to get that knowledge. – Jon Skeet Sep 13 '11 at 08:39
  • @androidnoob, for reading a file into byte array, I (also) think you're best off using a utility method from a library like [Guava](http://code.google.com/p/guava-libraries/) or [Commons IO](http://commons.apache.org/io/). (If the whole lib is too big, you could just take the relevant classes.) See e.g. [this question](http://stackoverflow.com/questions/6058003/beautiful-way-to-read-file-into-byte-array-in-java) for some examples. – Jonik Sep 13 '11 at 10:12
  • Thanks, figured out how to do it myself – Zerhinne Sep 14 '11 at 06:22
1

If you consider it as a normal file type, then it would solve your problem.

here is the code

File file = new File("/sdcard/download/The-Rock2.jpg");
byte[] bytes = getBytesFromFile(file);



public byte[] getBytesFromFile(File file) {
    byte[] bytes = null;
    try {

        InputStream is = new FileInputStream(file);
        long length = file.length();

        bytes = new byte[(int) length];

        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
            offset += numRead;
        }

        if (offset < bytes.length) {
            throw new IOException("Could not completely read file " + file.getName());
        }

        is.close();
    } catch (IOException e) {
                  //TODO Write your catch method here
    }
    return bytes;
}
NyanLH
  • 480
  • 1
  • 6
  • 17
  • 2
    An empty catch block, catching all exceptions? Ick. – Jon Skeet Sep 13 '11 at 08:39
  • 2
    So you're *inviting* someone who apparently doesn't know how to use streams to copy code which follows *very bad practice* (oh, and I've just noticed you don't close the file on exception), without including *any* sort of warning on it? Double ick. – Jon Skeet Sep 13 '11 at 08:53
  • Also, it doesn't make much sense to throw an IOException with an error message if you enclose it in a try-catch which silently ignores everything, including that exception... – Jonik Sep 13 '11 at 09:25