0

I have a byte array and I need to convert this to a image. I've read this tutorial:

http://www.mkyong.com/java/how-to-convert-byte-to-bufferedimage-in-java/

This tutorial uses the BufferedImage and ImageIO which is in this import statement

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

But I can't find these classes anywhere. I have the JDK x64 version with the javax installed. Anybody?

My whole source code:

public class ReadImageFromFileActivity extends Activity implements OnClickListener {

Context c;
Button read;
ImageView image;
byte[] fileBytes;
byte[] image1;
byte[] image2; 


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    c = getApplicationContext(); 

    read = (Button)findViewById(R.id.file); 
    read.setOnClickListener(this);

    image = (ImageView)findViewById(R.id.image);

    image1 = new byte[ 500 * 500]; 
    image2 = new byte[ 500 * 500]; 

}



public static byte[] getBytesFromFile(Context c) throws IOException {

    File file = new File("/mnt/sdcard/LeftIndex.FIR");
    Uri URIpath = Uri.fromFile(file);

    InputStream is = c.getContentResolver().openInputStream(URIpath);

    long length = file.length();


    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 < bytes.length
           && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
        offset += numRead;
    }

    // Ensure all the bytes have been read in
    if (offset < bytes.length) {
        throw new IOException("Could not completely read file "+file.getName());
    }

    // Close the input stream and return bytes
    is.close();
    return bytes;
}


public void onClick(View v) {

    try {
        fileBytes = getBytesFromFile(c);
        convertByteArray(fileBytes); 

    } catch (IOException e) {
        Log.e("++++getBytesFromFile SAYS: ", "Couldn't read file!!!");

        e.printStackTrace();
    }

}


public void convertByteArray(byte[] buffer) {

     System.arraycopy(buffer, 64, image1, 0, 500 * 500);  
     System.arraycopy(buffer, 60, image2, 0, 500 * 500); 



     Bitmap bmp = BitmapFactory.decodeByteArray(image1, 0, image1.length); 
     ImageView image=new ImageView(this);
     image.setImageBitmap(bmp);



}

}

Explanation of the code

in the function getBytesFromFile I construct a new File out of a .FIR file at my SD-card. This file will be converted to a Byte array and returns the constructed byte array. This byte-array will be passed into a function called convertByteArray which will be splitted into two separate Byte arrays. From each of these two byte arrays I want to construct a Bitmap. But logcat only says that the bitmapFactory returns null.

Anybody have any solution to this?

3
  • Yeah, thats right. I have a byte array which I want to create a image out of. Commented Jul 5, 2012 at 12:08
  • those 2 imports are part of your JDK. They should be there. If not try reinstalling the JDK Commented Jul 5, 2012 at 12:09
  • I can't understand what are you trying to do? You have a byte array with image inside and you want to convert it to some another format, or what? Commented Jul 5, 2012 at 12:14

1 Answer 1

4

Android does not have neither ImageIO nor BufferedImage. Here is an alternative: What is the best way to serialize an image (compatible with Swing) from Java to Android?

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

1 Comment

@TobiasMoeThorstensen You forgot the BitmapFactory.Options :)

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.