【发布时间】:2020-09-17 20:06:03
【问题描述】:
我正在开发一个从网上下载.JPG图片的功能。
当我使用BitmapFactory.decodeStream()下载并保存在我的内部存储中时,文件大小比原始文件大。
原始图像文件(.JPG)大小为 114KB 我保存的文件大小是261KB
b = BitmapFactory.decodeStream(urlConnection.getInputStream()); //Download original image
FileOutputStream fos = openFileOutput("x.jpg", MODE_PRIVATE);
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
File ne = new File(getFilesDir()+"/x.jpg");
String l = ""+ Formatter.formatFileSize(MainActivity.this,ne.length())+"byte"//261KB
编辑:
InputStream is = urlConnection.getInputStream();
OutputStream outStream = MainActivity.this.openFileOutput("x.jpg", MODE_PRIVATE);
byte[] buf = new byte[1024]; int len = 0;
int total_len=0;
while ((len = is.read(buf)) > 0) {
outStream.write(buf, 0, len);
total_len+=len;
}
is.close(); outStream.close();
//total_len = 116797 byte
//saved image file size = 116797 byte
Bitmap z = BitmapFactory.decodeFile(getFilesDir()+"/x.jpg");
img.setImageBitmap(z);
【问题讨论】:
-
评论不用于扩展讨论;这个对话是moved to chat。
标签: android android-image