从GalleryView中的外部存储加载图像

时间:2015-05-24 13:53:59

标签: java android gallery

我想将外部存储中的图像加载到android中的Gallery。我有文件路径。我想在图库中显示Mix文件夹的所有图片。

File file = new File(Environment.getExternalStorageDirectory().getPath() +"/Gallery/Mix/");

当我将图像放在可绘制文件夹中时,我可以在图库中加载图像,但我应该在哪里和哪些更改所以我可以显示混合文件夹中的图像。

PS。我知道Gallery已被弃用但仍需要使用它。

MainActivity.java

public class MainActivity extends Activity {

Gallery gallery;
ImageView imageView;

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

    gallery = (Gallery)findViewById(R.id.gallery1);
    imageView = (ImageView)findViewById(R.id.imageView1);

    gallery.setAdapter(new ImageAdapter(this));

    gallery.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(), position+"", Toast.LENGTH_SHORT).show();
            imageView.setImageResource(ImageAdapter.ThumbsIds[position]);
        }
    });
}
}

ImageAdapter.java

public class ImageAdapter extends BaseAdapter implements SpinnerAdapter {

    private Context context;

    public ImageAdapter(Context context) {
        this.context = context;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = null;
        if(convertView==null){
            imageView = new ImageView(context);
            imageView.setLayoutParams(new Gallery.LayoutParams(600,550));
            imageView.setPadding(4, 4, 4, 4);
        }else{
            imageView = (ImageView)convertView;
        }

        imageView.setImageResource(ThumbsIds[position]);
        return imageView;
    }

    public static Integer[] ThumbsIds={
            R.drawable.ppic1,
            R.drawable.ppic2,
            R.drawable.ppic3
    };
}

0 个答案:

没有答案