这是以非常低的分辨率保存图像的代码。来自 url 的图像太大但是当我通过此代码保存图像时,它会减小图像的大小和分辨率private void saveImage(final String uri) throws IOException, IllegalStateException { URL url = new URL(uri); InputStream input = url.openStream(); File storagePath = Environment.getExternalStorageDirectory(); OutputStream output = new FileOutputStream(storagePath + "/myImage.png"); try { byte[] buffer = new byte[2048]; int bytesRead = 0; while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) { output.write(buffer, 0, bytesRead); } } finally { output.close(); }
1 回答
ibeautiful
TA贡献1993条经验 获得超6个赞
尝试如下:
Bitmap bitmapImage; //your input bitmap to save
File storagePath = Environment.getExternalStorageDirectory();
OutputStream output = new FileOutputStream(storagePath + "/myImage.png")
try {
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, output);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
添加回答
举报
0/150
提交
取消
