編輯:關於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(); } }
android shareSDK實現第三方分享,androidsharesdk一. http://www.mob.com/ 在mob官網注冊賬號,
Fragment一般是宿主Activity UI的一部分或一種行為,作為Act
安卓高手之路之java層Binder,安卓binder很多人一提到Binder就說代理模式,人雲亦雲的多,能理解精髓的少。 本篇文章就從設計角度分析一下java
Android Activity生命周期與啟動模式,androidactivityActivity的完整生命周期如下圖: Activity的加載模式有四種: stand