96编辑器旗下产品
找图
终身
ID:0
到期时间:永久
退出登录
您的位置:首页 > 百科管理 > 详情

android图片压缩上传-android图片压缩避免OOM

原创:找图网 2023-05-03 16:34:34
  • android开发,上传图片时,怎样自动压缩成800x600的尺寸?求指教~~

  • protected Bitmap scaleImg(Bitmap bm, int newWidth, int newHeight) {

    // 图片源

    // Bitmap bm = (getResources()

    // .openRawResource(id));

    // 获得图片的宽高

    int width = ();

    int height = ();

    // 设置想要的大小

    int newWidth1 = newWidth;

    int newHeight1 = newHeight;

    // 计算缩放比例

    float scaleWidth = ((float) newWidth1) / width;

    float scaleHeight = ((float) newHeight1) / height;

    // 取得想要缩放的matrix参数

    Matrix matrix = new Matrix();

    (scaleWidth, scaleHeight);

    // 得到新的图片

    Bitmap newbm = (bm, 0, 0, width, height, matrix,

    true);

    return newbm;

    }

    你可以试下,如果不行,建议你还是先压缩在上传。

  • android图片压缩避免OOM

  • 简单吹下牛:很多app都会要加载图片,但是如果不压缩图片就很容易OOM,

    个人看来OOM 出现原因总的来说分为两种:

    一种是内存溢出(好像在扯淡,OOM本身就是内存溢出)

    另一种是:图片过大,一个屏幕显示不完全造成,似乎也是一。。 如有错误纯属扯淡;

    为了避免上面的情况:加载图片的时候可以进行压缩,上传的时候要可以进行压缩,在图片不可见的时候进行回收(onDetach()),再吹一句 用了fresco+压缩之后加载图片完全没问题了。

    一、质量压缩方法:

    privateBitmap compressImage(Bitmap image) {

    ByteArrayOutputStream baos =newByteArrayOutputStream();

    (,100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中

    intoptions =100;

    while( ().length /1024>100) {//循环判断如果压缩后图片是否大于100kb,大于继续压缩

    ();//重置baos即清空baos

    (, options, baos);//这里压缩options%,把压缩后的数据存放到baos中

    options -=10;//每次都减少10

    }

    ByteArrayInputStream isBm =newByteArrayInputStream(());//把压缩后的数据baos存放到ByteArrayInputStream中

    Bitmap bitmap = (isBm,null,null);//把ByteArrayInputStream数据生成图片

    returnbitmap;

    }

    二、图片按比例大小压缩方法(根据Bitmap图片压缩)

    privateBitmap comp(Bitmap image) {

    ByteArrayOutputStream baos =newByteArrayOutputStream();

    (,100, baos);

    if( ().length /1024>1024) {//判断如果图片大于1M,进行压缩避免在生成图片()时溢出

    ();//重置baos即清空baos

    (,50, baos);//这里压缩50%,把压缩后的数据存放到baos中

    }

    ByteArrayInputStream isBm =newByteArrayInputStream(());

     newOpts =();

    //开始读入图片,此时把 设回true了

     =true;

    Bitmap bitmap = (isBm,null, newOpts);

     =false;

    intw = ;

    inth = ;

    //现在主流手机比较多是800*480分辨率,所以高和宽我们设置为

    floathh = 800f;//这里设置高度为800f

    floatww = 480f;//这里设置宽度为480f

    //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可

    intbe =1;//be=1表示不缩放

    if(w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放

    be = (int) ( / ww);

    }elseif(w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放

    be = (int) ( / hh);

    }

    if(be <=0)

    be =1;

     = be;//设置缩放比例

    //重新读入图片,注意此时已经把 设回false了

    isBm =newByteArrayInputStream(());

    bitmap = (isBm,null, newOpts);

    returncompressImage(bitmap);//压缩好比例大小后再进行质量压缩

    }

  • android上传图片到php android用压缩为byte流 php怎么解压转为图片啊

  • android 文件上传,自己封装了个方法,

    <?php

    var_dump($_POST);

    var_dump($_FILES);

    foreach($_FILES as $key => $value){

    move_uploaded_file($_FILES[$key]['tmp_name'],

    $_SERVER['DOCUMENT_ROOT'].'/FileUpload/files/'.$_FILES[$key]['name']);

    }

    ?>

    PHP就这样接受了

    最新文章 更多
    最新素材 更多
    公司介绍 网站地图 图片资讯
    Copyright © 2010-2022 山东找图网络科技有限公司  鲁ICP备18007836号-3  邮箱:15653358549@163.com