[英]android save bitmap in sd card
i save the bitmap in sd card with the following code 我使用以下代码将位图保存在sd卡中
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image" +".jpg";
// String imageInSD1 = Environment.getExternalStorageDirectory().getAbsolutePath() +"/saved_images/" + fname;
File file = new File (myDir, fname);
// if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap_profile1.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
its working fine but issue is this i am doing this in loop which run every 5 sec the image slowly slowly demage . 它的工作正常,但问题是我正在循环执行此操作,每5秒运行一次,图像会慢慢慢慢损坏。 i want to save the image only one time how can i check this if image is already in sd card then do not save the image i have only one name for image image change but its name remain same please tell i want to save the one image for only one time if bitmap value change then image save othervise image not save. 我只想保存图像一次,如果图像已经在sd卡中,那么该如何保存呢?不要保存图像。我只有一个图像更改名称,但名称保持不变,请告诉我要保存一个图像如果位图值更改仅一次,则图像保存,而其他图像则不保存。
The function you're looking for is file.exists() 您正在寻找的功能是file.exists()
After creating your File file, go 创建文件文件后,转到
if(!file.exists())
try{...
....
Edit: Full code 编辑:完整代码
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image" +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) // only try to save picture if it doesn't exist already
try {
FileOutputStream out = new FileOutputStream(file);
bitmap_profile1.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.