2010-12-03 56 views
0

在我的項目中,我有一個圖片,通過程序,我提取像素,操縱這些像素,然後將其保存在我的包中。是我救了它的方式是:Android:解碼位圖圖像的問題

private void createPicture() 
{ 
    System.out.println("Inside createPicture"); 
    System.out.println(contextPath); 
    try{ 
     FileOutputStream fos = new FileOutputStream(contextPath + "/" + picName); 
     DataOutputStream dos = new DataOutputStream(fos); 

     for(int i=0; i<splitedPixel.length; i++) 
      dos.writeByte(splitedPixel[i]); 

     dos.flush(); 
     dos.close(); 
    } 
    catch(IOException e){ 
     System.out.println("IOException : " + e); 
    } 
    System.out.println("Picture Created."); 
} 

所以,在此目錄

/data/data/my_package_name/files/myPic.bmp

中保存我的圖片現在,我想閱讀這張新照片並提取這張照片的像素。我用過這種方法:

public Stego(Context context) 
{ 
    //Instantiate an ImageView and define its properties 
    contextPath = context.getFilesDir().getAbsolutePath(); 
    image = BitmapFactory.decodeFile(contextPath + "/" + picName); 

    System.out.println("Image= " + image); 
    imWidth = image.getWidth(); 
    imHeight = image.getHeight(); 
    getMaxMessageChars(); 
    System.out.println("Width: " + imWidth); 
} 

程序在這裏崩潰。 logcat指示圖像的結果爲空,並且程序達到getWidth崩潰時。

這些書寫和閱讀方式是否正確? 我可以通過模擬器轉到目錄來檢查是否創建了這張圖片嗎?

感謝

回答

0

我想你應該在createPicture()方法關閉fos流。

BitmapFactory.decode*()方法返回null出現錯誤。所以最好在解碼後檢查null

據我所知,只能在模擬器或根設備上從Android內部存儲器保存文件。這可以使用Eclipse的DDMS透視圖或Android SDK工具鏈中的ddms實用工具完成。只需點擊設備 - >文件資源管理器並從設備中拉出文件。

如果您想從非根設備獲取文件,最好將其保存到外部存儲設備(sdcard)。在這種情況下,您可以將設備連接到電腦,如外置硬盤。