【发布时间】:2015-09-19 07:11:06
【问题描述】:
我想从服务器下载图片。我尝试过使用下面的代码,但只下载 .png 格式的图片,仍然是 .jpeg、.gif、.bmp 等不是。有人可以帮助我如何在我的 Android 应用程序中下载所有类型的图像格式。在此先感谢。
这是我的下载方法
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();
}
}
这是我检查的另一种方法,如果条件图像以 png、gif、bmp 和 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();*/
}
【问题讨论】:
-
所以你想下载所有的图片并在以后从他们那里查询scard中的内容还是什么?
-
您是否在未阅读的情况下从某处复制粘贴了这些代码?第一个明确表示以 png 格式保存图像,第二个使用名为“Download_mp3file”的函数下载 images。
标签: android