我使用下面的代碼來獲取圖像路徑。並且保存到字符串數組路徑中。
String[] projection = {MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA, MediaStore.Images.ImageColumns.DATA};
Cursor cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.Media._ID);
int count = cursor.getCount();
int image_column_index = cursor.getColumnIndex(MediaStore.Images.Media._ID);
int image_path_index = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
int i;
for(i = 0; i < count; i++) {
cursor.moveToPosition(i);
int id = cursor.getInt(image_column_index);
pat[i[ = cursor.getString(image_path_index);
}
然後我用下面的代碼來獲取縮略圖。
int i;
for(i = 0; i < count; i++) {
String[] projection = {MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA};
Cursor cursor = act.managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, MediaStore.Images.Media.DATA + "=?", new String[] {path[i]}, MediaStore.Images.Media._ID); }
但是當文件數量很大時(差不多1000個文件),cursor就顯示null。我確信它不是由路徑名稱導致的錯誤,那麼是什麼原因導致的?
添加 cursor.close();
int i;
for(i = 0; i < count; i++) {
String[] projection = {MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA};
Cursor cursor = act.managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, MediaStore.Images.Media.DATA + "=?", new String[] {path[i]}, MediaStore.Images.Media._ID);
cursor.close();
}