gpt4 book ai didi

java - 错误,无法在此处访问无效/免费的位图!使用包含位图的模型类时

转载 作者:行者123 更新时间:2023-12-05 06:14:36 37 4
gpt4 key购买 nike

在我的应用中,我使用包含位图的模型类在 recyclerView 上显示图像

public class ImageModelClass {

Bitmap image;

public void setImage(Bitmap image) {
this.image = image;
}

public Bitmap getImage() {
return image;
}

问题是当我直接从我的模型类使用位图时,这个错误抛出Error, cannot access an invalid/free'd bitmap here!

当我将位图类替换为模型类中的字符串并使用编码位图将位图图像转换为字符串时,此错误就不会出现。

但是编码位图会花费大量时间加载图像。

public class ImageCollection extends AppCompatActivity {
public static final String IMAGE_SHARED_PREFS = "com.example.animproject_IMAGE_SHARED_PREFS";
public static final String IMAGE_DATA_KEY = "com.example.animproject_IMAGE_DATA_KEY";

private static final int REQUEST_CODE = 1;
RecyclerView recyclerView;
ImageCollectionAdapter adapter;
List<ImageModelClass> imageList;
FloatingActionButton fab;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_collection);

loadAlbumData();
recyclerView = findViewById(R.id.imageCollectionRecyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);

adapter = new ImageCollectionAdapter(this,imageList);
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, GridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
recyclerView.setAdapter(adapter);

fab = findViewById(R.id.fabButton);
fab.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void onClick(View view) {
Intent gallery = new Intent(Intent.ACTION_PICK);
gallery.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
gallery.setType("image/*");

startActivityForResult(gallery, REQUEST_CODE);
}
});

}


@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
assert data != null;
ClipData clipData = data.getClipData();

if (clipData != null) {
for (int i = 0; i < clipData.getItemCount(); i++) {

Uri imageUri = clipData.getItemAt(i).getUri();
InputStream inputStream;

try {
inputStream = getContentResolver().openInputStream(imageUri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
ImageModelClass imageModelClass = new ImageModelClass();
imageModelClass.setImage(bitmap);

imageList.add(imageModelClass);
adapter.notifyDataSetChanged();
saveGalleryData();
} catch (FileNotFoundException e) {
e.printStackTrace();
}

}
} else {
Uri imageUri = data.getData();
InputStream inputStream = null;
try {
assert imageUri != null;
inputStream = getContentResolver().openInputStream(imageUri);

} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
ImageModelClass imageModelClass = new ImageModelClass();
imageModelClass.setImage(bitmap);

imageList.add(imageModelClass);
adapter.notifyDataSetChanged();
saveGalleryData();
}
}
}

private void saveGalleryData() {
SharedPreferences preferences = getSharedPreferences(IMAGE_SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
Gson gson = new Gson();
String json = gson.toJson(imageList);
editor.putString(IMAGE_DATA_KEY, json);
editor.apply();
}

private void loadAlbumData() {
SharedPreferences preferences = getSharedPreferences(IMAGE_SHARED_PREFS, MODE_PRIVATE);
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<ImageModelClass>>() {
}.getType();
String data = preferences.getString(IMAGE_DATA_KEY, null);
imageList = gson.fromJson(data, type);

if (imageList == null) {
imageList = new ArrayList<>();
}
}

适配器

public class ImageCollectionAdapter extends RecyclerView.Adapter<ImageCollectionAdapter.MyViewHolder> {

List<ImageModelClass> list;
Context context;


public ImageCollectionAdapter(Context context,List<ImageModelClass> list) {
this.list = list;
this.context = context ;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new MyViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.image_collection_lists, parent, false));
}


@Override
public void onBindViewHolder(@NonNull
MyViewHolder holder, int position) {
ImageModelClass currentImage =
list.get(position);

Glide
.With(context.getApplicationContext)
.load(currentImages.getImages)
/* getting bitmap images from model
* class
*/
.into(holder.imageView);



}

@Override
public int getItemCount() {
return list.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder {
RoundedImageView imageView;

public MyViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.galleryPicture);
}
}
}

请帮助它会很感激:)

最佳答案

当我尝试从 textView 获取位图时,我遇到了同样的问题。 textView 位于底部工作表内的 RecyclerView 中。当我点击 textView 时,我得到了位图并关闭了底部工作表。

我从 textView 的绘图缓存中获取了位图。我使用调试器检查了位图是否已正确检索。但是当我将它传递给回调(从 recyclerView 的适配器到 Activity )时,回调中的位图没有显示在调试器中,我收到错误消息“无法在此处访问无效/免费的位图!”。

据我了解,当 textView 未在屏幕上显示/呈现时(在我的情况下,当我关闭底部工作表时)缓存的位图变得不可用。必须阅读相关文档才能了解问题的确切原因。

我为解决这个问题所做的是创建位图的副本并将其传递给回调并且成功了!

您可以像这样复制位图:

Bitmap copyBitmap = bitmap.copy(bitmap.getConfig(), false);

获取位图和创建新位图的完整代码:

       holder.textView.setDrawingCacheEnabled(true);
holder.textView.buildDrawingCache();
Bitmap bitmap = holder.textView.getDrawingCache();
Bitmap copyBitmap = bitmap.copy(bitmap.getConfig(), false);
holder.textView.setDrawingCacheEnabled(false);
callback.onWidgetSelected(copyBitmap);

希望对您有所帮助。我也会确保研究文档,并在找到此问题的原因时更新答案。

关于java - 错误,无法在此处访问无效/免费的位图!使用包含位图的模型类时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62817622/

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