Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android系統教程 >> Android開發教程 >> Android依據文件路徑加載指定文件

Android依據文件路徑加載指定文件

編輯:Android開發教程

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

 

 

 

源碼點擊下載

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved