android--------根据文件路径加载指定文件

2019-11-03  本文已影响0人  切切歆语

Android根据指定的文件路径,加载该路径下指定文件格式(图片格式 png, gif,jpg jpeg)的文件相关信息的列表。

如图:


image
public class MainActivity extends Activity {

    private ListView mListview;
    private List<AddFileInfo> list;

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

    private void initView() {
        // TODO Auto-generated method stub
        mListview = (ListView) findViewById(R.id.listview);
        list = Util.getSDPathFrom();
        mListview.setAdapter(new Adapter(MainActivity.this));
    }

    class Adapter extends BaseAdapter {
        private LayoutInflater inflater;
        private Context context;

        public Adapter(Context context) {
            this.context = context;
            this.inflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (null == convertView) {
                convertView = inflater.inflate(
                        R.layout.item_mytask_file_listview, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            AddFileInfo info = (AddFileInfo) getItem(position);
            holder.img.setImageBitmap(Util.convertToBitmap(info.getPath(), 99, 99));
            holder.tv_name.setText("文件名称:" + info.getName());
            holder.size.setText("文件大小:" + info.getSize());
            holder.time.setText("文件创建时间:" + info.getTime());
            return convertView;
        }

    }

    class ViewHolder {

        private TextView tv_name;
        private TextView size;
        private TextView time;
        private ImageView img;

        public ViewHolder(View view) {
            img = (ImageView) view.findViewById(R.id.img);
            tv_name = (TextView) view.findViewById(R.id.item_file_name);
            size = (TextView) view.findViewById(R.id.item_file_size);
            time = (TextView) view.findViewById(R.id.item_file_time);
        }
    }
}

不要忘记在AndroidManifest.xml加权限哦!

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

源码点击下载:https://github.com/DickyQie/android-file

上一篇下一篇

猜你喜欢

热点阅读