2012-07-05 82 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

回答