public Bitmap decodeSampledBitmapFromBytes(byte[] bytes,int reqWidth,int reqHeight){
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
}
public int calculateInSampleSize(BitmapFactory.Options options,int reqWidth,int reqHeight){
if(reqWidth == 0 || reqHeight == 0){
return 1;
}
final int width = options.outWidth;
final int height = options.outHeight;
int inSampleSize = 1;
if( width > reqWidth || height > reqHeight){
final int halfWidth = width / 2;
final int halfHeight = height / 2;
while ((halfWidth / inSampleSize) >= reqWidth && (halfHeight / inSampleSize) >= reqHeight){
inSampleSize *=2;
}
}
return inSampleSize;
}
public Bitmap decodeSampledBitmapFromResource(Resources res,int resId,int reqWidth,int reqHeight){
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res,resId,options);
options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res,resId,options);
}
public Bitmap decodeSampledBitmapFromDescrptor(FileDescriptor fd,int reqWidth,int reqHeight){
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fd,null,options);
options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFileDescriptor(fd,null,options);
}
public class ImageResizer {
private static final String TAG = "ImageResizer";
public ImageResizer(){}
public Bitmap decodeSampledBitmapFromResource(Resources res,int resId,int reqWidth,int reqHeight){
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res,resId,options);
options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res,resId,options);
}
public Bitmap decodeSampledBitmapFromBytes(byte[] bytes,int reqWidth,int reqHeight){
final BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap a = BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
Log.d(TAG, "before bitmap : " + a.getRowBytes() * a.getHeight());
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);
options.inJustDecodeBounds = false;
Bitmap b = BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
Log.d(TAG, "after bitmap : " + b.getRowBytes() * b.getHeight());
return b;
}
public Bitmap decodeSampledBitmapFromDescrptor(FileDescriptor fd,int reqWidth,int reqHeight){
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fd,null,options);
options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFileDescriptor(fd,null,options);
}
public int calculateInSampleSize(BitmapFactory.Options options,int reqWidth,int reqHeight){
if(reqWidth == 0 || reqHeight == 0){
return 1;
}
final int width = options.outWidth;
final int height = options.outHeight;
int inSampleSize = 1;
if( width > reqWidth || height > reqHeight){
final int halfWidth = width / 2;
final int halfHeight = height / 2;
while ((halfWidth / inSampleSize) >= reqWidth && (halfHeight / inSampleSize) >= reqHeight){
inSampleSize *=2;
}
}
return inSampleSize;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.yuan.test.Main2Activity">
<ImageView
android:id="@+id/main2Iv"
android:layout_width="200dp"
android:layout_height="200dp" />
</RelativeLayout>
public class Main2Activity extends AppCompatActivity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
testHttp(iv);
}
private void testHttp(final ImageView iv) {
new Thread(new Runnable() {
@Override
public void run() {
String urlString = "https://static.pexels.com/photos/295818/pexels-photo-295818.jpeg";
HttpURLConnection urlConnection = null;
InputStream in = null;
ByteArrayOutputStream outStream = null;
try {
URL url = new URL(urlString);
urlConnection = (HttpURLConnection) url.openConnection();
in = urlConnection.getInputStream();
byte[] buffer = new byte[1024];
int len;
outStream = new ByteArrayOutputStream();
while ((len = in.read(buffer)) != -1){
outStream.write(buffer,0,len);
}
final byte[] data = outStream.toByteArray();
runOnUiThread(new Runnable() {
@Override
public void run() { //在主线程加载UI
iv.setImageBitmap(new ImageResizer().decodeSampledBitmapFromBytes(data,200,200));
}
});
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(urlConnection !=null){
urlConnection.disconnect();
}
if(in != null){
in.close();
}
if(outStream != null){
outStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
}
private void initView() {
iv = (ImageView) findViewById(R.id.main2Iv);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有