2014-10-01 89 views
0

我在应用程序中保存为jpg图像。在SD卡上的文件夹。下面的代码在gridview中拉取图像。使用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" 
    /> 

回答

0

你需要把您的图像放入数组

+0

下面的链接可以帮助你为我工作的http://www.compiletimeerror .com/2013/02/display-images-in-gridview.html#.VCtWelWSwb4 – Boldbayar 2014-10-01 01:20:23

+0

它的更多评论而不是答案 – 2014-10-01 04:20:50