我正在尝试获取我的Android设备的所有照片。
我有一个onCreate
功能:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageAdapter adapter= new ImageAdapter(this);
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
Uri rr= MediaStore.Images.Media.INTERNAL_CONTENT_URI;
Cursor imagecursor = getContentResolver().query(
MediaStore.Images.Media.INTERNAL_CONTENT_URI,null, null,
null, orderBy);
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
this.count = imagecursor.getCount();
this.thumbnails = new Bitmap[this.count];
for (int i = 0; i < this.count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
getApplicationContext().getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, null);
adapter.mThumbIds[i]= thumbnails[i];
}
}
imageCursor.getCount()
上返回的计数返回0.
有人可以指导我,因为我无法使用此代码获取任何媒体吗?
注意:我正在测试Galaxy Note-2
答案 0 :(得分:14)
首先不要忘记
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
此方法将返回图库中的照片列表
public static ArrayList<String> getImagesPath(Activity activity) {
Uri uri;
ArrayList<String> listOfAllImages = new ArrayList<String>();
Cursor cursor;
int column_index_data, column_index_folder_name;
String PathOfImage = null;
uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaColumns.DATA,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME };
cursor = activity.getContentResolver().query(uri, projection, null,
null, null);
column_index_data = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
column_index_folder_name = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
while (cursor.moveToNext()) {
PathOfImage = cursor.getString(column_index_data);
listOfAllImages.add(PathOfImage);
}
return listOfAllImages;
}
答案 1 :(得分:2)
即使您的设备只有内部存储空间而没有外部存储空间,也可以尝试一下
尝试添加权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
并使用
阅读android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;