Skip to content

Commit

Permalink
第一个页面终于可以显示所有图片并且能滑动了
Browse files Browse the repository at this point in the history
  • Loading branch information
tycw committed Jan 28, 2019
1 parent 768193e commit 76d956c
Show file tree
Hide file tree
Showing 12 changed files with 638 additions and 31 deletions.
7 changes: 1 addition & 6 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

103 changes: 103 additions & 0 deletions app/src/main/java/com/taifua/material/FileManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.taifua.material;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

public class FileManager {
private static FileManager mInstance;
private static Context mContext;
private static ContentResolver mContentResolver;
private static Object mLock = new Object();

public static FileManager getInstance(Context context) {
if (mInstance == null) {
synchronized (mLock) {
if (mInstance == null) {
mInstance = new FileManager();
mContext = context;
mContentResolver = context.getContentResolver();
}
}
}
return mInstance;
}


/**
* 通过图片文件夹的路径获取该目录下的图片
*/
public List<String> getImgListByDir(String dir) {
ArrayList<String> imgPaths = new ArrayList<>();
File directory = new File(dir);
if (directory == null || !directory.exists()) {
return imgPaths;
}
File[] files = directory.listFiles();
for (File file : files) {
String path = file.getAbsolutePath();
if (FileUtils.isPicFile(path)) {
imgPaths.add(path);
}
}
return imgPaths;
}

/**
* 得到图片文件夹集合
*/
public List<ImgFolderBean> getImageFolders() {
List<ImgFolderBean> folders = new ArrayList<ImgFolderBean>();
// 扫描图片
Cursor c = null;
try {
c = mContentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null,
MediaStore.Images.Media.MIME_TYPE + "= ? or " + MediaStore.Images.Media.MIME_TYPE + "= ?",
new String[]{"image/jpeg", "image/png"}, MediaStore.Images.Media.DATE_MODIFIED);
List<String> mDirs = new ArrayList<String>();//用于保存已经添加过的文件夹目录
while (c.moveToNext()) {
String path = c.getString(c.getColumnIndex(MediaStore.Images.Media.DATA));// 路径
File parentFile = new File(path).getParentFile();
if (parentFile == null)
continue;

String dir = parentFile.getAbsolutePath();
if (mDirs.contains(dir))//如果已经添加过
continue;

mDirs.add(dir);//添加到保存目录的集合中
ImgFolderBean folderBean = new ImgFolderBean();
folderBean.setDir(dir);
folderBean.setFistImgPath(path);
if (parentFile.list() == null)
continue;
int count = parentFile.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
if (filename.endsWith(".jpeg") || filename.endsWith(".jpg") || filename.endsWith(".png")) {
return true;
}
return false;
}
}).length;

folderBean.setCount(count);
folders.add(folderBean);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (c != null) {
c.close();
}
}

return folders;
}
}
115 changes: 115 additions & 0 deletions app/src/main/java/com/taifua/material/FileUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.taifua.material;

import android.os.Environment;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class FileUtils {

/**文档类型*/
public static final int TYPE_DOC = 0;
/**apk类型*/
public static final int TYPE_APK = 1;
/**压缩包类型*/
public static final int TYPE_ZIP = 2;


/**
* 判断文件是否存在
* @param path 文件的路径
* @return
*/
public static boolean isExists(String path) {
File file = new File(path);
return file.exists();
}

public static int getFileType(String path) {
path = path.toLowerCase();
if (path.endsWith(".doc") || path.endsWith(".docx") || path.endsWith(".xls") || path.endsWith(".xlsx")
|| path.endsWith(".ppt") || path.endsWith(".pptx")) {
return TYPE_DOC;
}else if (path.endsWith(".apk")) {
return TYPE_APK;
}else if (path.endsWith(".zip") || path.endsWith(".rar") || path.endsWith(".tar") || path.endsWith(".gz")) {
return TYPE_ZIP;
}else{
return -1;
}
}


/**通过文件名获取文件图标
public static int getFileIconByPath(String path){
path = path.toLowerCase();
int iconId = R.mipmap.unknow_file_icon;
if (path.endsWith(".txt")){
iconId = R.mipmap.type_txt;
}else if(path.endsWith(".doc") || path.endsWith(".docx")){
iconId = R.mipmap.type_doc;
}else if(path.endsWith(".xls") || path.endsWith(".xlsx")){
iconId = R.mipmap.type_xls;
}else if(path.endsWith(".ppt") || path.endsWith(".pptx")){
iconId = R.mipmap.type_ppt;
}else if(path.endsWith(".xml")){
iconId = R.mipmap.type_xml;
}else if(path.endsWith(".htm") || path.endsWith(".html")){
iconId = R.mipmap.type_html;
}
return iconId;
}
*/

/**是否是图片文件*/
public static boolean isPicFile(String path){
path = path.toLowerCase();
if (path.endsWith(".jpg") || path.endsWith(".jpeg") || path.endsWith(".png")){
return true;
}
return false;
}


/** 判断SD卡是否挂载 */
public static boolean isSDCardAvailable() {
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
return true;
} else {
return false;
}
}

/**
* 从文件的全名得到文件的拓展名
*
* @param filename
* @return
*/
public static String getExtFromFilename(String filename) {
int dotPosition = filename.lastIndexOf('.');
if (dotPosition != -1) {
return filename.substring(dotPosition + 1, filename.length());
}
return "";
}
/**
* 读取文件的修改时间
*
* @param f
* @return
*/
public static String getModifiedTime(File f) {
Calendar cal = Calendar.getInstance();
long time = f.lastModified();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cal.setTimeInMillis(time);
// System.out.println("修改时间[2] " + formatter.format(cal.getTime()));
// 输出:修改时间[2] 2009-08-17 10:32:38
return formatter.format(cal.getTime());
}


}
21 changes: 21 additions & 0 deletions app/src/main/java/com/taifua/material/Images.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.taifua.material;

import android.content.Context;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class Images {
private static List<ImgFolderBean> imgFolderList;
public static List<String> imageThumbUrls = new ArrayList<>();
public static void init(Context context){
FileManager fileManager = FileManager.getInstance(context);
imgFolderList = fileManager.getImageFolders();
for (ImgFolderBean imgFolderBean:imgFolderList){
for (String s:fileManager.getImgListByDir(imgFolderBean.getDir()))
imageThumbUrls.add(s);
}
Log.d("图片总共有多少: ",""+imageThumbUrls.size());
}
}
53 changes: 53 additions & 0 deletions app/src/main/java/com/taifua/material/ImgFolderBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.taifua.material;

public class ImgFolderBean {
/**当前文件夹的路径*/
private String dir;
/**第一张图片的路径,用于做文件夹的封面图片*/
private String fistImgPath;
/**文件夹名*/
private String name;
/**文件夹中图片的数量*/
private int count;

public ImgFolderBean(){}

public ImgFolderBean(String dir, String fistImgPath, String name, int count) {
this.dir = dir;
this.fistImgPath = fistImgPath;
this.name = name;
this.count = count;
}

public String getDir() {
return dir;
}

public void setDir(String dir) {
this.dir = dir;
}

public String getFistImgPath() {
return fistImgPath;
}

public void setFistImgPath(String fistImgPath) {
this.fistImgPath = fistImgPath;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}
}
18 changes: 8 additions & 10 deletions app/src/main/java/com/taifua/material/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.GravityCompat;
Expand All @@ -16,31 +14,32 @@
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

/**
* 在这里把所有碎片和控件建好
* 然后再在fragment类里面写不同的页面
*/
public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener
{

private DrawerLayout mDrawerLayout;
private ViewPager viewPager;
private NavigationView navView;
BottomNavigationView bottomNavigationView;
private BottomNavigationView bottomNavigationView;
private PhotoFragment photoFragment = new PhotoFragment();
private AlbumFragment albumFragment = new AlbumFragment();
private AssistantFragment assistantFragment = new AssistantFragment();
private ShareFragment shareFragment = new ShareFragment();

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case android.R.id.home:
Expand All @@ -56,8 +55,7 @@ public boolean onOptionsItemSelected(MenuItem item)
}

@Override
protected void onCreate(Bundle savedInstanceState)
{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
Expand Down
Loading

0 comments on commit 76d956c

Please sign in to comment.