Android扫描SD卡中所有图片,获得图片的名称、路径、大小
2015-03-27 16:16
375 查看
public class MainActivity extends Activity {
private ListView mylist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mylist = (ListView)findViewById(R.id.mylist);
SimpleAdapter adapter = new SimpleAdapter(this, ScanPhoto(), R.layout.item, new String[] {"PhotoTitle","PhotoUrl","PhotoSize"},
new int[]{R.id.Title,R.id.Url,R.id.Size});
mylist.setAdapter(adapter);
mylist.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
//单击执行别的操作
}
});
}
public ArrayList<HashMap<String, Object>> ScanPhoto() {
//生成动态数组
ArrayList<HashMap<String, Object>> mylist = new ArrayList<HashMap<String, Object>>();
//查询媒体数据库
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Images.Media.DEFAULT_SORT_ORDER);
//遍历媒体数据库
if(cursor.moveToFirst()){
while (!cursor.isAfterLast()) {
//图片名称
String tilte ="名称:"+ cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.TITLE));
//图片路径
String Url= cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
String url ="路径:"+ Url;
//大小 :MediaStore.Audio.Media.SIZE
Long Size = (Long)cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE))/1024;
String size= "大小:"+Size+"KB";
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("PhotoTitle", tilte);
map.put("PhotoUrl", url);
map.put("PhotoSize",size);
mylist.add(map);
cursor.moveToNext();
}
}
return mylist;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main
<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="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:text="图库"
android:textSize="28sp" />
<View
android:id="@+id/view1"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:layout_marginTop="35dip"
android:background="@drawable/devide_line" />
<ListView
android:id="@+id/mylist"
android:layout_alignTop="@+id/view1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" >
</ListView>
</RelativeLayout>
item
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="TextView" />
<TextView
android:id="@+id/Url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/Title"
android:text="TextView" />
<TextView
android:id="@+id/Size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/Url"
android:text="TextView" />
</RelativeLayout>
private ListView mylist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mylist = (ListView)findViewById(R.id.mylist);
SimpleAdapter adapter = new SimpleAdapter(this, ScanPhoto(), R.layout.item, new String[] {"PhotoTitle","PhotoUrl","PhotoSize"},
new int[]{R.id.Title,R.id.Url,R.id.Size});
mylist.setAdapter(adapter);
mylist.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
//单击执行别的操作
}
});
}
public ArrayList<HashMap<String, Object>> ScanPhoto() {
//生成动态数组
ArrayList<HashMap<String, Object>> mylist = new ArrayList<HashMap<String, Object>>();
//查询媒体数据库
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Images.Media.DEFAULT_SORT_ORDER);
//遍历媒体数据库
if(cursor.moveToFirst()){
while (!cursor.isAfterLast()) {
//图片名称
String tilte ="名称:"+ cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.TITLE));
//图片路径
String Url= cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
String url ="路径:"+ Url;
//大小 :MediaStore.Audio.Media.SIZE
Long Size = (Long)cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE))/1024;
String size= "大小:"+Size+"KB";
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("PhotoTitle", tilte);
map.put("PhotoUrl", url);
map.put("PhotoSize",size);
mylist.add(map);
cursor.moveToNext();
}
}
return mylist;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main
<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="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:text="图库"
android:textSize="28sp" />
<View
android:id="@+id/view1"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:layout_marginTop="35dip"
android:background="@drawable/devide_line" />
<ListView
android:id="@+id/mylist"
android:layout_alignTop="@+id/view1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" >
</ListView>
</RelativeLayout>
item
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="TextView" />
<TextView
android:id="@+id/Url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/Title"
android:text="TextView" />
<TextView
android:id="@+id/Size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/Url"
android:text="TextView" />
</RelativeLayout>
相关文章推荐
- Android 把bitmap转换成String,计算图片的缩放值,根据路径获得突破并压缩返回bitmap用于显示,获取保存 隐患检查的图片文件夹名称,获取保存图片的目录,根据路径删除图片,添加到图库
- android开发从SD卡上获得路径怎么将图片变成Bitmap
- android通过ContentPrivider扫描sd卡所有图片,并显示
- android扫描sd卡下的所有.txt 并获取路径
- Android 视频开发中如何通过url或者本地视频文件路径获得视频的第一帧图片
- shell编程: 获得目录下(包括子目录)所有文件名,路径和文件大小
- Android之根据Uri获得图片或视频文件路径(解决4.4以上版本得不到路径的情况)
- android 获取SD卡的图片及其路径
- android 根据SD卡中图片路径读取并显示SD中的图片——源代码
- Android系统手机图片存放SD卡地址路径
- AndroidTXT阅读器的实现(—)扫描sd卡或选择文件路径添加文件到listview及listview的多选删除
- android 扫描手机上的图片 获取手机图片路径
- android 获得指定路径下可用存储大小
- 获得Android系统外置SD卡的路径
- android开发如何获得不同机型的SD卡路径
- android 获取SD卡的图片及其路径
- Android sd卡路径获取(解决Android6.0 sd卡名称混乱问题)
- Android Zxing框架扫描解决扫描框大小,图片压缩问题
- python3.6深度学习图片集处理项目源代码(为cnn等准备图片集合,批量生成、删除、改名、旋转、调整图片大小,包括删除指定路径下所有文件)
- Android获取SD卡上所有图片、视频缩略图和音乐专辑封面