创建自定义库,在gridview中显示特定外部文件夹中的图像?

时间:2018-04-18 14:10:26

标签: java android gridview android-external-storage

我正在尝试创建一个自定义图库,该图库将显示外部存储上某个文件夹中的所有图像。我已经尝试了我在stackoverflow上找到的所有东西,并且还找不到适用于我的东西。目前我有2个图像,当你创建一个硬编码到适配器中的新Android Studio项目时。到目前为止我的代码使用了GridView,遵循Android开发人员教程https://developer.android.com/guide/topics/ui/layout/gridview.html,所以如果有人能给出使用android开发人员gridview代码的答案,那就太棒了。我是stackoverflow的新手,也是Android开发和编码的新手,所以如果你能解释一下它的原因,我会说它。这是我工作的代码:

GalleryActivity.java(主要)

public class GalleryActivity extends AppCompatActivity {

GridView gridView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gallery);

    gridView = findViewById(R.id.grid_galllery);
    gridView.setAdapter(new ImageAdapter(this));
}

}

ImageAdapter.java(适配器)

class ImageAdapter extends BaseAdapter {

    Context mContext;

    public ImageAdapter (Context context) {
        mContext = context;
    }

    @Override
    public int getCount() {
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent 
    {
        ImageView mImageView;

        if (convertView == null) {
            mImageView = new ImageView(mContext);
            mImageView.setLayoutParams(new ViewGroup.LayoutParams(85, 
            85));
            mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            mImageView.setPadding(8, 8, 8, 8);
        }
        else {
            mImageView = (ImageView) convertView;
        }

        mImageView.setImageResource(mThumbIds[position]);
        return mImageView;
    }

    private Integer[] mThumbIds = {
        R.drawable.ic_launcher_background,
        R.drawable.ic_launcher_foreground
    };

activity_gallery.xml(主要布局)

<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_galllery"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />

0 个答案:

没有答案