我使用以下代码保存图像
FrameLayout mainLayout = (FrameLayout) findViewById(R.id.frame); // File root = Environment.getExternalStorageDirectory(); // File file = new File(root, "androidlife.jpg"); // File file = new File(Environment.getExternalStorageDirectory() // + File.separator + "/test.jpg"); Random fCount = new Random(); // for (int i = 0; i < 10; i++) { Comment by Lucifer int roll = fCount.nextInt(600) + 1; //System.out.println(roll); File file = new File(Environment.getExternalStorageDirectory() + File.separator + "/test" + String.valueOf(roll) +".jpg" ); Bitmap b = Bitmap.createBitmap(mainLayout.getWidth(), mainLayout.getHeight(), Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); mainLayout.draw(c); FileOutputStream fos = null; try { fos = new FileOutputStream(file); if (fos != null) { b.compress(Bitmap.CompressFormat.JPEG, 90, fos); fos.close(); } } catch (Exception e) { e.printStackTrace(); } // } Comment by Lucifer它可以完美地保存图像,但是当我按两次保存按钮时会覆盖...可能是什么问题? 任何消化?
i'm using the following code for saving the image
FrameLayout mainLayout = (FrameLayout) findViewById(R.id.frame); // File root = Environment.getExternalStorageDirectory(); // File file = new File(root, "androidlife.jpg"); // File file = new File(Environment.getExternalStorageDirectory() // + File.separator + "/test.jpg"); Random fCount = new Random(); // for (int i = 0; i < 10; i++) { Comment by Lucifer int roll = fCount.nextInt(600) + 1; //System.out.println(roll); File file = new File(Environment.getExternalStorageDirectory() + File.separator + "/test" + String.valueOf(roll) +".jpg" ); Bitmap b = Bitmap.createBitmap(mainLayout.getWidth(), mainLayout.getHeight(), Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); mainLayout.draw(c); FileOutputStream fos = null; try { fos = new FileOutputStream(file); if (fos != null) { b.compress(Bitmap.CompressFormat.JPEG, 90, fos); fos.close(); } } catch (Exception e) { e.printStackTrace(); } // } Comment by Luciferit save the image perfectly but overwrite when i press the save button twice...What can be the issue? Any sugestion??
最满意答案
您已经提供了静态文件名。
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "/test.jpg");因此,每次它都会在test.jpg名称和同一位置创建一个图像。 您需要实现的唯一逻辑是将文件名更改为动态文件名。 你可以这样试试
static int fCount = 0;
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "/test" + String.valueOf(fCount++) +".jpg" );现在上面的行将每次创建一个新文件,从名称test0.jpg,test1.jpg开始......等等。
但是,当您关闭应用程序并重新启动应用程序时,这可能会产生问题。 因为它将从0计数器再次开始。
所以我建议你使用随机数字与文件名联系。
You have given a static file name.
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "/test.jpg");So, Everytime it is going to create a image with test.jpg name and at a same location. The only logic you need to implement is to change your file name to be a dynamic file name. You can try it in this way
static int fCount = 0;
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "/test" + String.valueOf(fCount++) +".jpg" );Now above line is going to create a new file every time, starting with name test0.jpg, test1.jpg ... and so on.
But this could create a problem when you close your application and restart your application. Because it will going to start again from 0 counter.
So i suggest you to go with a random number contacatination with file name.
更多推荐
发布评论