【发布时间】:2015-10-12 05:39:29
【问题描述】:
我正在从服务器下载图像并将其存储在我的 internal_storage 文件夹中。但是下载的图像大小为 (Width 800 * Height 534) 以像素为单位, internal_storage 图像大小为 (Width 400 * 高度 267)。为什么不存储图像尺寸是 (Width 800 * Height 534) 以像素为单位的实际尺寸。
这是我从服务器下载图像的代码
str_DownLoadUrl = "http://103.24.4.60/CLASSNK1/MobileService.svc/DownloadFile/FileName/3_20150928162252018.png";
download_PngFile(str_DownLoadUrl);
void download_PngFile(String fileUrl) {
try {
URL ImgUrl = new URL(fileUrl);
HttpURLConnection conn = (HttpURLConnection) ImgUrl.openConnection();
conn.connect();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap imagenObtenida = BitmapFactory.decodeStream(conn.getInputStream(), null, options);
File file = new File(newFolder, imageName);
if (file.exists()) file.delete();
try
{
FileOutputStream out = new FileOutputStream(file);
imagenObtenida.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
int imagenObtenidaW = imagenObtenida.getWidth();
int imagenObtenidaH = imagenObtenida.getHeight();
Log.e("imagenObtenidaW " ," = +" + imagenObtenidaW + " imagenObtenidaH = " + imagenObtenidaH);
} catch (Exception e) {
}
} catch (IOException e) {
e.printStackTrace();
}
}
【问题讨论】:
-
我的图像查看器说这不是一个有效的 PNG 文件。
file实用程序将其识别为 JPEG。 -
options.inSampleSize = 2; -- 文档说 -- 如果设置为 > 1 的值,则请求解码器对原始图像进行二次采样,返回较小的图像以节省内存。 ----developer.android.com/reference/android/graphics/…
-
@Tasos 您应该将其发布为答案。
-
我给了我的答案试试这种方式,让我知道它是否有效?
标签: android