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

来源:互联网 发布:reverse软件下载 编辑:程序博客网 时间:2024/05/16 12:54

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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();}private void initView() {// TODO Auto-generated method stubmListview = (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);}@Overridepublic int getCount() {return list.size();}@Overridepublic Object getItem(int position) {return list.get(position);}@Overridepublic long getItemId(int position) {return 0;}@Overridepublic 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
原创粉丝点击