图片压缩

来源:互联网 发布:网络监控摄像头安装图 编辑:程序博客网 时间:2024/06/03 19:27
import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.AsyncTask;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.ImageView;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;public class MainActivity extends AppCompatActivity {    private ImageView imageView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        imageView = (ImageView) findViewById(R.id.tupian);    }    public void Button(View view){        yasuo("http://imgs.juheapi.com/comic_xin/6L6b5be06L6%2B55qE5YaS6Zmp/237401/0-MjM3NDAxMA==.jpg");    }    private void yasuo(String path) {        new AsyncTask<String,Void,Bitmap>(){            @Override            protected void onPostExecute(Bitmap bitmap) {                super.onPostExecute(bitmap);                if(bitmap != null){                    imageView.setImageBitmap(bitmap);                }else{                    imageView.setImageResource(R.mipmap.ic_launcher);                }            }            @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.setReadTimeout(5000);                    connection.setConnectTimeout(5000);                    int code = connection.getResponseCode();                    if(code == 200){                        InputStream is = connection.getInputStream();                        BitmapFactory.Options options = new BitmapFactory.Options();                        options.inJustDecodeBounds = true;                        BitmapFactory.decodeStream(is,null,options);                        int width = options.outWidth;                        int height = options.outHeight;                        int i = 1;                        if(width>100 || height>100){                            int w = width / 2;                            int h = height / 2;                            while ((w / i) >= 100 && (h / i) >= 100){                                i *=2;                            }                        }                        options.inSampleSize = i;                        options.inJustDecodeBounds = false;                        is.close();                        is = url.openStream();                        Bitmap bitmap = BitmapFactory.decodeStream(is,null,options);                        is.close();                        return  bitmap;                    }                } catch (Exception e) {                    e.printStackTrace();                }                return null;            }        }.execute(path);    }}
原创粉丝点击