本示例演示如何在Android中實現獲取本機中的所有圖片,並進行查看效果。
在這個示例中,我使用android-support-v4.jar中的載入器來實現獲取本機中所有圖片,關於這個包在以前的文章中也提到,是一個非常有用的包,關於這個包的詳細信息請大家查看官方文檔:http://developer.android.com/sdk/compatibility-library.html。
先讓我們看下本示例實現的效果圖:
項目結構圖如下所示:
MyDevicePhotoActivity.java文件中代碼如下:
001 |
package com.device.photo; |
003 |
import android.app.Dialog; |
004 |
import android.content.ContentResolver; |
005 |
import android.database.Cursor; |
006 |
import android.graphics.Bitmap; |
007 |
import android.net.Uri; |
008 |
import android.os.Bundle; |
009 |
import android.provider.MediaStore; |
010 |
import android.view.View; |
011 |
import android.view.View.OnClickListener; |
012 |
import android.widget.AdapterView; |
013 |
import android.widget.Button; |
014 |
import android.widget.ImageView; |
015 |
import android.widget.ListView; |
016 |
import android.widget.TextView; |
017 |
import android.widget.AdapterView.OnItemClickListener; |
019 |
import android.support.v4.app.FragmentActivity; |
020 |
import android.support.v4.app.LoaderManager.LoaderCallbacks; |
021 |
import android.support.v4.content.CursorLoader; |
022 |
import android.support.v4.content.Loader; |
023 |
import android.support.v4.widget.SimpleCursorAdapter; |
024 |
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder; |
028 |
* @Description: Android實現獲取本機中所有圖片 |
030 |
* @FileName: MyDevicePhotoActivity.java |
032 |
* @Package com.device.photo |
036 |
* @Date 2012-5-10 下午04:43:55 |
040 |
public class MyDevicePhotoActivity extends FragmentActivity implements LoaderCallbacks<Cursor>{ |
041 |
private Bitmap bitmap = null ; |
042 |
private byte [] mContent = null ; |
044 |
private ListView listView = null ; |
045 |
private SimpleCursorAdapter simpleCursorAdapter = null ; |
047 |
private static final String[] STORE_IMAGES = { |
048 |
MediaStore.Images.Media.DISPLAY_NAME, |
049 |
MediaStore.Images.Media.LATITUDE, |
050 |
MediaStore.Images.Media.LONGITUDE, |
051 |
MediaStore.Images.Media._ID |
054 |
/** Called when the activity is first created. */ |
056 |
public void onCreate(Bundle savedInstanceState) { |
057 |
super .onCreate(savedInstanceState); |
058 |
setContentView(R.layout.main); |
060 |
listView = (ListView)findViewById(android.R.id.list); |
061 |
simpleCursorAdapter = new SimpleCursorAdapter( |
063 |
R.layout.simple_list_item, |
066 |
new int [] { R.id.item_title, R.id.item_value}, |
070 |
simpleCursorAdapter.setViewBinder( new ImageLocationBinder()); |
071 |
listView.setAdapter(simpleCursorAdapter); |
072 |
// 注意此處是getSupportLoaderManager(),而不是getLoaderManager()方法。 |
073 |
getSupportLoaderManager().initLoader( 0 , null , this ); |
076 |
listView.setOnItemClickListener( new ShowItemImageOnClickListener()); |
080 |
public Loader<Cursor> onCreateLoader( int arg0, Bundle arg1) { |
081 |
// TODO Auto-generated method stub |
082 |
// 為了查看信息,需要用到CursorLoader。 |
083 |
CursorLoader cursorLoader = new CursorLoader( |
085 |
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, |
094 |
public void onLoaderReset(Loader<Cursor> arg0) { |
095 |
// TODO Auto-generated method stub |
096 |
simpleCursorAdapter.swapCursor( null ); |
100 |
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) { |
101 |
// TODO Auto-generated method stub |
102 |
// 使用swapCursor()方法,以使舊的游標不被關閉. |
103 |
simpleCursorAdapter.swapCursor(cursor); |
107 |
private class ImageLocationBinder implements ViewBinder{ |
109 |
public boolean setViewValue(View view, Cursor cursor, int arg2) { |
110 |
// TODO Auto-generated method stub |
113 |
double latitude = cursor.getDouble(arg2); |
114 |
double longitude = cursor.getDouble(arg2 + 1 ); |
116 |
if (latitude == 0.0 && longitude == 0.0 ) { |
117 |
((TextView)view).setText( "位置:未知" ); |
119 |
((TextView)view).setText( "位置:" + latitude + ", " + longitude); |
122 |
// 需要注意:在使用ViewBinder綁定數據時,必須返回真;否則,SimpleCursorAdapter將會用自己的方式綁定數據。 |
131 |
private class ShowItemImageOnClickListener implements OnItemClickListener{ |
133 |
public void onItemClick(AdapterView<?> parent, View view, int position, |
135 |
// TODO Auto-generated method stub |
136 |
final Dialog dialog = new Dialog(MyDevicePhotoActivity. this ); |
138 |
dialog.setContentView(R.layout.image_show); |
139 |
dialog.setTitle( "圖片顯示" ); |
141 |
ImageView ivImageShow = (ImageView) dialog.findViewById(R.id.ivImageShow); |
142 |
Button btnClose = (Button) dialog.findViewById(R.id.btnClose); |
144 |
btnClose.setOnClickListener( new OnClickListener() { |
147 |
public void onClick(View v) { |
153 |
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI.buildUpon(). |
154 |
appendPath(Long.toString(id)).build(); |
155 |
FileUtil file = new FileUtil(); |
156 |
ContentResolver resolver = getContentResolver(); |
160 |
mContent = file.readInputStream(resolver.openInputStream(Uri.parse(uri.toString()))); |
161 |
bitmap = file.getBitmapFromBytes(mContent, null ); |
162 |
ivImageShow.setImageBitmap(bitmap); |
163 |
} catch (Exception e) { |
164 |
// TODO: handle exception |
FileUtil.java文件主要是對圖片資源的處理。代碼如下:
01 |
package com.device.photo; |
03 |
import java.io.ByteArrayOutputStream; |
04 |
import java.io.InputStream; |
06 |
import android.graphics.Bitmap; |
07 |
import android.graphics.BitmapFactory; |
13 |
* @FileName: FileUtil.java |
15 |
* @Package com.device.photo |
19 |
* @Date 2012-5-10 下午01:37:49 |
23 |
public class FileUtil { |
25 |
// TODO Auto-generated constructor stub |
34 |
public byte [] readInputStream(InputStream inStream) throws Exception { |
35 |
byte [] buffer = new byte [ 1024 ]; |
37 |
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); |
39 |
while ((len = inStream.read(buffer)) != - 1 ) { |
40 |
outStream.write(buffer, 0 , len); |
43 |
byte [] data = outStream.toByteArray(); |
56 |
public Bitmap getBitmapFromBytes( byte [] bytes, BitmapFactory.Options opts) { |
59 |
return BitmapFactory.decodeByteArray(bytes, 0 , bytes.length,opts); |
62 |
return BitmapFactory.decodeByteArray(bytes, 0 , bytes.length); |
最後,希望轉載的朋友能夠尊重作者的勞動成果,加上轉載地址:http://www.cnblogs.com/hanyonglu/archive/2012/05/10/2494908.html,謝謝。
推薦閱讀:
※大「室」所趨,完美實現保利公園九里2房變3房
※從「黑船開國」到明治維新,日本如何快速實現工業化並晉陞列強行列
※Gamma校正及其實現
※論轉型期政治參與實現的特殊性
※CSS display:table-cell的應用IE6/7實現
TAG:圖片 | Android | 獲取 | 實現 |