2012-07-05 128 views
0

我有一个字节数组,我需要将其转换为图像。我读过这个教程:从字节数组创建BufferedImage java

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

本教程使用BufferedImageImageIO这是在这个import语句

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

但我不能在任何地方找到这些类。我有安装了javax的JDK x64版本。任何人?

我的整个源代码:

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); 



} 

}在功能getBytesFromFile代码

说明我在SD-建立一个新的文件了.FIR文件卡。该文件将被转换为Byte数组并返回构造的字节数组。这个字节数组将被传入一个名为convertByteArray的函数,该函数将被分成两个独立的Byte数组。从这两个字节数组中的每一个我都想构建一个位图。但logcat只是说,bitmapFactory返回null。

有人有任何解决方案吗?

+0

你在使用Android吗? – 2012-07-05 12:06:19

+0

是的,那就对了。我有一个字节数组,我想创建一个图像。 – 2012-07-05 12:08:41

+0

这2个导入是您的JDK的一部分。他们应该在那里。如果不尝试重新安装JDK – MaVRoSCy 2012-07-05 12:09:18

回答