【发布时间】:2014-10-17 10:01:40
【问题描述】:
我正在开发下载图片的壁纸应用程序,但我遇到了图片替换问题。
每当我下载新图像时,它都会被以前的图像替换。
这是我的代码:
public void saveImageToSDCard(Bitmap bitmap) {
String dirname = "/Amazing Wallpapers/";
File myDir = new File(Environment
.getExternalStorageDirectory().getPath() + dirname);
myDir.mkdirs();
String str = currentUrl.substring(
currentUrl.lastIndexOf('/')+1,
currentUrl.length());
String fname = "Wallpaper-" + str + ".jpg";
File file = new File(myDir, fname);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
Toast.makeText(
_context,
_context.getString(R.string.toast_saved).replace("#",
"\"" + pref.getGalleryName() + "\""),
Toast.LENGTH_SHORT).show();
Log.d(TAG, "Wallpaper saved to:" + file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(_context,
_context.getString(R.string.toast_saved_failed),
Toast.LENGTH_SHORT).show();
}
}
【问题讨论】:
标签: android image android-image environment fileoutputstream