2011-05-22 79 views
0
try { 
FileOutputStream out = new FileOutputStream("p1"); 
pictureTaken.compress(Bitmap.CompressFormat.PNG, 90, out); 
out.close(); 
} catch (Exception e) { 
e.printStackTrace(); 
} 

嘗試的FileInputStream和FileOutputSteam都拋出異常

 case R.id.open: 
     ImageView im = (ImageView) findViewById(R.id.im); 
     try { 
      FileInputStream in = new FileInputStream("p1"); 
      BufferedInputStream buf = new BufferedInputStream(in); 
      byte[] bitMapA= new byte[buf.available()]; 
      buf.read(bitMapA); 
      Bitmap bM = BitmapFactory.decodeByteArray(bitMapA, 0, bitMapA.length); 
      im.setImageBitmap(bM); 
      if (in != null) { 
      in.close(); 
      } 
      if (buf != null) { 
      buf.close(); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     break; 

兩個嘗試,但他們沒有在去錯誤傳遞,他們只是到了抓...我得到大多數零件在線並根據我的需要修改它們,但即便如此,這一切都是有道理的,並在我的腦海中起作用。只是不明白爲什麼會拋出異常。

+1

什麼是拋出的異常? – verdesmarald 2011-05-22 05:37:30

回答

2

聽起來就像它在Android上,並從您正在使用的路徑(p1),你只是試圖保存到應用程序的運行文件夾中的文件。一般來說,你不能寫入任何目錄。你會想要做這樣的事情:

FileOutputStream out = openFileOutput ("p1", MODE_WORLD_WRITABLE); 
第一個代碼塊

,然後:

FileInputStream in = openFileInput("p1"); 
在第二個代碼塊

+0

Femi是正確的,在Android中 - 您需要使用openFileInput/openFileOutput來獲取有效的Stream對象。 – 2011-05-22 07:36:11

+0

好的,謝謝,我被http://developer.android.com網站弄糊塗了,誤導了他們說這是保存和打開的方式。再一次,謝謝! – Mark 2011-05-24 06:07:09

+0

等一下,我還有一個問題,如果我想將圖片和其他文件保存到公共文件夾(可以在與usb連接時看到),該怎麼辦。我仍然使用輸入/輸出流嗎?另外,您可以通過壓縮來保存圖片,還有其他寫入方式(音頻,視頻等)。對不起,我只是新的Android開發= P。 – Mark 2011-05-26 04:30:46