我正在制作一个想要显示高分辨率图片For.eg(1920 X 1200)的图库的应用程序。当我用高分辨率图片设置items.add(new Item("Red", R.drawable.wall1));
时,它崩溃了。如何做到这一点。
请高手帮帮我。提前致谢。
public class ImageGalleryActivity extends Activity {
String strCategoryName = null;
GridView gridView;
Utils utils;
private int columnWidth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_gallery);
Intent intent;
intent = getIntent();
if (intent != null) {
strCategoryName = intent.getStringExtra("category");
}
gridView = (GridView) findViewById(R.id.gridView_cards);
gridView.setAdapter(new MyAdapter(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.image_gallery, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
class MyAdapter extends BaseAdapter {
private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
public MyAdapter(Context context) {
inflater = LayoutInflater.from(context);
items.add(new Item("Red", R.drawable.wall1));
items.add(new Item("Magenta", R.drawable.img10));
items.add(new Item("Dark Gray", R.drawable.img11));
items.add(new Item("Gray", R.drawable.img12));
items.add(new Item("Green", R.drawable.img13));
items.add(new Item("Cyan", R.drawable.img14));
items.add(new Item("Dark Gray", R.drawable.img11));
items.add(new Item("Gray", R.drawable.img12));
items.add(new Item("Dark Gray", R.drawable.img11));
items.add(new Item("Gray", R.drawable.img12));
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int i) {
return items.get(i);
}
@Override
public long getItemId(int i) {
return items.get(i).drawableId;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
ImageView picture;
TextView name;
if(v == null) {
v = inflater.inflate(R.layout.frame_layout_square_image_view, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
}
picture = (ImageView)v.getTag(R.id.picture);
name = (TextView)v.getTag(R.id.text);
Item item = (Item)getItem(i);
picture.setImageResource(item.drawableId);
name.setText(item.name);
return v;
}
private class Item {
final String name;
final int drawableId;
Item(String name, int drawableId) {
this.name = name;
this.drawableId = drawableId;
}
}
}
记录猫错误。
05-12 08:19:59.004: E/AndroidRuntime(16159): FATAL EXCEPTION: main
05-12 08:19:59.004: E/AndroidRuntime(16159): java.lang.OutOfMemoryError
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.content.res.Resources.loadDrawable(Resources.java:1965)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.content.res.Resources.getDrawable(Resources.java:660)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.ImageView.resolveUri(ImageView.java:616)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.ImageView.setImageResource(ImageView.java:349)
05-12 08:19:59.004: E/AndroidRuntime(16159): at com.example.paperfreepost.MyAdapter.getView(ImageGalleryActivity.java:155)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.GridView.makeAndAddView(GridView.java:1341)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.GridView.makeRow(GridView.java:341)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.GridView.fillDown(GridView.java:283)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.GridView.fillFromTop(GridView.java:417)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.GridView.layoutChildren(GridView.java:1229)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.AbsListView.onLayout(AbsListView.java:1994)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.View.layout(View.java:14008)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.ViewGroup.layout(ViewGroup.java:4373)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.View.layout(View.java:14008)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.ViewGroup.layout(ViewGroup.java:4373)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.View.layout(View.java:14008)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.ViewGroup.layout(ViewGroup.java:4373)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.View.layout(View.java:14008)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.ViewGroup.layout(ViewGroup.java:4373)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.View.layout(View.java:14008)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.ViewGroup.layout(ViewGroup.java:4373)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.Choreographer.doFrame(Choreographer.java:532)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.os.Handler.handleCallback(Handler.java:725)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.os.Handler.dispatchMessage(Handler.java:92)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.os.Looper.loop(Looper.java:137)
05-12 08:19:59.004: E/AndroidRuntime(16159): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-12 08:19:59.004: E/AndroidRuntime(16159): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 08:19:59.004: E/AndroidRuntime(16159): at java.lang.reflect.Method.invoke(Method.java:511)
05-12 08:19:59.004: E/AndroidRuntime(16159): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-12 08:19:59.004: E/AndroidRuntime(16159): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-12 08:19:59.004: E/AndroidRuntime(16159): at dalvik.system.NativeStart.main(Native Method)
最佳答案
添加此方法
public Bitmap decodeImage(int resourceId) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), resourceId, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 100; // you are free to modify size as your requirement
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeResource(getResources(), resourceId, o2);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
从适配器调用
picture.setImageBitmap((decodeImage(item.drawableId));
相反,这条线
picture.setImageResource(item.drawableId);