1

我正在使用Google 的 Hello GridView 教程来学习如何使用GridView. 它可以正常工作Drawable,我可以将许多项目添加到GridView. 运行应用程序时,我可以滚动和旋转手机。

但是,如果我使用 SD 卡中的图像或照片,就会出现问题。我可以显示Activity带有缩略图的Drawable,但是当我旋转手机时,我会收到以下消息:

应用程序意外停止。

有人能帮我吗?

显现 :

    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
    </uses-permission>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".TestActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

布局 :

    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <GridView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="90dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" />

</LinearLayout>

活动:包test.test;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
    protected String[] listImages;
    protected String txt;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        GridView gridview = (GridView) findViewById(R.id.gridview);

        // search files
        this.listImages = listFile("/sdcard/projetv/test");

        // to show files name
        TextView text = (TextView) findViewById(R.id.txt);
        text.setText(this.txt);

        gridview.setAdapter(new ImageAdapter(this));

    }

    /*
     * Return list of files in the directory "path", with extention "ext"
     */
    protected String[] listFile(String path) {
        File curs = new File(path);
        String[] temp = curs.list();

        this.txt = new String("");

        if (temp != null)
            for (int i = 0; i < temp.length; i++) {
                this.txt = this.txt + " | " + temp[i];
                temp[i] = path + "/" + temp[i];
            }

        return temp;
    }

    public class ImageAdapter extends BaseAdapter {
        private Context mContext;

        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            // return 20;
            return listImages.length;
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) { // if it's not recycled, initialize some
                                        // attributes
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }

            // load the image
            imageView.setImageBitmap(BitmapFactory
                    .decodeFile(listImages[position]));

            // load drawable
            // imageView.setImageResource(R.drawable.ic_launcher);
            return imageView;
        }
    }

}

日志猫:

06-18 07:14:33.625: D/dalvikvm(521): GC freed 687 objects / 56160 bytes in 50ms
06-18 07:14:34.744: D/dalvikvm(521): GC freed 71 objects / 35824 bytes in 44ms
06-18 07:14:35.544: D/dalvikvm(521): GC freed 139 objects / 39392 bytes in 41ms
06-18 07:14:36.344: D/dalvikvm(521): GC freed 78 objects / 68928 bytes in 44ms
06-18 07:14:39.184: D/dalvikvm(521): GC freed 179 objects / 43424 bytes in 115ms
06-18 07:14:40.194: E/dalvikvm-heap(521): 3840000-byte external allocation too large for this process.
06-18 07:14:40.194: E/(521): VM won't let us allocate 3840000 bytes
06-18 07:14:40.194: D/skia(521): --- decoder->decode returned false
06-18 07:14:40.194: D/AndroidRuntime(521): Shutting down VM
06-18 07:14:40.194: W/dalvikvm(521): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
06-18 07:14:40.194: E/AndroidRuntime(521): Uncaught handler: thread main exiting due to uncaught exception
06-18 07:14:40.204: E/AndroidRuntime(521): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:271)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:296)
06-18 07:14:40.204: E/AndroidRuntime(521):     at test.test.TestActivity$ImageAdapter.getView(TestActivity.java:92)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.AbsListView.obtainView(AbsListView.java:1256)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.GridView.makeAndAddView(GridView.java:1218)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.GridView.makeRow(GridView.java:265)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.GridView.fillSpecific(GridView.java:465)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.GridView.layoutChildren(GridView.java:1123)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.AbsListView.onLayout(AbsListView.java:1113)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.view.View.layout(View.java:6830)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.view.View.layout(View.java:6830)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.view.View.layout(View.java:6830)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.view.View.layout(View.java:6830)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.view.View.layout(View.java:6830)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.view.ViewRoot.performTraversals(ViewRoot.java:996)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.os.Looper.loop(Looper.java:123)
06-18 07:14:40.204: E/AndroidRuntime(521):     at android.app.ActivityThread.main(ActivityThread.java:4363)
06-18 07:14:40.204: E/AndroidRuntime(521):     at java.lang.reflect.Method.invokeNative(Native Method)
06-18 07:14:40.204: E/AndroidRuntime(521):     at java.lang.reflect.Method.invoke(Method.java:521)
06-18 07:14:40.204: E/AndroidRuntime(521):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-18 07:14:40.204: E/AndroidRuntime(521):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-18 07:14:40.204: E/AndroidRuntime(521):     at dalvik.system.NativeStart.main(Native Method)
06-18 07:14:40.224: I/dalvikvm(521): threadid=7: reacting to signal 3
06-18 07:14:40.224: E/dalvikvm(521): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

第二步:

我尝试在 gridview 中加载图像,就像你说的 Luksprog 一样,但它不起作用。我想我不明白我怎么能做到这一点。

新的 TestActivity.java :

package test.test;

import java.io.File;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
    protected String[] listImages;
    protected String txt;
    protected BitmapFactory.Options opt;
    protected GridView.LayoutParams paramgrid;

    public TestActivity() {
        super();
        // search files
        if (this.listImages == null)
            this.listImages = listFile("/sdcard/projetv/test");

        // set options of thumbnails
        this.opt = new BitmapFactory.Options();
        this.opt.inPreferredConfig = Bitmap.Config.RGB_565;
        this.opt.inSampleSize = 3;
        this.paramgrid = new GridView.LayoutParams(85, 85);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        GridView gridview = (GridView) findViewById(R.id.gridview);

        // to show files name
        TextView text = (TextView) findViewById(R.id.txt);
        text.setText(this.txt);

        // load images in GridView
        ImageView imageView;
        for(int i=0;i<this.listImages.length;i++){
            imageView = new ImageView(this);
            imageView.setLayoutParams(this.paramgrid);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
            imageView.setImageBitmap(BitmapFactory.decodeFile(this.listImages[i], this.opt));

            gridview.addView(imageView, i);
        }

    }

    /*
     * Return list of files in the directory "path", with extention "ext"
     */
    protected String[] listFile(String path) {
        File curs = new File(path);
        String[] temp = curs.list();

        this.txt = new String("");

        if (temp != null)
            for (int i = 0; i < temp.length; i++) {
                this.txt = this.txt + " | " + temp[i];
                temp[i] = path + "/" + temp[i];
            }

        return temp;
    }
}

日志猫:

06-20 07:48:50.946: D/dalvikvm(520): GC freed 773 objects / 57808 bytes in 57ms
06-20 07:48:51.466: D/AndroidRuntime(520): Shutting down VM
06-20 07:48:51.466: W/dalvikvm(520): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
06-20 07:48:51.466: E/AndroidRuntime(520): Uncaught handler: thread main exiting due to uncaught exception
06-20 07:48:51.466: E/AndroidRuntime(520): java.lang.RuntimeException: Unable to start activity ComponentInfo{test.test/test.test.TestActivity}: java.lang.UnsupportedOperationException: addView(View, int) is not supported in AdapterView
06-20 07:48:51.466: E/AndroidRuntime(520):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
06-20 07:48:51.466: E/AndroidRuntime(520):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
06-20 07:48:51.466: E/AndroidRuntime(520):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
06-20 07:48:51.466: E/AndroidRuntime(520):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
06-20 07:48:51.466: E/AndroidRuntime(520):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 07:48:51.466: E/AndroidRuntime(520):     at android.os.Looper.loop(Looper.java:123)
06-20 07:48:51.466: E/AndroidRuntime(520):     at android.app.ActivityThread.main(ActivityThread.java:4363)
06-20 07:48:51.466: E/AndroidRuntime(520):     at java.lang.reflect.Method.invokeNative(Native Method)
06-20 07:48:51.466: E/AndroidRuntime(520):     at java.lang.reflect.Method.invoke(Method.java:521)
06-20 07:48:51.466: E/AndroidRuntime(520):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-20 07:48:51.466: E/AndroidRuntime(520):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-20 07:48:51.466: E/AndroidRuntime(520):     at dalvik.system.NativeStart.main(Native Method)
06-20 07:48:51.466: E/AndroidRuntime(520): Caused by: java.lang.UnsupportedOperationException: addView(View, int) is not supported in AdapterView
06-20 07:48:51.466: E/AndroidRuntime(520):     at android.widget.AdapterView.addView(AdapterView.java:448)
06-20 07:48:51.466: E/AndroidRuntime(520):     at test.test.TestActivity.onCreate(TestActivity.java:56)
06-20 07:48:51.466: E/AndroidRuntime(520):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-20 07:48:51.466: E/AndroidRuntime(520):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
06-20 07:48:51.466: E/AndroidRuntime(520):     ... 11 more
06-20 07:48:51.486: I/dalvikvm(520): threadid=7: reacting to signal 3
06-20 07:48:51.486: E/dalvikvm(520): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

你有更多信息可以帮助我吗?谢谢

4

1 回答 1

0

感谢 Luksprog 的指导,我在网上搜索并找到了一个很好的关于这个想法的教程:Displaying images from SD card in Android。有用 !我希望它也可以帮助某人。

于 2012-07-02T07:19:30.860 回答