问题
I'm trying to convert my LinearLayout to a bitmap 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!
回答1:
After
File outputfile = new File(file, filename);
Insert this:
outputfile.createNewFile();
回答2:
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.
来源:https://stackoverflow.com/questions/3629204/convert-view-to-bitmap-filenotfoundexception-problem