效果圖示例android
//把手機裏sdcard的全部圖片找出來 像第一張圖那樣緩存
//而後點擊其中任意一張圖片時 就查看原圖ide
=====================================================工具
一、添加讀和寫的權限佈局
二、activity_main.xml佈局文件this
代碼url
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >code
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:numColumns="auto_fit" />orm
<TextView
android:id="@+id/empty_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/gridview"
android:text="沒有圖片數據" />xml
</RelativeLayout>
-----------------------------------------------
三、寫一個工具類用於對sdcard的讀寫 的操做
代碼
//操做SDCARD的文件工具類
public class FileUtil {
//存放圖片的路徑
private static final String CACHE_DIR = Environment.getExternalStorageDirectory() + "/my_caches/images";
private static int COMP_JPG = 0;
private static int COMP_PNG = 1;
//判斷sdcard 是否掛載(是否有sdcard)
public static boolean isMounted(){
//獲取手機設備的sdcard狀態
String state = Environment.getExternalStorageState();
//MOUNTED -- 安裝
return state.equals(Environment.MEDIA_MOUNTED);
}
//獲取sdcard文件 根路徑的絕對路徑
public static String getSDCARD(){
return Environment.getExternalStorageDirectory().getAbsolutePath();
}
//獲取文件名
public static String getFilename(String url){
return url.substring(url.lastIndexOf('/') + 1);
}
//保存文件 方法1
public static void sava1(String url,byte[] data){
//判斷是否有sdcard
if(!isMounted()){
return ;
}
//有sdcard
//判斷是否有緩存文件夾
File dir = new File(CACHE_DIR);
if(!dir.exists()){
//不存在緩存文件夾 建立文件夾用來保存文件
dir.mkdirs();
}
//把文件 數據存到sdcard
File file = new File(dir,getFilename(url));
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//保存文件 方法2
public static void sava2(String url,Bitmap bitmap,int format){
//判斷 是否有sdcard
if(!isMounted()){
return ;
}
File dir = new File(CACHE_DIR);
if(!dir.exists()){
dir.mkdirs();
}
//把 文件數據 寫到 sdcard
File file = new File(dir,getFilename(url));
try {
FileOutputStream fos = new FileOutputStream(file);
//把圖片文件寫入緩存
bitmap.compress((format == COMP_JPG?CompressFormat.JPEG:CompressFormat.PNG), 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//讀取圖片
public static Bitmap readImage(String url){
if(!isMounted()){
return null;
}
File file = new File(CACHE_DIR,getFilename(url));
if(file.exists()){
return BitmapFactory.decodeFile(file.getAbsolutePath());
}
return null;
}
//清空 緩存目錄
public void clearCaches(){
File dir = new File(CACHE_DIR);
File[] file_datas = dir.listFiles();
for(File file : file_datas){
file.delete();
}
}
//生成略縮圖
public static Bitmap creatThumbnail(String filepath,int newWidth,int newHeight){
BitmapFactory.Options options = new BitmapFactory.Options();
//
options.inJustDecodeBounds = true;
//目的:不生成bitmap數據,可是要獲取原圖的參數值
BitmapFactory.decodeFile(filepath, options);
//原有寬高
int originalWidth = options.outWidth;
int originalHeight = options.outHeight;
//按比例縮小的寬高
int width = originalWidth/newWidth;
int height = originalHeight/newHeight;
options.inSampleSize = height > width? height : width;
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filepath, options);
}
}
---------------------------------------------------------------
MainActivity 類
代碼
public class MainActivity extends Activity {
private MyAdapter adapter;
private GridView gridview;
private TextView empty_textview;
private List<String> img_path = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.gridview = (GridView) this.findViewById(R.id.gridview);
this.empty_textview = (TextView) this.findViewById(R.id.empty_textview);
//setEmptyView方法 -- gridview沒有數據 就會啓動該方法
gridview.setEmptyView(empty_textview);
//獲取sdcard根目錄
String sd_path = FileUtil.getSDCARD();
//使用遞歸 遍歷 sdcard 把找到的圖片 設置到gridview
find_images(sd_path);
adapter = new MyAdapter(img_path);
gridview.setAdapter(adapter);
}
//查找圖片的 遞歸 方法
private void find_images(String sd_path) {
File file = new File(sd_path);
//用file.listFiles()獲取file目錄下的全部文件夾和文件
File[] files = file.listFiles();
if(files != null && files.length > 0){
for(File f :files){
if(f.isDirectory()){//若是是文件夾繼續查找
find_images(f.getAbsolutePath());
}else{
//若是是文件 在判斷是不是 圖片
if(f.getPath().endsWith("jpg") || f.getPath().endsWith("png")){
//把圖片路徑 保存在一個集合裏
// Log.i("data", "img_path:" + img_path.toString());
img_path.add(f.getPath());
}
}
}
}
}
//適配器類
class MyAdapter extends BaseAdapter{
private List<String> list; public MyAdapter(List<String> img_path) { this.list = img_path; } @Override public int getCount() { return this.list.size(); } @Override public Object getItem(int position) { return this.list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ImageView imageview = null; if(convertView == null){ imageview = new ImageView(parent.getContext()); imageview.setMaxHeight(150); imageview.setMaxWidth(90); imageview.setBackgroundResource(R.drawable.image_border); imageview.setPadding(5, 5, 5, 5); }else{ imageview = (ImageView) convertView; } //設置數據 -- 調用工具類的略縮圖方法 -- 把點中的圖片修改 Bitmap bitmap = FileUtil.creatThumbnail(this.list.get(position), 150, 90); imageview.setImageBitmap(bitmap); //每張圖片 的點擊 事件 //點中後 查看原圖 imageview.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction(intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File(list.get(position))), "image/*"); startActivity(intent); } }); return imageview; } }}