图片压缩

来源:互联网 发布:电子音合成软件 编辑:程序博客网 时间:2024/06/15 11:10

public class MainActivity extends AppCompatActivity {

private ImageView ivIcon;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    ivIcon = (ImageView) findViewById(R.id.ivIcon);}public void btnImgeResizer(View v){    loadImage("http://20.1.2.198:8080/beautiful.jpg");}private void loadImage(String imgePath){    new AsyncTask<String,Void,Bitmap>(){        @Override        protected void onPreExecute() {            super.onPreExecute();        }        @Override        protected void onPostExecute(Bitmap bitmap) {            super.onPostExecute(bitmap);            if(bitmap != null){                ivIcon.setImageBitmap(bitmap);            }        }        @Override        protected Bitmap doInBackground(String... params) {            try {                //得到图片的地址                String path = params[0];                URL url = new URL(path);                HttpURLConnection connection = (HttpURLConnection) url.openConnection();                connection.setRequestMethod("GET");                connection.setConnectTimeout(5*1000);                connection.setReadTimeout(5*1000);                //代表是否可以获取服务器的响应流                connection.setDoInput(true);                //代表发送post请求是否可以将请求参数发出去                connection.setDoOutput(true);                int code = connection.getResponseCode();                if(code == HttpURLConnection.HTTP_OK){                    InputStream is = connection.getInputStream();                    // 图片加载  方法                    BitmapFactory.Options options = new BitmapFactory.Options();                   options.inJustDecodeBounds = true;                   options.inSampleSize = inSampleSize;        options.inJustDecodeBounds = false;                    Bitmap bitmap = BitmapFactory.decodeStream(is,null,options);                    return bitmap;                }            } catch (IOException e) {                e.printStackTrace();            }            return null;        }
0 0