用于在android中显示图像的Gridview

时间:2013-09-30 06:24:09

标签: android xml gridview

用于在android

中显示图像的网格视图

我正在尝试在GridView中显示图像我正在使用示例

Androidhive

但我收到了日志错误 ............

如何解决此问题?


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>

main.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

1 个答案:

答案 0 :(得分:1)

试试这个,您需要解码图像大小。使用以下代码可以帮助您。

  private Bitmap getBitmap(String url) {


    // from SD cache
    Bitmap b = decodeFile(f);
    if (b != null)
        return b;

    // from web
    try {
        Bitmap bitmap = null;
        URL imageUrl = new URL(url);

        HttpURLConnection conn = (HttpURLConnection) imageUrl
                .openConnection();
        conn.setConnectTimeout(1000);
        conn.setReadTimeout(1000);
        conn.setInstanceFollowRedirects(true);
        InputStream is = conn.getInputStream();
        OutputStream os = new FileOutputStream(f);
        Utils.CopyStream(is, os);
        os.close();
        bitmap = decodeFile(f);
        return bitmap;
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

// decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
    try {
        // decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        // Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE = 150;
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE
                    || height_tmp / 2 < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        // decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
    }
    return null;
}