2010-12-04 96 views
0

我想複製asstets文件夾中的圖片並將其粘貼到我的包中,僅供測試。但最終的形象不顯示任何東西。當我想用油漆打開圖片時,它會說「這不是有效的位圖文件」。Android:複製並粘貼圖像不起作用!

在我的節目,我開始用這種方法來讀取原始圖像:

private void copyImage() 
{ 
    AssetManager am = getResources().getAssets();    
    try{ 
     image = BitmapFactory.decodeStream(am.open("tasnim.png")); 
     imWidth = image.getWidth(); 
     imHeight = image.getHeight(); 
    } 
    catch(IOException e){ 
     e.printStackTrace(); 
     System.out.println("Error accoured!"); 
    } 
} 

接下來,我會得到圖像的像素或提取像素,並保存像素的整數(rgbstream)的陣列。

private void getPixelsOfImage() 
{ 
    rgbStream = new int[imWidth * imHeight]; 
    image.getPixels(rgbStream, 0, imWidth, 0, 0, imWidth, imHeight); 
} 

最後,我想將它保存在我的包,

private void createPicture() 
{ 
    contextPath = context.getFilesDir().getAbsolutePath(); 
    String path = contextPath + "/" + picName; 

    try{ 
     FileOutputStream fos = new FileOutputStream(path); 
     DataOutputStream dos = new DataOutputStream(fos); 

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

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

代碼工作正常,但結果是,沒有! :( 當我檢查DDMS它創建新的文件和存儲所有像素(因爲它顯示這個文件的大小是13300和我的原始圖片的尺寸是100 * 133)。當我點擊「從設備拉文件」我可以將它保存在我的桌面上,但是,當我打開它時,沒有任何東西。

你覺得呢?我的代碼中有沒有問題? 謝謝

+0

我幾乎肯定,這不是PNG的工作方式。 – Falmarri 2010-12-04 05:39:38

+0

你能解釋一下「什麼都沒有」的意思嗎?有文件嗎?它有多大?當你看十六進制轉儲時,有什麼內容?你打開它怎麼樣? – EboMike 2010-12-04 05:40:49

回答

1

我不知道你的意圖是什麼 - 你想寫出一個原始圖像文件?

假設你想要寫一個JPEG或PNG或什麼的,你可以刪除你的整個代碼,做一些輕鬆了不少:

Bitmap image = BitmapFactory.decodeStream(am.open("tasnim.png")); 
FileOutputStream fos = new FileOutputStream(path); 
image.compress(Bitmap.CompressFormat.PNG, 100, fos); 

當然了適當的錯誤檢查。