編輯:關於Android編程
方法:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ContentResolver cr = getContentResolver(); Uri uri = Media.EXTERNAL_CONTENT_URI; Cursor cursor = cr.query(uri, null, null, null, null); for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){ String path,display; long date; int size,width,height; path = cursor.getString(cursor.getColumnIndex(Media.DATA)); display = cursor.getString(cursor.getColumnIndex(Media.DISPLAY_NAME)); date = cursor.getLong(cursor.getColumnIndex(Media.DATE_TAKEN)); size = cursor.getInt(cursor.getColumnIndex(Media.SIZE)); width = cursor.getInt(cursor.getColumnIndex("width")); height = cursor.getInt(cursor.getColumnIndex("height")); System.out.println("--------------------------"); System.out.println("path = " + path); System.out.println("display = " + display); System.out.println("date = " + new Date(date)); System.out.println("size = " + size); System.out.println("width = " + width); System.out.println("height = " + height); } } }
Image:
public class Image { private String path; private String display; private long date; private int size; private int width; private int height; private long id; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public String getDisplay() { return display; } public void setDisplay(String display) { this.display = display; } public long getDate() { return date; } public void setDate(long date) { this.date = date; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } }
public class ImageAdapter extends BaseAdapter { private Listimages; public ImageAdapter(List images) { setImages(images); } private void setImages(List images) { if (images == null) { images = new ArrayList(); } this.images = images; } @Override public int getCount() { // TODO Auto-generated method stub return images.size(); } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if(convertView == null){ holder = new ViewHolder(); convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.lv_item, null); holder.image = (ImageView)convertView.findViewById(R.id.iv_image); holder.path = (TextView)convertView.findViewById(R.id.tv_item_path); holder.display = (TextView)convertView.findViewById(R.id.tv_item_name); holder.px = (TextView)convertView.findViewById(R.id.tv_item_px); holder.size = (TextView)convertView.findViewById(R.id.tv_item_size); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } Image img = images.get(position); //容易導致OOM問題,Bitmap對象太大 //Bitmap bm = BitmapFactory.decodeFile(img.getPath()); Bitmap bm; // 方法一:自定義方法獲取Bitmap // bm = decodeFile(img); // 方法二:使用Thumbnails bm = Thumbnails.getThumbnail(parent.getContext().getContentResolver(), img.getId(), Thumbnails.MICRO_KIND, null); holder.display.setText(img.getDisplay()); holder.size.setText(img.getSize() / 1024+ "KB"); holder.path.setText(img.getPath()); holder.px.setText(img.getWidth() + " x " + img.getHeight() + "px"); holder.image.setImageBitmap(bm); return convertView; } static final int MAX_WIDTH = 80; static final int MAX_HEIGHT = 80; private Bitmap decodeFile(Image img){ Bitmap bm = null; Options opts = new Options(); int rate; // 未知圖片的寬與高時的做法: opts.inJustDecodeBounds = true; bm = BitmapFactory.decodeFile(img.getPath(),opts); int width = opts.outWidth; int height = opts.outHeight; if(width >height){ rate = height / MAX_HEIGHT - 1; }else{ rate = width / MAX_WIDTH - 1; } opts.inJustDecodeBounds = false; // 當已知圖片的寬與高時,可以使用以下的方法 // if(img.getWidth() > img.getHeight()){ // rate = img.getWidth() / MAX_WIDTH - 1; // }else{ // rate = img.getHeight() / MAX_HEIGHT - 1; // } opts.inSampleSize = rate; bm = BitmapFactory.decodeFile(img.getPath(),opts); return bm; } private class ViewHolder { ImageView image; TextView path, display, px, size; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } }
public class MainActivity extends Activity { private Listimages = new ArrayList(); private ListView listView; private ImageAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); loadImages(); listView = (ListView) findViewById(R.id.lv_images); adapter = new ImageAdapter(images); listView.setAdapter(adapter); } private void loadImages(){ ContentResolver cr = getContentResolver(); Uri uri = Media.EXTERNAL_CONTENT_URI; Cursor cursor = cr.query(uri, null, null, null, null); // Image image = new Image(); for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){ Image image = new Image(); image.setId(cursor.getLong(cursor.getColumnIndex(Media._ID))); image.setPath(cursor.getString(cursor.getColumnIndex(Media.DATA))); image.setDisplay(cursor.getString(cursor.getColumnIndex(Media.DISPLAY_NAME))); image.setDate(cursor.getLong(cursor.getColumnIndex(Media.DATE_TAKEN))); image.setSize(cursor.getInt(cursor.getColumnIndex(Media.SIZE))); image.setWidth(cursor.getInt(cursor.getColumnIndex("width"))); image.setHeight(cursor.getInt(cursor.getColumnIndex("height"))); images.add(image); } cursor.close(); } }
package com.example.lianxi; import java.util.ArrayList; import java.util.Date; import java.util.List; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore.Audio.Media; import android.widget.ListView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 媒體庫中的音頻所具有的部分字段: // _id _data titile_size duration album_artist album album_key // ContentResolver Uri ContentResolver cr = getContentResolver(); Uri uri = Media.EXTERNAL_CONTENT_URI; Cursor cursor = cr.query(uri, null, null, null, null); Listmusics = new ArrayList (); Music music; for (cursor.moveToFirst(); cursor.isAfterLast(); cursor.moveToNext()) { music = new Music(); music.setId(cursor.getLong(cursor.getColumnIndex(Media._ID))); music.setData(cursor.getString(cursor.getColumnIndex(Media.DATA))); music.setTitle(cursor.getString(cursor.getColumnIndex(Media.TITLE))); music.setArtist(cursor.getString(cursor .getColumnIndex(Media.ARTIST))); music.setAlbum(cursor.getString(cursor.getColumnIndex(Media.ALBUM))); music.setAlbumkey(cursor.getString(cursor .getColumnIndex(Media.ALBUM_KEY))); music.setSize(cursor.getInt(cursor.getColumnIndex(Media.SIZE))); music.setDuration(cursor.getInt(cursor .getColumnIndex(Media.DURATION))); musics.add(music); System.out.println(music.toString()); } cursor.close(); } }
01-01 13:41:27.514: I/System.out(11445): Music [id=15, data=/storage/emulated/0/Music/Groove Coverage - She.mp3, title=Groove Coverage - She, size=3664535, duration=229033, artist=
01-01 13:41:27.518: I/System.out(11445): Music [id=16, data=/storage/emulated/0/Music/number2.mp3, title=number2, size=3664535, duration=229033, artist=
01-01 13:41:27.530: I/System.out(11445): Music [id=17, data=/storage/emulated/0/Music/number3.mp3, title=Can't Get You Out Of My Head, size=3681031, duration=230016, artist=Kylie Minogue, album=Can't Get You Out of My Head [UK CD]]
01-01 13:41:27.530: I/System.out(11445): Music [id=18, data=/storage/emulated/0/Music/number1.mp3, title=number1, size=4727537, duration=295471, artist=
使用媒體庫做出的獲取音樂信息
與獲取圖片類似
MusicAdapter:
public class MusicAdapter extends BaseAdapter{ private Listmusics; public MusicAdapter(List musics){ setMusics(musics); } public void setMusics(List musics){ if(musics == null){ musics = new ArrayList (); } this.musics = musics; } @Override public int getCount() { // TODO Auto-generated method stub return musics.size(); } private class ViewHolder{ ImageView image; TextView title,duration,artist,album; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder; if(convertView == null){ convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.lv_item, null); holder = new ViewHolder(); holder.image = (ImageView) convertView.findViewById(R.id.iv_image); holder.title = (TextView) convertView.findViewById(R.id.tv_item_title); holder.duration = (TextView) convertView.findViewById(R.id.tv_item_time); holder.artist = (TextView) convertView.findViewById(R.id.tv_item_artist); holder.album = (TextView) convertView.findViewById(R.id.tv_item_name); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } Music music = musics.get(position); holder.title.setText(music.getTitle()); holder.duration.setText(formatDuration(music.getDuration())); holder.artist.setText(music.getArtist()); holder.album.setText(music.getAlbum()); if(music.getImagePath() == null){ }else{ holder.image.setImageBitmap(BitmapFactory.decodeFile(music.getImagePath())); } return convertView; } private String formatDuration(int duration){ return new SimpleDateFormat("mm:ss",Locale.CHINA).format(new Date(duration)); } }
Music:
public class Music { private long id; private String data; private String title; private int size; private int duration; private String artist; private String album; private String albumkey; private String imagePath; public String getImagePath() { return imagePath; } public void setImagePath(String imagePath) { this.imagePath = imagePath; } public String getArtist() { return artist; } public void setArtist(String artist) { this.artist = artist; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getData() { return data; } public void setData(String data) { this.data = data; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public int getDuration() { return duration; } public void setDuration(int duration) { this.duration = duration; } public String getAlbum() { return album; } public void setAlbum(String album) { this.album = album; } public String getAlbumkey() { return albumkey; } public void setAlbumkey(String albumkey) { this.albumkey = albumkey; } @Override public String toString() { return "Music [id=" + id + ", data=" + data + ", title=" + title + ", size=" + size + ", duration=" + duration + ", artist=" + artist + ", album=" + album + "]"; } }
public class MainActivity extends Activity { private Listmusics; private ListView listView; private MusicAdapter adapter; private Uri uri; private Cursor cursor; private ContentResolver cr; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); loadMusic(); listView = (ListView) findViewById(R.id.lv_images); adapter = new MusicAdapter(musics); listView.setAdapter(adapter); } private void loadMusic(){ cr = getContentResolver(); uri = Media.EXTERNAL_CONTENT_URI; cursor = cr.query(uri, null, null, null, null); musics = new ArrayList (); for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){ Music music = new Music(); music.setId(cursor.getLong(cursor.getColumnIndex(Media._ID))); music.setData(cursor.getString(cursor.getColumnIndex(Media.DATA))); music.setTitle(cursor.getString(cursor.getColumnIndex(Media.TITLE))); music.setArtist(cursor.getString(cursor.getColumnIndex(Media.ARTIST))); music.setAlbum(cursor.getString(cursor.getColumnIndex(Media.ALBUM))); music.setAlbumkey(cursor.getString(cursor.getColumnIndex(Media.ALBUM_KEY))); music.setSize(cursor.getInt(cursor.getColumnIndex(Media.SIZE))); music.setDuration(cursor.getInt(cursor.getColumnIndex(Media.DURATION))); System.out.println(music.toString()); Uri imageUri = Albums.EXTERNAL_CONTENT_URI; String[] projection = new String[] {Albums.ALBUM_ART}; String selection = Albums.ALBUM_KEY + "=?"; String[] selectionArgs = new String[] {music.getAlbumkey()}; Cursor imageCursor = cr.query(imageUri, projection, selection, selectionArgs, null); if(imageCursor.moveToFirst()){ music.setImagePath(imageCursor.getString(0)); } musics.add(music); } cursor.close(); } }
本文來自http://blog.csdn.net/hellogv/ ,引用必須注明出處! 上次講了Android手機與BLE終端之間的通信,而最常見的BLE終端應該是蘋果
問題背景: 參考鏈接 做了一個圖片浏覽,用ContentResolver掃描圖庫照片,且嚴格按照時間拍攝順序排好序顯示在listview裡。如下圖所示:遇到的問題是在4.
1、美國GPS使用的是WGS84坐標系統,以經緯度的形式來表示地球平面上的某一個位置。但在我國,出於國家安全考慮,國內所有導航電子地圖必須使用國家測繪局制定的加密坐標系統
一、問題描述LBS位置服務是android應用中重要的功能,應用越來越廣泛,下面我們逐步學習和實現lbs相關的應用如定位、地圖、導航等,首先我們看如何基於百度地圖實現定位