setImageView(Bitmap bitmap)报错

http://freestyle21.cn

双击,弹出popwindow,里面是一个自定义布局,只有一个imageview。

问题是

		View contentView = LayoutInflater.from(con).inflate(R.layout.popup, null);
		// 设置popupWindow的背景颜色
		contentView.setBackgroundColor(Color.GREEN);

		View view1=View.inflate(con,R.layout.main,null);
         View view2=View.inflate(con,R.layout.popup,null);
		 ImageView iview=(ImageView)view1.findViewById(R.id.view);
	    Log.i("===","iview:"+iview);
		iview.setBackgroundColor(Color.RED);
		
		 Bitmap bmp=BitmapFactory.decodeResource(getResources(),R.drawable.a);
		 Log.i("===","bmp:"+bmp);
		 iview.setImageBitmap(bmp);

总不能显示图片,bmp里面也有东西,一直想,找了人帮忙,都没办法。。

冷静了一下,想想这应该很简单,以前的都可以,现在为什么不可以呢,到底是哪里不同呢?

想了一下,以前是直接findViewbyid(R.id.**),现在那个imageview不在默认的layout里面,我是通过

View view2=View.inflate(con,R.layout.popup,null);
		 ImageView iview=(ImageView)view1.findViewById(R.id.view);

来做了,有什么问题呢?

仔细看下代码,第一行的是什么东西,不就是我刚才的

View view2=View.inflate(con,R.layout.popup,null);么?于是尝试着把<pre name="code" class="java" style="background-color: rgb(255, 255, 255); ">View view2=View.inflate(con,R.layout.popup,null);删掉,搞定。
想想和以前哪里不一样了。
 

 

### 解决Android ImageView加载大尺寸Bitmap导致内存溢出的方法 当处理大型图片文件时,直接加载整个图像到内存中可能会引发`OutOfMemoryError`异常。为了避免这种情况发生,可以采用多种策略来优化图片加载过程。 #### 使用缩放比例加载适当大小的图像 通过计算合适的采样率(`inSampleSize`),可以在解码之前缩小原始图片尺寸,从而减少占用的内存空间。具体实现方式如下: ```java public static int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) { inSampleSize *= 2; } } return inSampleSize; } ``` 接着,在实际读取图片前设置此参数: ```java final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); // Calculate inSampleSize based on required dimensions. options.inSampleSize = calculateInSampleSize(options, targetWidth, targetHeight); options.inJustDecodeBounds = false; // Now decode with actual size. Bitmap scaledBitmap = BitmapFactory.decodeFile(filePath, options); imageView.setImageBitmap(scaledBitmap); ``` 这种方法能够有效降低所占内存并提高性能[^1]。 #### 利用异步线程进行后台处理 为了防止阻塞主线程影响用户体验,建议利用`AsyncTask`或其他并发机制将耗时操作移至子线程执行。这不仅有助于提升应用响应速度,还能避免因长时间等待而引起的ANR(Application Not Responding)现象。 ```java private class LoadImageTask extends AsyncTask<String, Void, Bitmap> { private WeakReference<ImageView> imageViewWeakReference; public LoadImageTask(ImageView imageView){ this.imageViewWeakReference = new WeakReference<>(imageView); } @Override protected Bitmap doInBackground(String... params) { String filePath = params[0]; try{ final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath,options ); options.inSampleSize = calculateInSampleSize(options,targetWidth ,targetHeight ); options.inJustDecodeBounds=false ; return BitmapFactory.decodeFile(filePath,options ); }catch(Exception e){ Log.e("LoadImageTask",e.getMessage()); return null ; } } @Override protected void onPostExecute(Bitmap result) { super.onPostExecute(result); if(isCancelled()){ result=null ; } if(imageViewWeakReference !=null ){ ImageView imageView=imageViewWeakReference.get(); if(imageView!=null &&result !=null ) imageView.setImageBitmap(result ); } } } new LoadImageTask(imageView).execute(pathToYourImage); ``` 上述方法确保了即使在复杂场景下也能平稳运行而不至于消耗过多资源[^3]。 #### 考虑使用第三方库简化开发流程 对于更复杂的项目或者希望快速集成高质量解决方案的情况,考虑引入成熟的开源框架如Glide或Picasso等。这些工具提供了丰富的功能集以及良好的兼容性和稳定性支持,极大地方便开发者专注于核心业务逻辑的设计与实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值