Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a BufferedImage from a byte array java
    primarykey
    data
    text
    <p>I have a byte array and I need to convert this to a image. I've read this tutorial:</p> <p><a href="http://www.mkyong.com/java/how-to-convert-byte-to-bufferedimage-in-java/" rel="nofollow">http://www.mkyong.com/java/how-to-convert-byte-to-bufferedimage-in-java/</a></p> <p>This tutorial uses the <code>BufferedImage</code> and <code>ImageIO</code> which is in this import statement</p> <pre><code>import javax.imageio.ImageIO; import java.awt.image.BufferedImage; </code></pre> <p>But I can't find these classes anywhere. I have the JDK x64 version with the javax installed. Anybody? </p> <p>My whole source code:</p> <pre><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 &gt; 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 &lt; bytes.length &amp;&amp; (numRead=is.read(bytes, offset, bytes.length-offset)) &gt;= 0) { offset += numRead; } // Ensure all the bytes have been read in if (offset &lt; 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); } </code></pre> <p>}</p> <p><strong>Explanation of the code</strong></p> <p>in the function <code>getBytesFromFile</code> 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 <code>convertByteArray</code> 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. </p> <p>Anybody have any solution to this? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload