Android根据文件路径加载指定文件

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

 如图:

        

代码:

 

public class Util {
	
	/****
	 * 计算文件大小
	 * 
	 * @param length
	 * @return
	 */
	public static String ShowLongFileSzie(Long length) {
		if (length >= 1048576) {
			return (length / 1048576) + "MB";
		} else if (length >= 1024) {
			return (length / 1024) + "KB";
		} else if (length < 1024) {
			return length + "B";
		} else {
			return "0KB";
		}
	}
	
	/***
	 * 
	 * 更具路径得到该路径下的全部图片信息
	 * @return
	 */
	
	public static List<AddFileInfo> getSDPathFrom() {
        // 图片列表
        List<AddFileInfo> imagePathList = new ArrayList<AddFileInfo>();
        // 得到sd卡内image文件夹的路径 
        String filePath = Environment.getExternalStorageDirectory().toString() + File.separator+"BigNoxHD/cache/";
        //得到该路径文件夹下所有的文件
        File fileAll = new File(filePath);
        File[] files = fileAll.listFiles();
        // 将所有的文件存入ArrayList中,并过滤所有图片格式的文件
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            if (checkIsFile(file.getPath())) {
            	String time = new SimpleDateFormat("yyyy-MM-dd")
				.format(new Date(file.lastModified()));
            	AddFileInfo info=new AddFileInfo(file.getName(), Util.ShowLongFileSzie(file.length()), time, file.getAbsolutePath());
                imagePathList.add(info);
            }
        }
        // 返回得到的图片列表
        return imagePathList;
    }

	/****
	 * 验证文件格式
	 * @param fName
	 * @return
	 */
	public static boolean checkIsFile(String fName) {
        boolean isImageFile = false;
        // 获取扩展名
        String FileEnd = fName.substring(fName.lastIndexOf(".") + 1,
                fName.length()).toLowerCase();
        if (FileEnd.equals("jpg") || FileEnd.equals("png") || FileEnd.equals("gif")|| FileEnd.equals("jpeg") ) {
            isImageFile = true;
        } else {
            isImageFile = false;
        }
        return isImageFile;
    }
	
	/****
	 * 根据文件路径获取图片
	 * 其中w和h你需要转换的大小
	 * @param path 
	 * @param w
	 * @param h
	 * @return
	 */
	public static Bitmap convertToBitmap(String path, int w, int h) {
		BitmapFactory.Options opts = new BitmapFactory.Options();
		// 设置为ture只获取图片大小
		opts.inJustDecodeBounds = true;
		opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
		// 返回为空
		BitmapFactory.decodeFile(path, opts);
		int width = opts.outWidth;
		int height = opts.outHeight;
		float scaleWidth = 0.f, scaleHeight = 0.f;
		if (width > w || height > h) {
			// 缩放
			scaleWidth = ((float) width) / w;
			scaleHeight = ((float) height) / h;
		}
		opts.inJustDecodeBounds = false;
		float scale = Math.max(scaleWidth, scaleHeight);
		opts.inSampleSize = (int)scale;
		WeakReference<Bitmap> weak = new WeakReference<Bitmap>(BitmapFactory.decodeFile(path, opts));
		return Bitmap.createScaledBitmap(weak.get(), w, h, true);
	}
}


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" />

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值