Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8175977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:58:40+00:00 2026-06-06T22:58:40+00:00

I have a byte array and I need to convert this to a image.

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:58:42+00:00Added an answer on June 6, 2026 at 10:58 pm

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a byte array, received from java application via network. I need to
I have a java.awt.Image that I need to add CCITT T.6 compression and convert
In my project I have an bitmap image. I need to convert this picture
I have a number of byte[] array variables I need to convert to string
I have a bin file that I need to convert to a byte array.
I have a hexidecimal string that I need to convert to a byte array.
I have a base32 string which I need to convert to a byte array.
Part 1) I have a bitmap/jpeg image. I need to convert this image into
How to Convert a byte array into an int array? I have a byte
How do I convert a structure to a byte array in C#? I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.