0

I am creating one directory i.e file and storing the bitmap images into that file,now how to convert it into byte array

File myDir = new File(root + "/saved_images");      
                myDir.mkdirs();
                Random generator = new Random();
                int n = 10000;
                n = generator.nextInt(n);
                String fname = "Image-"+ n +".jpg";
                File file = new File (myDir, fname);

                if (file.exists ()) file.delete (); 
                try {
                    FileOutputStream out = new FileOutputStream(file);

                    bmp.compress(Bitmap.CompressFormat.JPEG, 90, out);

                    out.flush();
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();

                }
user1522869
  • 109
  • 2
  • 13
  • 1
    Possible duplicate: http://stackoverflow.com/questions/6058003/beautiful-way-to-read-file-into-byte-array-in-java – sdabet Sep 03 '12 at 12:00
  • [See Link-1](http://www.roseindia.net/answers/viewqa/JSP-Servlet/26031-Servletoutputstream-and-Bytearrayoutputstream.html) AND [Also See this](https://www.google.co.in/#hl=en&sclient=psy-ab&q=convert+jpg+file+into+byte+array+java+roseindia&oq=convert+jpg+file+into+byte+array+java+roseindia&gs_l=hp.3...220204.221187.3.221637.4.4.0.0.0.2.758.1612.0j2j5-1j1.4.0...0.0...1c.y6ah6u3Cd_U&pbx=1&bav=on.2,or.r_gc.r_pw.r_qf.&fp=ecec44c8cfc02f2d&biw=1173&bih=807) – Chintan Raghwani Sep 03 '12 at 12:04

4 Answers4

0

Not exactly sure what you're trying to do, but you can try something like:

InputStream is = ...
ByteArrayOutputStream buffer = new ByteArrayOutputStream();

int nRead;
byte[] data = new byte[some huge number, power of 2 preferably];

while ((nRead = is.read(data, 0, data.length)) != -1) {
  buffer.write(data, 0, nRead);
}

buffer.flush();

byte[] byteArray = buffer.toByteArray();
LuxuryMode
  • 33,401
  • 34
  • 117
  • 188
0

Just Use this to read the file where you kept.

// Returns the contents of the file in a byte array.
public static byte[] getBytesFromFile(File file) throws IOException {
    InputStream is = new FileInputStream(file);

    // Get the size of the file
    long length = file.length();

    // You cannot create an array using a long type.
    // It needs to be an int type.
    // Before converting to an int type, check
    // to ensure that file is not larger than Integer.MAX_VALUE.
    if (length > Integer.MAX_VALUE) {
        // File is too large
    }

    // Create the byte array to hold the data
    byte[] bytes = new byte[(int)length];

    // Read in the bytes
    int offset = 0;
    int numRead = 0;
    while (offset = 0) {
        offset += numRead;
    }

    // Ensure all the bytes have been read in
    if (offset 

Courtesy : http://www.exampledepot.com

Ajay Kumar Meher
  • 1,932
  • 16
  • 24
0

If you just want to modify your existing code to write the image to a byte array instead of a file, then replace the try block with this code:

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 90, out);
    bytes = out.getBytes();

... where bytes has type byte[], and get rid of the code that generates the filename and deletes the existing file if it exists. Since you writing to a ByteArrayOutputStream, there is not need to call flush() or close() on out. (They won't do anything.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

I have used this code for converting image file into byte araay,

Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.abc);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.JPEG, 40 , bos);
            public byte[] bitmapdata = bos.toByteArray();
            Log.w("Image Conversion", String.valueOf(bitmapdata.length));
            String converted_txt="";
            for (int i = 0; i < bitmapdata.length; i++) 
           { 
               Log.w("Image Conversion", String.valueOf(bitmapdata[i]));
               ba = bitmapdata[i];
               converted_txt=converted_txt+bitmapdata[i]; 
           }
           try 
           {
               File myFile = new File("/sdcard/myImageToByteFile.jpg");
               myFile.createNewFile();
               fOut = new FileOutputStream(myFile);
               OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
               myOutWriter.write(ba);
               myOutWriter.close();
               fOut.close();
           }
           catch (Exception e) 
           {
               Toast.makeText(getApplicationContext(), e.getMessage(),5000).show();
           }
Sahil Mahajan Mj
  • 11,033
  • 8
  • 53
  • 100