在sdcard android中保存图像覆盖(Saving image overwrite in sdcard android)

编程入门 行业动态 更新时间:2024-10-25 00:27:12
在sdcard android中保存图像覆盖(Saving image overwrite in sdcard android)

我使用以下代码保存图像

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 Lucifer

it 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.

更多推荐

本文发布于:2023-07-23 12:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1231632.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   android   sdcard   overwrite   image

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!