Android 如何调整通过URL检索的图像的大小并保存它?
我从互联网上通过URL获取一张图片,并试图在保存之前调整其大小(到较小的大小)。我设法保存了它,但无法调整大小。我怎么能这么做?代码如下:Android 如何调整通过URL检索的图像的大小并保存它?,android,Android,我从互联网上通过URL获取一张图片,并试图在保存之前调整其大小(到较小的大小)。我设法保存了它,但无法调整大小。我怎么能这么做?代码如下: URL url = new URL(LogoURL); InputStream input = url.openStream(); try { OutputStream output = new FileOutputStream("data/data/com.android.mylogo/logo.jpg"); try {
URL url = new URL(LogoURL);
InputStream input = url.openStream();
try {
OutputStream output = new FileOutputStream("data/data/com.android.mylogo/logo.jpg");
try {
//byte[] buffer = new byte[aReasonableSize];
int bytesRead = 0;
System.out.println("Buffer Length is \t:-" + buffer.length);
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
System.out.println("inside while");
output.write(buffer, 0, bytesRead);
}
} finally {
output.close();
System.out.println("saved image");
}
} finally {
input.close();
}
JPG无论如何都是压缩的,所以没有必要进一步压缩它们。
一般来说,要压缩某些内容,请查看软件包的类。如果要将图像缩小到特定尺寸,可以使用以下代码:
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(LogoURL);
try {
HttpResponse response = client.execute(request);
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w(LOG_TAG, "Error " + statusCode + " while retrieving bitmap from " + url);
return null;
}
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream is = null;
BufferedInputStream bis = null;
try {
is = url.openStream();
bis = new BufferedInputStream(is);
int sampleSize = 1;
bis.mark(Integer.MAX_VALUE);
Options bounds = new Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeStream(bis, null, bounds);
if (bounds.outWidth != -1) {
int width = bounds.outWidth;
int height = bounds.outHeight;
boolean withinBounds = width <= YOUR_DESIRED_WIDTH && height <= YOUR_DESIRED_HEIGHT;
int newWidth = width;
int newHeight = height;
while (!withinBounds) {
newWidth /= 2;
newHeight /= 2;
sampleSize *= 2;
withinBounds = newWidth <= YOUR_DESIRED_WIDTH && newHeight <= YOUR_DESIRED_HEIGHT;
}
} else {
Log.w(LOG_TAG, "Can't open bitmap at " + url);
return null;
}
try {
bis.reset();
} catch (IOException e) {
if(is != null){
is.close();
}
if(bis != null){
bis.close();
}
if(!entity.isRepeatable()){
entity.consumeContent();
response = client.execute(request);
entity = response.getEntity();
}
is = entity.getContent();
bis = new BufferedInputStream(is);
}
Options opts = new Options();
opts.inSampleSize = sampleSize;
Bitmap bm = BitmapFactory.decodeStream(bis, null, opts);
return bm;
} finally {
if (is != null) {
is.close();
}
if (bis != null) {
bis.close();
}
entity.consumeContent();
}
}
} catch (IOException e) {
request.abort();
Log.w(LOG_TAG, "I/O error while retrieving bitmap from " + url, e);
} catch (IllegalStateException e) {
request.abort();
Log.w(LOG_TAG, "Incorrect URL: " + url);
} catch (Exception e) {
request.abort();
Log.w(LOG_TAG, "Error while retrieving bitmap from " + url, e);
}
HttpClient=newdefaulthttpclient();
HttpGet请求=新的HttpGet(LogoURL);
试一试{
HttpResponse response=client.execute(请求);
final int statusCode=response.getStatusLine().getStatusCode();
if(statusCode!=HttpStatus.SC\u OK){
Log.w(从“+url”检索位图时,日志标签“错误”+statusCode+”;
返回null;
}
HttpEntity=response.getEntity();
如果(实体!=null){
InputStream=null;
BufferedInputStream bis=null;
试一试{
is=url.openStream();
bis=新的BufferedInputStream(is);
int sampleSize=1;
双标记(整数最大值);
选项边界=新选项();
bounds.InjustDecodeBunds=true;
解码流(bis,null,bounds);
if(bounds.outWidth!=-1){
int width=bounds.outWidth;
int height=bounds.outHeight;
boolean withinBounds=width Compress表示什么类型的压缩?在大小上,或者您希望使用类将其压缩。您可以这样做。@onkar我更新了您的问题。。请检查这是否是您想要的。通常,您必须非常具体地说明您在这里提出的问题:)但您在SD卡更新中保存图像的位置非常重要为其他人服务