问题描述
- android下载图片过大导致报错
-
package tarena.day2801;import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;public class MainActivity extends Activity {
private Button button; private ImageView img; private String image_Path = "http://2.javatl.sinaapp.com/images/51d8c1d77fed4.jpg"; private ProgressDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button1); img = (ImageView) findViewById(R.id.imageView1); LinearLayout rl = (LinearLayout) findViewById(R.id.lr); ImageView img = new ImageView(this); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int height = dm.heightPixels/3; int width = dm.widthPixels / 2; LayoutParams params = new LayoutParams(width, height); img.setLayoutParams(params); rl.setGravity(Gravity.CENTER); img.setImageResource(R.drawable.ic_launcher); dialog = new ProgressDialog(this); dialog.setTitle("提示信息"); dialog.setMessage("正在下载,请稍候..."); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub new MYTask().execute(image_Path); } }); } /** * 使用异步任务的规则: 1、申明的类继承AsyncTask 标注三个参数的类型 * 2、第一个参数表示要执行的任务,通常是网络的路径;第二个参数表示进度的刻度,第三个参数表示任务执行的返回结果 * * @author liende * */ public class MYTask extends AsyncTask<String, Void, Bitmap> { /** * 表示任务执行之前的操作 */ @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); dialog.show(); } /** * 主要是完成耗时的操作 */ @Override protected Bitmap doInBackground(String... arg0) { // TODO Auto-generated method stub // 使用网络连接类HttpClient类王城对网络数据的提取 HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(arg0[0]); Bitmap bitmap = null; try { HttpResponse httpResponse = httpClient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode() == 200) { HttpEntity httpEntity = httpResponse.getEntity(); byte[] data = EntityUtils.toByteArray(httpEntity); bitmap = BitmapFactory .decodeByteArray(data, 0, data.length);
//在这里调用压缩方法
bitmap = compressImage(bitmap); } } catch (Exception e) { // TODO: handle exception } return bitmap; } /** * 主要是更新UI的操作 */ @Override protected void onPostExecute(Bitmap bitmap) { // TODO Auto-generated method stub super.onPostExecute(bitmap); Bitmap bmcopy = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()); Paint paint = new Paint(); Canvas canvas = new Canvas(bmcopy); Matrix mt = new Matrix(); mt.setRotate(30, bmcopy.getWidth()/2, bmcopy.getHeight()/2); //mt.setScale(0.5f, 2); canvas.drawBitmap(bitmap, mt, paint); img.setImageBitmap(bmcopy); dialog.dismiss(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }
** //图片压缩方法**
private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中 int options = 100; while ( baos.toByteArray().length / 1024>100) { //循环判断如果压缩后图片是否大于100kb,大于继续压缩 baos.reset();//重置baos即清空baos image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中 options -= 10;//每次都减少10 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中 Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片 return bitmap; }
}
明明调用了图片压缩方法,但是不知道为什么运行的时候还是崩溃
解决方案
你那个压缩循环有问题,压缩已经压缩过的东西基本没有效果的。所以,如果你第一次JPEG压缩不能达到100kb一下的话,那么这个while基本就是个死循环。
解决方案二:
推荐使用Universal-ImageLoader.