2012-06-15 143 views
1

我正在使用Google's Hello GridView Tutorial瞭解如何使用GridView。它與Drawable正常工作,我可以將許多項目添加到GridView。在運行應用程序時,我可以滾動並旋轉手機。GridView + ImageView + rotate =意外停止?

但是,如果我從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; 
     } 
    } 

} 

的logcat:

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; 
    } 
} 

的logcat:

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 

你有更多的信息來幫助我嗎?謝謝

+1

從logcat中添加異常堆棧跟蹤。 – Luksprog

+0

對不起,我忘了它 – user1458955

+0

因爲您一次又一次地解碼適配器中的每個圖像,所以您的內存不足。你應該在'GridView'中延遲加載這些圖像。 – Luksprog

回答