保存的图像将覆盖sd卡android中的上一个图像
在我的应用程序中,我允许用户从Url链接下载大量图像,并将它们存储在SD卡中。每次下载的新映像都会覆盖具有相同名称的前一映像。所以最后,我在SD卡上只有一个图像是最后一次下载的保存的图像将覆盖sd卡android中的上一个图像,android,image,download,android-sdcard,Android,Image,Download,Android Sdcard,在我的应用程序中,我允许用户从Url链接下载大量图像,并将它们存储在SD卡中。每次下载的新映像都会覆盖具有相同名称的前一映像。所以最后,我在SD卡上只有一个图像是最后一次下载的 try { URL url = new URL(src); URLConnection connection = (URLConnection) url.openConnection(); connection.setDoInput(tru
try {
URL url = new URL(src);
URLConnection connection = (URLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(input);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
String filename;
Date date = new Date(0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
filename = sdf.format(date);
File file = new File(Environment.getExternalStorageDirectory() + File.separator + filename + ".jpg");
try {
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
Toast.makeText(getApplicationContext(),filename,Toast.LENGTH_LONG).show();
// 5
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
我需要帮助存储我下载的所有图像 试试这个
try this:
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
filename = sdf.format(date+ n);
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
getString(R.string.images));
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";
File file = new File(mediaStorageDir, fname);
您为图像命名相同,因此出现了此问题。使用随机生成器给出图像的名称。请建议我怎么做?简单地使用时间戳生成文件名。url的外观如何?它们将包含图像名称?那就用这个名字<代码>文件.createNewFile()代码>。删除该声明。该文件将由新的FileOutputStream创建。