I want to download images from server.I have tried with below code but only .png format images are download , remain .jpeg, .gif, .bmp etc are not. Can someone help me how to download all type of image format's in my Android Application.Thanks in advanced.
This is my Download method
void download_PngFile(String fileUrl)
{
Log.e("In download_PngFile ", " str_imgList_imageaudioPath = " + imageName);
Bitmap imagenObtenida = null;
try {
URL ImgUrl = new URL(fileUrl);
HttpURLConnection conn = (HttpURLConnection) ImgUrl.openConnection();
conn.connect();
imagenObtenida = BitmapFactory.decodeStream(conn.getInputStream());
Log.e("imagenObtenida", " = " + imagenObtenida);
String fotoname = imageName;
File file = new File(newFolder, fotoname);
int sizeOfImage = (int) file.length();
Log.e("sizeOfImage "," = "+ sizeOfImage +"@ "+ imageName);
if (file.exists()) file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
imagenObtenida.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
Log.e("Png = ","DownLoad complete");
} catch (Exception e) {
}
} catch (IOException e) {
e.printStackTrace();
}
}
Here is my another method which i checked in if condition images are ends with png, gif, bmp, and jpeg
public void getDoenLoaddata()
{
Log.e("Record is exists !!", " == ");
//CreateFile();
dbhelper = new MyDbHelper(this);
SQLiteDatabase db1 = dbhelper.getReadableDatabase();
Cursor cursor = db1.rawQuery("select * from ActivityObjectList", null);
if (cursor.moveToFirst())
{
do
{
imageName = cursor.getString(cursor.getColumnIndex("imageaudioPath"));
String strDownLoadStatus = cursor.getString(cursor.getColumnIndex("DownLoad_Status"));
//Log.e("imageName ", " = " + imageName + " & strDownLoadStatus is " + strDownLoadStatus);
if (strDownLoadStatus.equalsIgnoreCase("0"))
{
Log.e("Insert", " Status One");
if (imageName.endsWith(mp3_Pattern))
{
String str_Mp3Url = "http://103.24.4.60/CLASSNK1/MobileService.svc/DownloadFile/FileName/"+imageName;
Log.e("str_ImageUrl#####", " = " + str_Mp3Url);
download_Mp3File(str_Mp3Url);
strDownLoadStatus = "1";
dbhelper.update_DownLoadStatus(imageName, strDownLoadStatus);
}
if (imageName.endsWith(png_Pattern))
{
Log.e("imageName", " Status One" + imageName);
String str_ImageUrl = "http://103.24.4.60/CLASSNK1/MobileService.svc/DownloadFile/FileName/"+imageName;
Log.e("png_Pattern ", " str_ImageUrl = " + str_ImageUrl);
download_PngFile(str_ImageUrl);
strDownLoadStatus = "1";
dbhelper.update_DownLoadStatus(imageName, strDownLoadStatus);
}
if (imageName.endsWith(jpg_pattern))
{
String str_Mp3Url = "http://103.24.4.60/CLASSNK1/MobileService.svc/DownloadFile/FileName/"+imageName;
Log.e("str_ImageUrl#####", " = " + str_Mp3Url);
download_Mp3File(str_Mp3Url);
strDownLoadStatus = "1";
dbhelper.update_DownLoadStatus(imageName, strDownLoadStatus);
}
if (imageName.endsWith(jpeg_pattern))
{
String str_Mp3Url = "http://103.24.4.60/CLASSNK1/MobileService.svc/DownloadFile/FileName/"+imageName;
Log.e("str_ImageUrl#####", " = " + str_Mp3Url);
download_Mp3File(str_Mp3Url);
strDownLoadStatus = "1";
dbhelper.update_DownLoadStatus(imageName, strDownLoadStatus);
}
if (imageName.endsWith(gif_pattern))
{
String str_Mp3Url = "http://103.24.4.60/CLASSNK1/MobileService.svc/DownloadFile/FileName/"+imageName;
Log.e("str_ImageUrl#####", " = " + str_Mp3Url);
download_Mp3File(str_Mp3Url);
strDownLoadStatus = "1";
dbhelper.update_DownLoadStatus(imageName, strDownLoadStatus);
}
if (imageName.endsWith(bmp_pattern))
{
String str_Mp3Url = "http://103.24.4.60/CLASSNK1/MobileService.svc/DownloadFile/FileName/"+imageName;
Log.e("str_ImageUrl#####", " = " + str_Mp3Url);
download_Mp3File(str_Mp3Url);
strDownLoadStatus = "1";
dbhelper.update_DownLoadStatus(imageName, strDownLoadStatus);
}
}
else if (strDownLoadStatus.equalsIgnoreCase("1"))
{
// Log.e("Nothind To","Here !! ");
}
} while (cursor.moveToNext());
} cursor.close();
db1.close();
/* db1.close();*/
}