用于在 android 中显示图像的网格视图
我正在尝试在使用示例的 GridView 中显示图像
但我收到日志错误............
如何解决这个问题?
FullImageActivity.java
public class FullImageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
}
}
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.pic_1, R.drawable.pic_2,
R.drawable.pic_3, R.drawable.pic_4,
R.drawable.pic_5, R.drawable.pic_6,
R.drawable.pic_7, R.drawable.pic_8,
R.drawable.pic_9, R.drawable.pic_10,
R.drawable.pic_11, R.drawable.pic_12,
R.drawable.pic_13, R.drawable.pic_14,
R.drawable.pic_15
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imageView;
}
}
AndroidGridLayoutActivity.java
public class AndroidGridLayoutActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
/**
* On Click event for Single Gridview Item
* */
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
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);
}
});
}
}
full_image.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView android:id="@+id/full_image_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
grid_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth" >
</GridView>
主要的.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
显现
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidhive"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".AndroidGridLayoutActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- FullImageActivity -->
<activity android:name=".FullImageActivity"></activity>
</application>
</manifest>
日志::
09-30 11:42:42.324: D/dalvikvm(946): GC_EXTERNAL_ALLOC freed <1K, 53% free 2563K/5379K, external 14047K/14595K, paused 80ms
09-30 11:42:43.054: D/dalvikvm(946): GC_EXTERNAL_ALLOC freed <1K, 53% free 2564K/5379K, external 17047K/17595K, paused 77ms
09-30 11:42:43.804: D/dalvikvm(946): GC_EXTERNAL_ALLOC freed <1K, 53% free 2566K/5379K, external 20047K/20595K, paused 73ms
09-30 11:42:44.474: D/dalvikvm(946): GC_EXTERNAL_ALLOC freed <1K, 53% free 2568K/5379K, external 23047K/23595K, paused 73ms
09-30 11:42:44.874: D/dalvikvm(946): GC_EXTERNAL_ALLOC freed 2K, 53% free 2567K/5379K, external 24547K/26595K, paused 74ms
09-30 11:42:45.044: I/dalvikvm-heap(946): Clamp target GC heap from 32.494MB to 32.000MB
09-30 11:42:45.076: D/dalvikvm(946): GC_FOR_MALLOC freed 0K, 53% free 2567K/5379K, external 26047K/26595K, paused 61ms
09-30 11:42:45.534: D/dalvikvm(946): GC_EXTERNAL_ALLOC freed 2K, 53% free 2568K/5379K, external 26047K/26595K, paused 76ms
09-30 11:42:45.564: E/dalvikvm-heap(946): 1536000-byte external allocation too large for this process.
09-30 11:42:45.694: I/dalvikvm-heap(946): Clamp target GC heap from 32.495MB to 32.000MB
09-30 11:42:45.694: E/GraphicsJNI(946): VM won't let us allocate 1536000 bytes
09-30 11:42:45.704: D/dalvikvm(946): GC_FOR_MALLOC freed 0K, 53% free 2568K/5379K, external 26047K/26595K, paused 62ms
09-30 11:42:45.704: D/skia(946): --- decoder->decode returned false
09-30 11:42:45.704: D/AndroidRuntime(946): Shutting down VM
09-30 11:42:45.714: W/dalvikvm(946): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-30 11:42:45.744: E/AndroidRuntime(946): FATAL EXCEPTION: main
09-30 11:42:45.744: E/AndroidRuntime(946): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
09-30 11:42:45.744: E/AndroidRuntime(946): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
09-30 11:42:45.744: E/AndroidRuntime(946): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
09-30 11:42:45.744: E/AndroidRuntime(946): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
09-30 11:42:45.744: E/AndroidRuntime(946): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
09-30 11:42:45.744: E/AndroidRuntime(946): at android.content.res.Resources.loadDrawable(Resources.java:1709)
09-30 11:42:45.744: E/AndroidRuntime(946): at android.content.res.Resources.getDrawable(Resources.java:581)
09-30 11:42:45.744: E/AndroidRuntime(946): at android.widget.ImageView.resolveUri(ImageView.java:501)
09-30 11:42:45.744: E/AndroidRuntime(946): at android.widget.ImageView.setImageResource(ImageView.java:280)
09-30 11:42:45.744: E/AndroidRuntime(946): at com.example.androidhive.ImageAdapter.getView(ImageAdapter.java:48)
09-30 11:42:45.744: E/AndroidRuntime(946): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
09-30 11:42:45.744: E/AndroidRuntime(946): at android.widget.GridView.onMeasure(GridView.java:935)
09-30 11:42:45.744: E/AndroidRuntime(946): at android.view.View.measure(View.java:8313)
09-30 11:42:45.744: E/AndroidRuntime(946): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-30 11:42:45.744: E/AndroidRuntime(946): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
09-30 11:42:45.744: E/AndroidRuntime(946): at android.view.View.measure(View.java:8313)
09-30 11:42:45.744: E/AndroidRuntime(946): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
09-30 11:42:45.744: E/AndroidRuntime(946): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
09-30 11:42:45.744: E/AndroidRuntime(946): at android.view.View.measure(View.java:8313)
09-30 11:42:45.744: E/AndroidRuntime(946): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-30 11:42:45.744: E/AndroidRuntime(946): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
09-30 11:42:45.744: E/AndroidRuntime(946): at android.view.View.measure(View.java:8313)
09-30 11:42:45.744: E/AndroidRuntime(946): at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
09-30 11:42:45.744: E/AndroidRuntime(946): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
09-30 11:42:45.744: E/AndroidRuntime(946): at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 11:42:45.744: E/AndroidRuntime(946): at android.os.Looper.loop(Looper.java:123)
09-30 11:42:45.744: E/AndroidRuntime(946): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-30 11:42:45.744: E/AndroidRuntime(946): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 11:42:45.744: E/AndroidRuntime(946): at java.lang.reflect.Method.invoke(Method.java:507)
09-30 11:42:45.744: E/AndroidRuntime(946): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-30 11:42:45.744: E/AndroidRuntime(946): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-30 11:42:45.744: E/AndroidRuntime(946): at dalvik.system.NativeStart.main(Native Method)
09-30 11:47:45.834: I/Process(946): Sending signal. PID: 946 SIG: 9