如果特定图片路径是/mnt/sdcard/newphoto/,怎么实现加载这个指定文件夹下的全部图片
这个是代码,路径应该放在什么地方:
public class LoadImagesFromSDCardActivity extends Activity implements
OnItemClickListener {
private ListView sdcardImages;
private ImageAdapter imageAdapter; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.new_stir); setupViews();
setProgressBarIndeterminateVisibility(true);
loadImages();
} /**
* Free up bitmap related resources.
*/
protected void onDestroy() {
super.onDestroy();
final ListView grid = sdcardImages;
final int count = grid.getChildCount();
ImageView v = null;
for (int i = 0; i < count; i++) {
v = (ImageView) grid.getChildAt(i);
((BitmapDrawable) v.getDrawable()).setCallback(null);
}
} /**
* Setup the grid view.
*/
private void setupViews() {
sdcardImages = (ListView) findViewById(R.id.new_listview);
sdcardImages.setOnItemClickListener(LoadImagesFromSDCardActivity.this);
imageAdapter = new ImageAdapter(getApplicationContext());
sdcardImages.setAdapter(imageAdapter);
} /**
* Load images.
*/
private void loadImages() {
final Object data = getLastNonConfigurationInstance();
if (data == null) {
new LoadImagesFromSDCard().execute();
} else {
final LoadedImage[] photos = (LoadedImage[]) data;
if (photos.length == 0) {
new LoadImagesFromSDCard().execute();
}
for (LoadedImage photo : photos) {
addImage(photo);
}
}
} /**
* Add image(s) to the grid view adapter.
*
* @param value
* Array of LoadedImages references
*/
private void addImage(LoadedImage... value) {
for (LoadedImage image : value) {
imageAdapter.addPhoto(image);
imageAdapter.notifyDataSetChanged();
}
} /**
* Save bitmap images into a list and return that list.
*
* @see android.app.Activity#onRetainNonConfigurationInstance()
*/
@Override
public Object onRetainNonConfigurationInstance() {
final ListView grid = sdcardImages;
final int count = grid.getChildCount();
final LoadedImage[] list = new LoadedImage[count]; for (int i = 0; i < count; i++) {
final ImageView v = (ImageView) grid.getChildAt(i);
list[i] = new LoadedImage(
((BitmapDrawable) v.getDrawable()).getBitmap());
} return list;
} /**
* Async task for loading the images from the SD card.
*
* @author Mihai Fonoage
*
*/
class LoadImagesFromSDCard extends AsyncTask<Object, LoadedImage, Object> { /**
* Load images from SD Card in the background, and display each image on
* the screen.
*
* @see android.os.AsyncTask#doInBackground(Params[])
*/
@Override
protected Object doInBackground(Object... params) {
// setProgressBarIndeterminateVisibility(true);
Bitmap bitmap = null;
Bitmap newBitmap = null;
Uri uri = null; // Set up an array of the Thumbnail Image ID column we want
String[] projection = { MediaStore.Images.Thumbnails._ID };
// Create the cursor pointing to the SDCard
Cursor cursor = managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null, null);
int columnIndex = cursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
int size = cursor.getCount();
// If size is 0, there are no images on the SD Card.
if (size == 0) {
// No Images available, post some message to the user
}
int imageID = 0;
for (int i = 0; i < size; i++) {
cursor.moveToPosition(i);
imageID = cursor.getInt(columnIndex);
uri = Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""
+ imageID);
try {
bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(uri));
if (bitmap != null) {
newBitmap = Bitmap.createScaledBitmap(bitmap, 70, 70,
true);
bitmap.recycle();
if (newBitmap != null) {
publishProgress(new LoadedImage(newBitmap));
}
}
} catch (IOException e) {
// Error fetching image, try to recover
}
}
cursor.close();
return null;
} /**
* Add a new LoadedImage in the images grid.
*
* @param value
* The image.
*/
@Override
public void onProgressUpdate(LoadedImage... value) {
addImage(value);
} /**
* Set the visibility of the progress bar to false.
*
* @see android.os.AsyncTask#onPostExecute(java.lang.Object)
*/
@Override
protected void onPostExecute(Object result) {
setProgressBarIndeterminateVisibility(false);
}
} /**
* Adapter for our image files.
*
* @author Mihai Fonoage
*
*/
class ImageAdapter extends BaseAdapter { private Context mContext;
private ArrayList<LoadedImage> photos = new ArrayList<LoadedImage>(); public ImageAdapter(Context context) {
mContext = context;
} public void addPhoto(LoadedImage photo) {
photos.add(photo);
} public int getCount() {
return photos.size();
} public Object getItem(int position) {
return photos.get(position);
} public long getItemId(int position) {
return position;
} public View getView(int position, View convertView, ViewGroup parent) {
final ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
} else {
imageView = (ImageView) convertView;
}
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setPadding(8, 8, 8, 8);
imageView.setImageBitmap(photos.get(position).getBitmap());
return imageView;
}
} /**
* A LoadedImage contains the Bitmap loaded for the image.
*/
private static class LoadedImage {
Bitmap mBitmap; LoadedImage(Bitmap bitmap) {
mBitmap = bitmap;
} public Bitmap getBitmap() {
return mBitmap;
}
} /**
* When an image is clicked, load that image as a puzzle.
*/
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
int columnIndex = 0;
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection,
null, null, null);
if (cursor != null) {
columnIndex = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToPosition(position);
String imagePath = cursor.getString(columnIndex); FileInputStream is = null;
BufferedInputStream bis = null;
try {
is = new FileInputStream(new File(imagePath));
bis = new BufferedInputStream(is);
Bitmap bitmap = BitmapFactory.decodeStream(bis);
Bitmap useThisBitmap = Bitmap.createScaledBitmap(bitmap,
parent.getWidth(), parent.getHeight(), true);
bitmap.recycle();
// Display bitmap (useThisBitmap)
} catch (Exception e) {
// Try to recover
} finally {
try {
if (bis != null) {
bis.close();
}
if (is != null) {
is.close();
}
cursor.close();
projection = null;
} catch (Exception e) {
}
}
}
}}
androidsdcardAsyncTask
这个是代码,路径应该放在什么地方:
public class LoadImagesFromSDCardActivity extends Activity implements
OnItemClickListener {
private ListView sdcardImages;
private ImageAdapter imageAdapter; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.new_stir); setupViews();
setProgressBarIndeterminateVisibility(true);
loadImages();
} /**
* Free up bitmap related resources.
*/
protected void onDestroy() {
super.onDestroy();
final ListView grid = sdcardImages;
final int count = grid.getChildCount();
ImageView v = null;
for (int i = 0; i < count; i++) {
v = (ImageView) grid.getChildAt(i);
((BitmapDrawable) v.getDrawable()).setCallback(null);
}
} /**
* Setup the grid view.
*/
private void setupViews() {
sdcardImages = (ListView) findViewById(R.id.new_listview);
sdcardImages.setOnItemClickListener(LoadImagesFromSDCardActivity.this);
imageAdapter = new ImageAdapter(getApplicationContext());
sdcardImages.setAdapter(imageAdapter);
} /**
* Load images.
*/
private void loadImages() {
final Object data = getLastNonConfigurationInstance();
if (data == null) {
new LoadImagesFromSDCard().execute();
} else {
final LoadedImage[] photos = (LoadedImage[]) data;
if (photos.length == 0) {
new LoadImagesFromSDCard().execute();
}
for (LoadedImage photo : photos) {
addImage(photo);
}
}
} /**
* Add image(s) to the grid view adapter.
*
* @param value
* Array of LoadedImages references
*/
private void addImage(LoadedImage... value) {
for (LoadedImage image : value) {
imageAdapter.addPhoto(image);
imageAdapter.notifyDataSetChanged();
}
} /**
* Save bitmap images into a list and return that list.
*
* @see android.app.Activity#onRetainNonConfigurationInstance()
*/
@Override
public Object onRetainNonConfigurationInstance() {
final ListView grid = sdcardImages;
final int count = grid.getChildCount();
final LoadedImage[] list = new LoadedImage[count]; for (int i = 0; i < count; i++) {
final ImageView v = (ImageView) grid.getChildAt(i);
list[i] = new LoadedImage(
((BitmapDrawable) v.getDrawable()).getBitmap());
} return list;
} /**
* Async task for loading the images from the SD card.
*
* @author Mihai Fonoage
*
*/
class LoadImagesFromSDCard extends AsyncTask<Object, LoadedImage, Object> { /**
* Load images from SD Card in the background, and display each image on
* the screen.
*
* @see android.os.AsyncTask#doInBackground(Params[])
*/
@Override
protected Object doInBackground(Object... params) {
// setProgressBarIndeterminateVisibility(true);
Bitmap bitmap = null;
Bitmap newBitmap = null;
Uri uri = null; // Set up an array of the Thumbnail Image ID column we want
String[] projection = { MediaStore.Images.Thumbnails._ID };
// Create the cursor pointing to the SDCard
Cursor cursor = managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null, null);
int columnIndex = cursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
int size = cursor.getCount();
// If size is 0, there are no images on the SD Card.
if (size == 0) {
// No Images available, post some message to the user
}
int imageID = 0;
for (int i = 0; i < size; i++) {
cursor.moveToPosition(i);
imageID = cursor.getInt(columnIndex);
uri = Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""
+ imageID);
try {
bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(uri));
if (bitmap != null) {
newBitmap = Bitmap.createScaledBitmap(bitmap, 70, 70,
true);
bitmap.recycle();
if (newBitmap != null) {
publishProgress(new LoadedImage(newBitmap));
}
}
} catch (IOException e) {
// Error fetching image, try to recover
}
}
cursor.close();
return null;
} /**
* Add a new LoadedImage in the images grid.
*
* @param value
* The image.
*/
@Override
public void onProgressUpdate(LoadedImage... value) {
addImage(value);
} /**
* Set the visibility of the progress bar to false.
*
* @see android.os.AsyncTask#onPostExecute(java.lang.Object)
*/
@Override
protected void onPostExecute(Object result) {
setProgressBarIndeterminateVisibility(false);
}
} /**
* Adapter for our image files.
*
* @author Mihai Fonoage
*
*/
class ImageAdapter extends BaseAdapter { private Context mContext;
private ArrayList<LoadedImage> photos = new ArrayList<LoadedImage>(); public ImageAdapter(Context context) {
mContext = context;
} public void addPhoto(LoadedImage photo) {
photos.add(photo);
} public int getCount() {
return photos.size();
} public Object getItem(int position) {
return photos.get(position);
} public long getItemId(int position) {
return position;
} public View getView(int position, View convertView, ViewGroup parent) {
final ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
} else {
imageView = (ImageView) convertView;
}
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setPadding(8, 8, 8, 8);
imageView.setImageBitmap(photos.get(position).getBitmap());
return imageView;
}
} /**
* A LoadedImage contains the Bitmap loaded for the image.
*/
private static class LoadedImage {
Bitmap mBitmap; LoadedImage(Bitmap bitmap) {
mBitmap = bitmap;
} public Bitmap getBitmap() {
return mBitmap;
}
} /**
* When an image is clicked, load that image as a puzzle.
*/
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
int columnIndex = 0;
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection,
null, null, null);
if (cursor != null) {
columnIndex = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToPosition(position);
String imagePath = cursor.getString(columnIndex); FileInputStream is = null;
BufferedInputStream bis = null;
try {
is = new FileInputStream(new File(imagePath));
bis = new BufferedInputStream(is);
Bitmap bitmap = BitmapFactory.decodeStream(bis);
Bitmap useThisBitmap = Bitmap.createScaledBitmap(bitmap,
parent.getWidth(), parent.getHeight(), true);
bitmap.recycle();
// Display bitmap (useThisBitmap)
} catch (Exception e) {
// Try to recover
} finally {
try {
if (bis != null) {
bis.close();
}
if (is != null) {
is.close();
}
cursor.close();
projection = null;
} catch (Exception e) {
}
}
}
}}
androidsdcardAsyncTask
解决方案 »
- 安卓、苹果客户端开发分别需要哪些工具,是否需要搭建开发平台?
- 安卓4.0版本 代码设置横屏 activey会从新创建?
- 怎么调用前摄像头拍照?
- intent 传递错误 但是没有报错信息 点击按钮退出
- 分辨率问题
- android 4.1中Fragment在进行转屏的时候onCreate和onAttach()调用了两次,而onStart()只调用了一次
- 适配联想k910 Cm11编译时出现的问题求解
- 安卓中如何实现点击按钮出现图标,点击空白处图标就消失
- 安卓studio 打开xml文件没有preview
- 如何修改安卓手机蓝牙的识别类型
- 用一个变量来标示Dialog的最后点击了什么的功能,我现在用的是一个类变量,感觉不够科学,那Dialog有没有一个可以做标示的?比如View有个setTag 谢
- Android函数前面加$符号是什么意思?
这个是路径
假如我要获取这个文件夹下的图片:/mnt/sdcard/newphoto/
用Uri怎么表示
File sdDir = null;
boolean sdCardExist = Environment.getExternalStorageState()
.equals(Android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在
if (sdCardExist)
{
sdDir = Environment.getExternalStorageDirectory();//获取跟目录
}
return sdDir.toString();
}然后:在后面加上斜杠,在加上文件名
String fileName = getSDPath() +"/" + name;//以name存在目录中
这是你要的uri
string myImageUrl = "content://media/external/images/media/******";
Uri uri = Uri.parse(myImageUrl);
首先external是我保存sdcard配置的数据库名,在/data/data/com.android.providers.media下面,打开数据库可以看到一张表,对应那张表写后面的内容就知道,,,
/mnt/sdcard/newphoto/
这个路径怎么代入那个uri
因为上面的例子是扫描全部的图片,如果我改一下路径Uri就能扫描一部分图片了
第一种用媒体数据库实现:Cursor cursor = context.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, null, "bucket_display_name in ('newphoto')", null, null);循环cursor,将得到newphoto这个文件夹下所有图片的信息(当然包括路径,图片名等等)。
第二种就是找到对应存储卡中newphoto的文件夹,循环遍历:
File file = new File(Environment.getExternalStorageDirectory()+"/newphoto");
File[] files = file.listFiles();得到newphoto的文件夹所有文件的信息。路径中最好不要用/mnt/sdcard/这样的方式获取,不是每个手机存储卡都以这样的方式开头。最好用系统自带的API获取。
private String path = "content://main.images/";
private static final String AUTHORTY = "mnt/sdcard/pingziyi/";
class LoadImagesFromSDCard extends AsyncTask<Object, LoadedImage, Object> {
@Override
protected Object doInBackground(Object... params) {
// setProgressBarIndeterminateVisibility(true);
Bitmap bitmap = null;
Bitmap newBitmap = null;
Uri uri = null;
Uri mUri = Uri.parse(path+AUTHORTY);
// Set up an array of the Thumbnail Image ID column we want
String[] projection = { MediaStore.Images.Thumbnails._ID };
Cursor cursor = managedQuery(mUri, projection, null,
null, null);
int columnIndex = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
int size = cursor.getCount();
// If size is 0, there are no images on the SD Card.
if (size == 0) {
// No Images available, post some message to the user
}
int imageID = 0;
for (int i = 0; i < size; i++) {
cursor.moveToPosition(i);
imageID = cursor.getInt(columnIndex);
uri = Uri.withAppendedPath(mUri, "" + imageID); try {
bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(mUri));
if (bitmap != null) {
newBitmap = Bitmap.createScaledBitmap(bitmap, 70, 70,
true);
bitmap.recycle();
if (newBitmap != null) {
publishProgress(new LoadedImage(newBitmap));
}
}
} catch (IOException e) {
// Error fetching image, try to recover
}
}
cursor.close();
return null;
}
@Override
public void onProgressUpdate(LoadedImage... value) {
addImage(value);
} @Override
protected void onPostExecute(Object result) {
setProgressBarIndeterminateVisibility(false);
}
}
出错的代码位置:空指针
int columnIndex = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
用了这个还是出错 了,地址是:/mnt/sdcard/pingziyi/
这个是代码:
class LoadImagesFromSDCard extends AsyncTask<Object, LoadedImage, Object> {
@Override
protected Object doInBackground(Object... params) {
// setProgressBarIndeterminateVisibility(true);
Bitmap bitmap = null;
Bitmap newBitmap = null;
Uri uri = null;
Cursor cursor = context.getContentResolver().query(
Media.EXTERNAL_CONTENT_URI, null,
"bucket_display_name in ('pingziyi')",
null, null);
int columnIndex = cursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
int size = cursor.getCount();
if (size == 0) {
// No Images available, post some message to the user
}
int imageID = 0;
for (int i = 0; i < size; i++) {
cursor.moveToPosition(i);
imageID = cursor.getInt(columnIndex);
uri = Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""
+ imageID); try {
bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(uri));
if (bitmap != null) {
newBitmap = Bitmap.createScaledBitmap(bitmap, 70, 70,
true);
bitmap.recycle();
if (newBitmap != null) {
publishProgress(new LoadedImage(newBitmap));
}
}
} catch (IOException e) {
// Error fetching image, try to recover
}
}
cursor.close();
return null;
}
@Override
public void onProgressUpdate(LoadedImage... value) {
addImage(value);
} @Override
protected void onPostExecute(Object result) {
setProgressBarIndeterminateVisibility(false);
}
}
content://media/external/images/media/就是整个文件夹,content://media/external/images/media/3就是你的第一个图片
上面那个方法,还没想到用什么方法去修改。我是用SQLite expert!
上面那个方法,还没想到用什么方法去修改。我是用SQLite expert!看了下,那个方法, 是要获取文件夹目录下的全部文件,然后迭代,最烦就是迭代了,得出的数据只能在循环里,
有没有方法修改这个代码 去获取某文件夹下的图片,我用了这个代码(放到下面,这里写不下了),但是滑动行慢,还会出现OOM:异步加载就不会出现,
private ListView lv;
private String path = "/mnt/sdcard/pingziyi/";
private HashMap<String, Object> map = new HashMap<String, Object>();
private List<HashMap<String, Object>> Map = new ArrayList<HashMap<String, Object>>();
private MyAdapter adapter;
private final String IMAGE = "image";
private final String NAME = "name";
private final String SIZE = "size";
private final String DATE = "date"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.listview);
getDate(path);
adapter = new MyAdapter(MainActivity.this);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
} private List<HashMap<String, Object>> getDate(String path) {
Map = new ArrayList<HashMap<String, Object>>();
File file = new File(path);
String[] filepath = file.list();
if (checkImage() == true) {
for (String str : filepath) {
map = new HashMap<String, Object>();
String filename = str;
System.out.println("filename-:" + filename);
System.out.println("filename--:" + path + filename);
Bitmap bm = ImageCommon.getFitSizePicture(path + filename); map.put(IMAGE, bm);
map.put(NAME, filename);
map.put(SIZE, file.length());
map.put(DATE, file.lastModified());
Map.add(map);
// handler.obtainMessage(1, null).sendToTarget();
}
}
return Map; } private boolean checkImage() {
File allfile = new File(path); File[] files = allfile.listFiles();
for (int i = 0; i < files.length; i++) {
System.out.println("file[iname]" + files[i].getName());
if (files[i].getName().endsWith("jpg") && files[i].isFile()
|| files[i].getName().endsWith("png") && files[i].isFile()) {
System.out.println("file[iname]" + files[i].getName());
return true;
} return true;
}
return false;
} private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
adapter = new MyAdapter(MainActivity.this);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
break; default:
break;
}
}
}; @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} private class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater; public MyAdapter(Context context) {
this.mInflater = LayoutInflater.from(context); } @Override
public int getCount() {
return getDate(path).size();
} @Override
public Object getItem(int position) {
return position;
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.share_content, null);
holder = new ViewHolder();
holder.img = (ImageView) convertView.findViewById(R.id.photo);
holder.name = (TextView) convertView
.findViewById(R.id.photo_name);
holder.size = (TextView) convertView
.findViewById(R.id.photo_size);
holder.date = (TextView) convertView
.findViewById(R.id.photo_time); } else {
holder = (ViewHolder) convertView.getTag(); }
holder.img.setImageBitmap((Bitmap) getDate(path).get(position).get(
IMAGE));
holder.name.setText(getDate(path).get(position).get(NAME)
.toString());
holder.size.setText(getDate(path).get(position).get(SIZE)
.toString());
holder.date.setText(getDate(path).get(position).get(DATE)
.toString());
return convertView;
} public final class ViewHolder {
public ImageView img;
public TextView name;
public TextView size;
public TextView date;
}
}
}
第一种出错,应该是你的方法没用对。按道理应使用第一个方法,毕竟那些数据都是系统处理好的。不管使用哪一种,OOM都可能会出现,那么你就要针对OOM做文章了。
第一种出错,应该是你的方法没用对。按道理应使用第一个方法,毕竟那些数据都是系统处理好的。不管使用哪一种,OOM都可能会出现,那么你就要针对OOM做文章了。
按你的方法只加了这句:
Cursor cursor = MainActivity.this.getContentResolver().query(
Media.EXTERNAL_CONTENT_URI, null,
"bucket_display_name in ('pingziyi')", null, null);class LoadImagesFromSDCard extends AsyncTask<Object, LoadedImage, Object> { @Override
protected Object doInBackground(Object... params) {
Bitmap bitmap = null;
Bitmap newBitmap = null;
Uri uri = null; // String[] projection = { MediaStore.Images.Thumbnails._ID };
Cursor cursor = MainActivity.this.getContentResolver().query(
Media.EXTERNAL_CONTENT_URI, null,
"bucket_display_name in ('pingziyi')", null, null);
int columnIndex = cursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
int size = cursor.getCount();
if (size == 0) {
}
int imageID = 0;
for (int i = 0; i < size; i++) {
cursor.moveToPosition(i);
imageID = cursor.getInt(columnIndex);
uri = Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""
+ imageID);
try {
bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(uri));
if (bitmap != null) {
newBitmap = Bitmap.createScaledBitmap(bitmap, 70, 70,
true);
bitmap.recycle();
if (newBitmap != null) {
publishProgress(new LoadedImage(newBitmap));
}
}
} catch (IOException e) {
}
}
cursor.close();
return null;
} @Override
public void onProgressUpdate(LoadedImage... value) {
addImage(value);
} @Override
protected void onPostExecute(Object result) {
setProgressBarIndeterminateVisibility(false);
}
}