android图片下载及处理(包括缓冲加载,预取图片等)

本文详细介绍了在Android平台上如何实现图片的高效下载和处理,包括使用AsyncTask进行后台下载,Bitmap优化以减少内存消耗,以及实现ImageView的缓冲加载和预加载策略,提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class MainActivity extends Activity {

	ImageView imageView ;
	String uil = "http://192.168.56.1:8080/webroot/sna.jpg"; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		imageView = (ImageView)findViewById(R.id.pic);
		
		
		
		/*FileOutputStream out;
		try {
			out = this.openFileOutput("sna.jpg", Context.MODE_WORLD_READABLE);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.write("QWER".getBytes());
		out.close();*/
		
	}

	@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;
	}

	public void onclick (View v){
		//int id = v.getId();
		Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"/sna.jpg");
		Log.i("tag", ""+Environment.getExternalStorageDirectory().getAbsolutePath()+"/picture1.jpg");
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)&&bitmap!=null) {
				imageView.setImageBitmap(bitmap);
		}else {
			MyAsyncTask myAsyncTask = new MyAsyncTask(imageView); 
			myAsyncTask.execute(uil);
		}	
		
		
		/*
			MyAsyncTask myAsyncTask = new MyAsyncTask(imageView); 
			myAsyncTask.execute(uil);*/
	
	}
	
}



public class MyAsyncTask extends AsyncTask<String, Integer, Bitmap> {

	ImageView iv ;
	Bitmap bitmap ;
	public MyAsyncTask(ImageView imageView) {
		super();
		iv = imageView;
	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
	}
	
	@Override
	protected Bitmap doInBackground(String... params) {
		//客户端对象
		HttpClient client = new DefaultHttpClient();
		//
		HttpGet get = new HttpGet(params[0]);
		try {
			HttpResponse response = client.execute(get);
			if (response.getStatusLine().getStatusCode() == 200) {
				//流用完就会制空
				InputStream inputStream = response.getEntity().getContent();
				//bitmap = BitmapFactory.decodeStream(inputStream);
				
				File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/sna.jpg");
				Log.i("tag", ""+Environment.getExternalStorageDirectory().getAbsolutePath()+"/picture.jpg");
				
				FileOutputStream out;
				try {
					out = new FileOutputStream(file);
					byte[] date = new byte[1024];
					int length = 1;
					while ((length = inputStream.read(date))!=-1) {
						out.write(date,0,length);
					}
					
					
					out.flush();
					out.close();
					bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"/sna.jpg");
					
					
					inputStream.close();
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bitmap;
	}

	@Override
	protected void onPostExecute(Bitmap result) {
		if (result!=null) {
			iv.setImageBitmap(result);
		}
		super.onPostExecute(result);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值