GridView實現載入本地所有圖片
阿新 • • 發佈:2019-02-11
public class MainActivity extends Activity {
private GridView gridView;
// 獲取所有圖片的路徑
List<String> imagePath = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView1);
String sdpath=Environment.getExternalStorageDirectory()+"/";
getFiles(sdpath);
if(imagePath.size()<1){
return;
}
final MyAdapter adapter = new MyAdapter();
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
imagePath.remove(position);
adapter.notifyDataSetChanged();
}
});
}
// 獲取所有圖片格式
private static final String[] imageFromSet = new String[] { "jpg", "png",
"gif" };
// 判斷是否是圖片
private static boolean isImageFile(String path) {
for (String imagePath : imageFromSet) {
if (path.contains(imagePath)) {
return true;
}
}
return false;
}
//遍歷指定路徑
private void getFiles(String url){
//建立檔案物件
File files=new File(url);
File[] file=files.listFiles();
try {
for(File f:file){
if(f.isDirectory()){//如果是目錄
getFiles(f.getAbsolutePath());//遞迴呼叫
}else{
if(isImageFile(f.getPath())){
imagePath.add(f.getPath());//將圖片路徑加入到list集合中
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return imagePath.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return imagePath.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if(convertView==null){
imageView=new ImageView(MainActivity.this);
//設定影象的寬和高
imageView.setAdjustViewBounds(true);
imageView.setMaxWidth(500);
imageView.setMaxHeight(400);
imageView.setPadding(5, 5, 5, 5);
}else{
imageView=(ImageView) convertView;
}
Bitmap bitmap = BitmapFactory.decodeFile(imagePath.get(position));
imageView.setImageBitmap(bitmap);
return imageView;
}
}
}
private GridView gridView;
// 獲取所有圖片的路徑
List<String> imagePath = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView1);
String sdpath=Environment.getExternalStorageDirectory()+"/";
getFiles(sdpath);
if(imagePath.size()<1){
return;
}
final MyAdapter adapter = new MyAdapter();
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
imagePath.remove(position);
adapter.notifyDataSetChanged();
}
});
}
// 獲取所有圖片格式
private static final String[] imageFromSet = new String[] { "jpg", "png",
"gif" };
// 判斷是否是圖片
private static boolean isImageFile(String path) {
for (String imagePath : imageFromSet) {
if (path.contains(imagePath)) {
return true;
}
}
return false;
}
//遍歷指定路徑
private void getFiles(String url){
//建立檔案物件
File files=new File(url);
File[] file=files.listFiles();
try {
for(File f:file){
if(f.isDirectory()){//如果是目錄
getFiles(f.getAbsolutePath());//遞迴呼叫
}else{
if(isImageFile(f.getPath())){
imagePath.add(f.getPath());//將圖片路徑加入到list集合中
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return imagePath.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return imagePath.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if(convertView==null){
imageView=new ImageView(MainActivity.this);
//設定影象的寬和高
imageView.setAdjustViewBounds(true);
imageView.setMaxWidth(500);
imageView.setMaxHeight(400);
imageView.setPadding(5, 5, 5, 5);
}else{
imageView=(ImageView) convertView;
}
Bitmap bitmap = BitmapFactory.decodeFile(imagePath.get(position));
imageView.setImageBitmap(bitmap);
return imageView;
}
}
}