我在网上看了一些解析图片的代码,然后用一些网站的图片进行测试,但是当解析我的网站的图片的时候,就直接报错,我个人认为应该是我的网站的图片太大的缘故,然后我按照网上给的图片压缩的方法进行尝试,但是发现没有什么效果,不知道是什么原因,恳请各位指教。少都不说了直接上代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.buttonId);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myAsyncTask = new MyAsyncTask();
myAsyncTask.execute();
}
});
imageView=(ImageView)findViewById(R.id.imageViewId);
}
//下载图片
public void loadImageFromUrl() {
URL m;
// String url=“http://news.ucas.ac.cn/Content/Upload/2014/5/315.jpg”//这个链接是网站需要的图片
String url="http:////imgsrc.baidu.com/forum/pic/item/0a6d462c11dfa9eca10597e560d0f703908fc15c.jpg"// 测试用的图片
InputStream i = null;
BufferedInputStream bis = null;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
try {
m = new URL(url);
i = (InputStream) m.getContent();
bis = new BufferedInputStream(i,1024 * 8);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// 从输入流读取数据
byte[] data = StreamTool.read(bis);
BitmapFactory.decodeByteArray(data, 0, data.length, options);
int w = options.outWidth;
int h = options.outHeight;
// 从服务器端获取的图片大小为:80x120
// 我们想要的图片大小为:40x40
// 缩放比:120/40 = 3,也就是说我们要的图片大小为原图的1/3
// 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int ratio = 1; // 默认为不缩放
if (w >= h && w > width) {
ratio = (int) (w / width);
} else if (w < h && h > height) {
ratio = (int) (h / height);
}
if (ratio <= 0) {
ratio = 1;
}
System.out.println("图片的缩放比例值ratio = " + ratio);
options.inJustDecodeBounds = false;
// 属性值inSampleSize表示缩略图大小为原始图片大小的几分之一,即如果这个值为2,
// 则取出的缩略图的宽和高都是原始图片的1/2,图片大小就为原始大小的1/4。
options.inSampleSize = ratio;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
// 图片质量默认值为100,表示不压缩
int quality = 10;
// PNG是无损的,将会忽略质量设置。因此,这里设置为JPEG
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outStream);
// 判断压缩后图片的大小是否大于100KB,大于则继续压缩
while (outStream.toByteArray().length / 1024 > 100) {
outStream.reset();
// 压缩quality%,把压缩后的数据存放到baos中
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outStream);
quality -= 10;
}
System.out.println("quality = " + quality);
byte[] data01 = outStream.toByteArray();
bitmap01 = BitmapFactory.decodeByteArray(data, 0, data.length);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(bitmap01.getWidth()+"::"+bitmap01.getHeight());
}
private Bitmap getBitmap()
{
return bitmap01;
}
//开启新的线程下载图片
private class MyAsyncTask extends AsyncTask<Object, Integer, Void>{
@Override
protected Void doInBackground(Object... params) {
loadImageFromUrl();
return null;
}
@Override
protected void onPostExecute(Void result) {
imageView.setImageBitmap(getBitmap());
//imageView.invalidate();
}
}
}
不清楚是啥原因,望知道的大神告知。。。万分感谢
------解决方案--------------------
根据url获得图片来源(这段代码写在子线程里)
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.connect();
InputStream input = conn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
input.close();
conn.disconnect();
return bitmap;
/**
* 压缩图片
*
* @param bitmap
* 源图片
* @param width
* 想要的宽度
* @param height
* 想要的高度
* @param isAdjust
* 是否自动调整尺寸, true图片就不会拉伸,false严格按照你的尺寸压缩
* @return Bitmap
*/
public Bitmap reduce(Bitmap bitmap, int width, int height, boolean isAdjust) {
if (null == bitmap) {
return null;
}
if (bitmap.getWidth() < width && bitmap.getHeight() < height && isAdjust) {