Vora :
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?
somega :
I found the BasicImageDownloader class on the web. The last parameter of writeToDisk is for overwriting. So if you set it to true like this:
BasicImageDownloader.writeToDisk(..., true);
it should overwrite the file.