在android中列出图像缩略图名称和图像大小

时间:2013-02-14 12:51:00

标签: android listview imageview adapter

在我的Android应用程序中,我正在致命的例外。我列出图像信息,如缩略图,图像名称和图像大小。 但是当我试图在其给出异常下面执行代码时。 这是代码 -

 public class ImportFile<Context> extends ListActivity {
 String names[];
 Bitmap bb[];
 long size1[];
 Intent i;
  private String myFile;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylist); 

        File file = new File(Environment.getExternalStorageDirectory()
            + File.separator
            + "gallery" //folder name
        );
        if (!file.exists()) {
            file.mkdirs();
        }

        setListAdapter(new IconicAdapter());

    }

    class IconicAdapter extends ArrayAdapter<String> {
        IconicAdapter() {
            super(ImportFile.this, R.layout.row, R.id.lable, names);
        }

        public View getView(int position,View convertview,ViewGroup parent){
            View row=super.getView(position, convertview, parent);
            ImageView icon=(ImageView)row.findViewById(R.id.icon);

        icon.setImageBitmap(bb[position]);
        TextView size=(TextView) row.findViewById(R.id.size);

        size.setText(size1[position]+"");
        return row;
        }

    }

    //read from sdcard
    private void getSD() {
        List<String> item = new ArrayList<String>();
        File f = new File("/sdcard/download");
        File[] files = f.listFiles();

        int j=0;
        for(int i = 0; i < files.length; i++) {
            File file = files[i];
            //take the file name only
            long size = file.length()/1024;

            myFile = file.getPath().substring(file.getPath().lastIndexOf("/")+1,file.getPath().length()).toLowerCase(); 

            if(myFile.endsWith(".jpeg")||myFile.endsWith(".png")|| myFile.endsWith(".gif") || myFile.endsWith(".jpg"))
            {
                names[j]=myFile;

                size1[j]=size;

                Bitmap b=ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(file.getPath()), 50,50);
                bb[j]=b;
                j++;
            item.add(b +myFile+"             "+"Size:"+size+" KB" );

            }

        }

    }
    }

以下是例外 -

02-14 18:00:44.267: E/AndroidRuntime(909): FATAL EXCEPTION: main
02-14 18:00:44.267: E/AndroidRuntime(909): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.zproject/com.my.zproject.ImportFile}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
02-14 18:00:44.267: E/AndroidRuntime(909):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-14 18:00:44.267: E/AndroidRuntime(909):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-14 18:00:44.267: E/AndroidRuntime(909):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-14 18:00:44.267: E/AndroidRuntime(909):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-14 18:00:44.267: E/AndroidRuntime(909):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 18:00:44.267: E/AndroidRuntime(909):  at android.os.Looper.loop(Looper.java:123)
02-14 18:00:44.267: E/AndroidRuntime(909):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-1 4 18:00:44.267: E/AndroidRuntime(909):     at java.lang.reflect.Method.invokeNative(Native Method)
02-14 18:00:44.267: E/AndroidRuntime(909):  at java.lang.reflect.Method.invoke(Method.java:521)
02-14 18:00:44.267: E/AndroidRuntime(909):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-14 18:00:44.267: E/AndroidRuntime(909):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-14 18:00:44.267: E/AndroidRuntime(909):  at dalvik.system.NativeStart.main(Native Method)
  02-14 18:00:44.267: E/AndroidRuntime(909): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

02-14 18:00:44.267: E/AndroidRuntime(909): at android.app.ListActivity.onContentChanged(ListActivity.java:245)
  02-14 18:00:44.267: E/AndroidRuntime(909): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:201)
  02-14 18:00:44.267: E/AndroidRuntime(909): at   android.app.Activity.setContentView(Activity.java:1647)
  02-14 18:00:44.267: E/AndroidRuntime(909): at com.my.zproject.ImportFile.onCreate(ImportFile.java:33)

这里是row.xml -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >


<ImageView 
    android:id="@+id/icon"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_vertical"
  android:padding="2dip"

    />

<LinearLayout 
    android:layout_width="fill_parent"
 android:layout_height="wrap_content"
android:orientation="vertical"
    >
    <TextView 
        android:id="@+id/lable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        />
     <TextView 
        android:id="@+id/size"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
     </LinearLayout>
   </LinearLayout>

这里是mylist.xml

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
   <ListView 
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
    android:id="@+id/list" >
  </ListView>
   </LinearLayout>

1 个答案:

答案 0 :(得分:1)

尝试将ListView的ID更改为@android:id/list