我在主屏幕上有10张小图片,当我点击这些图片时,我可以看到大图 在这个屏幕上我有一个设置为壁纸的按钮。
问题是,当我点击一些不同的图片我有outOfMemoryError,我无法修复此错误。我尝试了一些解决方案,但我认为我的问题有点不同。
public class FullImageActivity extends Activity {
Button button1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
button1=(Button) findViewById(R.id.button1);
Intent i = getIntent();
final int position = i.getExtras().getInt("id");
final ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
if(position==0)
{
imageView.setImageResource(R.drawable.image_0);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(R.drawable.image_0);
Toast.makeText(
getBaseContext(),
"Wallpaper has been updated",
Toast.LENGTH_SHORT).show();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
我有10个这样的位置。我的图像来自可绘制文件夹。
我试过这个解决方案,但我无法解决它。我不知道我在哪里使用它。
BitmapDrawable bitmapDrawable = ((BitmapDrawable) imageView.getDrawable());
if (null != bitmapDrawable && bitmapDrawable.getBitmap().isRecycled()) {
bitmapDrawable.getBitmap().recycle(); }
bitmapDrawable = null;
这是主要活动
public class MainActivity extends Activity {
private GridView gridView;
private GridViewAdapter customGridAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView);
customGridAdapter = new GridViewAdapter(this, R.layout.row_grid, getData());
gridView.setAdapter(customGridAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
}
private ArrayList<ImageItem> getData() {
final ArrayList<ImageItem> imageItems = new ArrayList<ImageItem>();
// retrieve String drawable array
TypedArray imgs = getResources().obtainTypedArray(R.array.image_ids);
for (int i = 0; i < imgs.length(); i++) {
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),
imgs.getResourceId(i, -1));
imageItems.add(new ImageItem(bitmap, "Image#" + i));
}
return imageItems;
}
}
答案 0 :(得分:0)
在显示10张小图片的主屏幕中,我猜您使用的代码如下:
imageView.setImageDrawable(R.drawable.image0);
即使您的ImageViews显示在屏幕的小区域上,Drawable仍然会被解码为完整尺寸,而不是缩放以适合ImageView的尺寸。在设置为ImageView之前,您应该将这些Drawables解码为具有适当大小的位图。 您可以查看此主题,了解如何执行此操作http://developer.android.com/training/displaying-bitmaps/load-bitmap.html