您的位置:首页 > 移动开发 > Android开发

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 图片 listview
相关文章推荐