讀取SD卡上某個資料夾下的所有圖片資源,並迴圈播放
阿新 • • 發佈:2018-11-21
sd卡上的圖片資料夾名稱是Pictures
獲取sd卡根路徑下的api
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"Pictures"+"/"+"01.jpg";//獲取視訊路徑
1先加讀取sd卡許可權
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2程式碼
private List<String> mListString =getImagePathFromSD(); private ImageSwitcher mImagSwitcher ; mImagSwitcher = (ImageSwitcher) findViewById(R.id.imagV_ad); if(mImagSwitcher!=null){ mImagSwitcher.setFactory(new ViewSwitcher.ViewFactory() { @Override public View makeView() { // makeView返回的是當前需要顯示的ImageView控制元件,用於填充進ImageSwitcher中 return new ImageView(SpeechMainActivity.this); } }); } if(mImagSwitcher!=null){ mImagSwitcher.postDelayed(new Runnable() { int currentIndex= 0; @Override public void run() { // mImagSwitcher.setImageResource(images[currentIndex]); Log.d(TAG, "mListString:"+mListString.get(currentIndex)); Bitmap bit = BitmapFactory.decodeFile(mListString.get(currentIndex)); Drawable drawable = new BitmapDrawable(bit); mImagSwitcher.setBackgroundDrawable(drawable); if(currentIndex ==(mListString.size() - 1)) currentIndex = 0; else currentIndex++; mImagSwitcher.postDelayed(this,1000); } },200); } /** * 從sd卡獲取圖片資源 * @return */ private List<String> getImagePathFromSD() { // 圖片列表 List<String> imagePathList = new ArrayList<String>(); // 得到sd卡內image資料夾的路徑 File.separator(/) // String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"Pictures"+"/"+"01.jpg";//獲取視訊路徑 String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+"Pictures"; // 得到該路徑資料夾下所有的檔案 File fileAll = new File(filePath); File[] files = fileAll.listFiles(); // 將所有的檔案存入ArrayList中,並過濾所有圖片格式的檔案 for (int i = 0; i < files.length; i++) { File file = files[i]; if (checkIsImageFile(file.getPath())) { imagePathList.add(file.getPath()); } } // 返回得到的圖片列表 return imagePathList; } /** * 檢查副檔名,得到圖片格式的檔案 * @param fName 檔名 * @return */ @SuppressLint("DefaultLocale") private boolean checkIsImageFile(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")|| FileEnd.equals("bmp") ) { isImageFile = true; } else { isImageFile = false; } return isImageFile; }