如何在gridview中显示带有图像名称的图像图标

时间:2014-10-01 01:11:34

标签: android

我在应用中将图片保存为jpg。 SD卡上的文件夹。下面的代码在gridview中提取图像。使用gridview我想在gridview中显示图像图标以及图像的名称。我怎样才能做到这一点?感谢

ImageAdapter myImageAdapter;
GridView gridview;


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

      gridview = (GridView) findViewById(R.id.gridview);
        myImageAdapter = new ImageAdapter(this);
        gridview.setAdapter(myImageAdapter);


        String ExternalStorageDirectoryPath = Environment.getExternalStorageDirectory().getAbsolutePath(); 
        String targetPath = ExternalStorageDirectoryPath + "/MyApp/";


         File targetDirectory = new File(targetPath);
        File[] files = targetDirectory.listFiles();
        for (File file : files){


         myImageAdapter.add(file.getAbsolutePath());

        }

// my_layout.xml

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



 <GridView
    android:id="@+id/gridview"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:layout_marginTop="10dp"
    android:gravity="center"
    />

1 个答案:

答案 0 :(得分:0)

您需要将图像放入数组