gpt4 book ai didi

android - 避免位图被回收android

转载 作者:太空宇宙 更新时间:2023-11-03 10:18:10 25 4
gpt4 key购买 nike

我有两个异步任务,其中一个下载图像,另一个添加模糊效果,但我的问题是,在下载任务的 onPostExecute 回调中,我想将位图放入一个 ImageView 然后将它传递给第二个任务以对其应用模糊效果但有时我得到

trying to use a recycled bitmap  

我在这个问题上搜索了很多,但我没有找到一个明确的解决方案来避免这个问题,特别是我在 View 寻呼机的 fragment 中使用这个图像这是我的代码

  protected void onPostExecute(Bitmap result) {
Bitmap bitmap=result;
bmImage.setImageBitmap(bitmap);
if(bitmap!=null && !bitmap.isRecycled()){
Bitmap bb=bitmap;

new blurTask().execute(bb);}
}

这是我完整的 fragment 代码

public class ScreenSlidePageFragment extends Fragment {
int page;
String img;
// ImageView blured;
String[] checkblur;
int size;
Boolean onCreateViewCalled = false;
ImageView blur;
ImageView imageView;

public static ScreenSlidePageFragment newInstance(int page, String img, int size) {
ScreenSlidePageFragment fragmentFirst = new ScreenSlidePageFragment();
Bundle args = new Bundle();
args.putInt("someInt", page);
args.putString("img", img);
args.putInt("size", size);
fragmentFirst.setArguments(args);
return fragmentFirst;
}

// Store instance variables based on arguments passed
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
page = getArguments().getInt("someInt", 0);
img = getArguments().getString("img");
size = getArguments().getInt("size");
checkblur = new String[size];
onCreateViewCalled = false;
}

public void ChangeAlpha(float alpha) {
System.out.println(alpha);
blur.setAlpha(alpha);

}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = (View) inflater.inflate(
R.layout.fragment_screen_slide_page, container, false);

imageView = (ImageView) v.findViewById(R.id.image);
blur = (ImageView) v.findViewById(R.id.blur);
//Glide.with(this).load(img).into(imageView);
new DownloadImageTask(imageView).execute(img);
return v;
}
class blurTask extends AsyncTask<Bitmap, Integer, BitmapDrawable> {
Bitmap image;

@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected BitmapDrawable doInBackground(Bitmap... params) {
image = params[0];


Bitmap b = Blur.blurBitmap(image, getActivity());
final BitmapDrawable ob = new BitmapDrawable(getResources(), b);


return ob;
}

@Override
protected void onPostExecute(BitmapDrawable blur1) {
// Do whatever you need with the string, you can update your UI from here
// blured.setBackground(blur);
if (blur1 != null)
System.out.println("yryryryry");
blur.setBackground(blur1);
}


}
class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;

public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}

protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}

protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(Bitmap.createBitmap(result));
new blurTask().execute(Bitmap.createBitmap(bmImage));

}
}
}

所以我得到图像然后出现模糊效果但是当我尝试滑动到下一个 viewpager 页面时我得到这个错误
有帮助吗?
谢谢。

最佳答案

您可以通过在处理之前将原始位图复制到新位图来避免尝试使用回收位图。试试这个

protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
new blurTask().execute(Bitmap.createBitmap(result));
}

希望对你有用。

关于android - 避免位图被回收android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31693681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com