切切歆语的博客

人生逍遥乐对,红尘千般滋味;做人就好比游泳,游累了也是不能停的。

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

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

如图:

       


MainActivity.java

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加权限哦!

<!-- SD卡权限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

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

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/DickyQie/article/details/53994016
个人分类: Android
上一篇android--------根据文件路径使用File类获取文件相关信息
下一篇Android加载SD卡目录,文件夹遍历,图片设置,设置文件对应打开方式等
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭