[英]How to replace downloaded image with same file name in storage from server?
I implement the function in which user can save image from the server.我实现了用户可以从服务器保存图像的功能。 Here is my code.这是我的代码。
BasicImageDownloader imageDownloader = new BasicImageDownloader(new BasicImageDownloader.OnImageLoaderListener()
{
@Override
public void onError(BasicImageDownloader.ImageError error)
{
Toast.makeText(mContext, "Error code " + error.getErrorCode() + ": " + error.getMessage(), Toast.LENGTH_LONG).show();
}
@Override
public void onProgressChange(int percent)
{
}
@Override
public void onComplete(Bitmap result)
{
final Bitmap.CompressFormat mFormat = Bitmap.CompressFormat.JPEG;
final File myImageFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator
+ "Android" + File.separator
+"data" +File.separator
+"test" +File.separator
+ "profileimage" +File.separator
+ mPost.get(i).getUid() + "." + mFormat.name().toLowerCase());
BasicImageDownloader.writeToDisk(myImageFile, result, new BasicImageDownloader.OnBitmapSaveListener() {
@Override
public void onBitmapSaved() {
Toast.makeText(mContext, "Image saved as: " + myImageFile.getAbsolutePath(), Toast.LENGTH_LONG).show();
}
@Override
public void onBitmapSaveError(BasicImageDownloader.ImageError error) {
Toast.makeText(mContext, "Error code " + error.getErrorCode() + ": " +
error.getMessage(), Toast.LENGTH_LONG).show();
error.printStackTrace();
}
}, mFormat, false);
}
});
imageDownloader.download("link", true);
It works great but whenever the same name exists in the storage location .它工作得很好,但只要存储位置中存在相同的名称。 It throw the exception:"file is already exists".它抛出异常:“文件已经存在”。 Now the problem is how to replace downloaded image whenever same file exists in the storage location?现在的问题是,只要存储位置中存在相同的文件,如何替换下载的图像?
I found the BasicImageDownloader class on the web.我在网上找到了 BasicImageDownloader 类。 The last parameter of writeToDisk is for overwriting. writeToDisk 的最后一个参数用于覆盖。 So if you set it to true like this:因此,如果您像这样将其设置为 true:
BasicImageDownloader.writeToDisk(..., true);
it should overwrite the file.它应该覆盖文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.