小弟是新手中的新手啊,这个问题给个思路或代码也行。就是android手机客户端读取网站一编新闻的文章 ,文章中的不定个数的图片,但这些图片会比较大,怎么在android手机客户端把所有图片压缩后再显示文章所有内容呢(包括图片和文字)Android手机图片压缩

解决方案 »

  1.   

    楼主说的图片比较大,是在考虑内存不足的问题么?如果是的话,可以通过修改图片的采用率来压缩图片,但是图片的质量会下降。
    如果说图片比较大,是指的是图片尺寸问题,那就修改图片的尺寸咯。给一段参考代码:private Bitmap decodeFile(File file) {
    try {
    BitmapFactory.Options op = new BitmapFactory.Options();
    op.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(new FileInputStream(file), null, op); // Find the correct scale value. It should be the power of 2.
    final int REQUIRED_SIZE = 70;
    int width_tmp = op.outWidth;
    int height_tmp = op.outHeight;
    int scale = 1;
    while (true) {
    if (width_tmp / 2 < REQUIRED_SIZE
    || height_tmp / 2 < REQUIRED_SIZE)
    break;
    width_tmp /= 2;
    height_tmp /= 2;
    scale *= 2;
    } BitmapFactory.Options op2 = new BitmapFactory.Options();
    op2.inSampleSize = scale;
    return BitmapFactory.decodeStream(new FileInputStream(file), null,
    op2); } catch (Exception e) {
    e.printStackTrace();

    } catch (OutOfMemoryError oom) {
    oom.printStackTrace();
    }
    return null;
    }
    上面代码中的op2.inSampleSize = scale;就是在调整采样率,以减少内存使用。
    当然,也可以通过上面代码的BitmapFactory.Options中的outWidth和outHeight来对尺寸就行修改。
      

  2.   

    不太明白你的意思,如果图片是来源于web的话,那不管你想对图片做什么处理第一步还是要先把原图加载到本地