开发者

Convert View to bitmap FileNotFoundException problem

开发者 https://www.devze.com 2023-01-14 19:51 出处:网络
I\'m trying to convert my LinearLayout to a b开发者_Python百科itmap so that I can save the current layout content as an image in SD card. First, I create bitmap and canvas and attach the layout to the

I'm trying to convert my LinearLayout to a b开发者_Python百科itmap so that I can save the current layout content as an image in SD card. First, I create bitmap and canvas and attach the layout to the canvas. Followed steps from http://www.brighthub.com/mobile/google-android/articles/30676.aspx#comments.

//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);   

When I press the save button, it should save the current layout as an image to SD card. Here are my steps:

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);

It can create folder in SD card but no file created under this folder. It always gives me FileNotFoundException. I'm not sure it is the file creating problem or the screenBitmap problem. Can anyone give me some clue? Thanks!


Have you enabled the right permissions in the Android Manifest? i.e. android.permission.WRITE_EXTERNAL_STORAGE. I was getting the same FileNotFoundException when trying to save to SD before adding the permission.


After

File outputfile = new File(file, filename);

Insert this:

outputfile.createNewFile();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号