2010-09-02 42 views
0

我试图将我的LinearLayout转换为位图,以便可以将当前布局内容另存为SD卡中的图像。首先,我创建位图和画布并将布局附加到画布上。遵循http://www.brighthub.com/mobile/google-android/articles/30676.aspx#comments的步骤。将视图转换为位图FileNotFoundException问题

//code to add child view into layout before creating bitmap 
screenBitmap = Bitmap.createBitmap(200,200,Bitmap.Config.ARGB_8888); 
Canvas canvas = new Canvas(screenBitmap); 
layout.draw(canvas); 

当我按下保存按钮,它应该在当前布局保存为图像,以SD卡。下面是我的步骤:

FileOutputStream outStream = null; 
File file = new File("/sdcard/Health Management System/"); 
file.mkdirs(); 

File outputFile = new File(file, fileName); 
outStream = new FileOutputStream(outputFile); 
BufferedOutputStream bos = new BufferedOutputStream(outStream); 

bos.flush(); 
bos.close(); 

screenBitmap.compress(Bitmap.CompressFormat.PNG, 100,bos); 

它可以创建在SD卡的文件夹,但没有这个文件夹下创建的文件。它总是给我FileNotFoundException。我不确定这是文件创建问题还是screenBitmap问题。任何人都可以给我一些线索吗?谢谢!

回答

0

File outputfile = new File(file, filename); 

后插入此:

outputfile.createNewFile(); 
+0

现在它给出IOException,文件的父目录不存在。但是我可以看到在SD卡中创建的父目录啊?为什么? – Wen 2010-09-03 02:09:38

+0

好的,我犯了一个非常愚蠢的错误。我以错误的方式定义了文件名。我把“/”放在文件名中,不被接受。删除后,现在一切正常。 :) – Wen 2010-09-03 03:12:07

1

您已在Android清单正确的权限?即android.permission.WRITE_EXTERNAL_STORAGE。 在添加权限之前尝试保存到标清时,我收到了相同的FileNotFoundException。

+0

雅,我补充说。 – Wen 2010-09-03 02:02:14

+0

这是文件名问题。 – Wen 2010-09-03 03:12:36