保存的图像覆盖了 SD 卡中的先前图像 android
Saved image is overriding the previous image in sd card android
在我的应用程序中,我允许用户从 Url 链接下载大量图像并将它们存储在 SD 卡中。每次下载的新图像都会覆盖以前同名的图像。所以最后我在SD卡中只有一张图片是最后下载的。
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 链接下载大量图像并将它们存储在 SD 卡中。每次下载的新图像都会覆盖以前同名的图像。所以最后我在SD卡中只有一张图片是最后下载的。
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);