0

我正在尝试插入图像ListView,当我单击它以在新的图像视图(更大的视图)中显示图像时。

我没有 Android 手机来执行我的应用程序。

这是我的 ImageViewer 类。

public class ImageViewer extends ListActivity {

   Uri uri;
   Cursor c;
   ListView l;
   ArrayList<String> al1 = new ArrayList<String>();
   ArrayList<String> al2 = new ArrayList<String>();
   int data, dname;
   String dt, disnam, imgid;
   int id;
   String type;


   @Override
   protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);

      uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
      String project[] = {MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.DATA,
            MediaStore.Images.Media._ID};
      c = getContentResolver().query(uri, project, null, null, null);
      data = c.getColumnIndex(MediaStore.Images.Media.DATA);
      dname = c.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME);
      id = c.getColumnIndex(MediaStore.Images.Media._ID);

      if (c.moveToFirst()) {
         do {
            dt = c.getString(data);
            disnam = c.getString(dname);
            imgid = c.getString(id);
            al1.add(dt);
            al2.add(disnam);
         } while (c.moveToNext());
      }
      l = getListView();
      l.setAdapter(new MyAdapter());
      l.setOnItemClickListener(new OnItemClickListener() {

         @Override
         public void onItemClick(
               AdapterView<?> arg0,
               View arg1, int arg2, long arg3
         ) {

            String filename = al1.get(arg2);
            Intent intent = new Intent(ImageViewer.this, Viewer.class);
            intent.putExtra("image2view", filename);
            intent.putExtra("type", type);
            intent.putExtra("id", id);
            startActivity(intent);
         }
      });
   }


   public class MyAdapter extends BaseAdapter {

      @Override
      public int getCount() {
         // TODO Auto-generated method stub
         return 0;
      }

      @Override
      public Object getItem(int arg0) {
         // TODO Auto-generated method stub
         return null;
      }

      @Override
      public long getItemId(int arg0) {
         // TODO Auto-generated method stub
         return 0;
      }

      @Override
      public View getView(int pos, View convertview, ViewGroup vg) {
         // TODO Auto-generated method stub


         View row = convertview;
         if (row == null) {
            LayoutInflater inf = getLayoutInflater();
            row = inf.inflate(R.layout.image, vg, false);
         }

         TextView filename = (TextView) row.findViewById(R.id.ImageName);
         filename.setText(al2.get(pos) + "\n");

         ImageView thumbs = (ImageView) row.findViewById(R.id.Thumb);
         thumbs.setImageResource(id);
         thumbs.setScaleType(ImageView.ScaleType.CENTER_CROP);
         thumbs.setPadding(8, 8, 28, 8);

         return row;
      }
   }
}

这是我的查看器类,它必须全屏显示所选图像。

public class Viewer extends Activity{
    
    ImageView iv;
    TextView tv;
    public void onCreate(Bundle b1){
        super.onCreate(b1);
        
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        
        setContentView(R.layout.imageview);
                System.gc();
                
        Bundle b2=getIntent().getExtras();
        //String file=b2.getString("image2view");
        int id=b2.getInt("id");
        
        iv=(ImageView)findViewById(R.id.imageView101);
        iv.setImageResource(id);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                R.layout.help_title);
        ImageView iv = (ImageView) findViewById(R.id.header);
        iv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new Intent();
                setResult(RESULT_OK);
                finish();
            }
        });
    }
}

用于ImageViewer类的 xml 文件是 image.xml。

<RelativeLayout 
    android:id="@+id/RelativeLayout02" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:background="@drawable/mainbkgnd" 
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageView
        android:id="@+id/Thumb"
        android:layout_width="50dp"
        android:layout_height="70dp"
        android:src="@drawable/ic_launcher"/>

    <TextView 
        android:layout_width="wrap_content" 
        android:layout_toRightOf="@+id/Thumb" 
        android:layout_height="wrap_content" 
        android:id="@+id/ImageName"
        android:text="Image Name"
        android:textColor="#000000">
    </TextView>
    
</RelativeLayout>

Viewer 类的 xml 代码是 imageview.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:id="@+id/itextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:visibility="gone" />

    <ImageView
        android:id="@+id/imageView101"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.62"
        android:src="@drawable/ic_launcher" />
    
</LinearLayout>

我搜索但找不到合适的代码。

4

1 回答 1

1

我不相信这个id

id=c.getColumnIndex(MediaStore.Images.Media._ID);

是否适合以下资源id

iv.setImageResource(id);

您应该可以在查看器的 onCreate() 中执行此操作:

String filename = b2.getString("filename");
if(filename != null)
    iv.setImageUri(Uri.fromFile(new File(filename)));
于 2012-06-24T18:56:44.067 回答