请给我一个解决方案,加载Android手机内部和外部存储的所有图像? 我的方法下面只返回壁纸和锁屏图像。为了测试,我使用了mi redmi note 3和android v6.0 os并且没有插入外部存储器。 请给我最新的解决方案。我是android的初学者。
public ArrayList<String> getImagePaths() {
ArrayList<String> resultIAV = new ArrayList<String>();
URI u = MediaStore.Images.Media.INTERNAL_CONTENT_URI;
Cursor c = getContentResolver().query(u, projection, null,
null, null);
column_index_data = c.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
while (c.moveToNext()) {
absolutePathOfImage = c.getString(column_index_data);
resultIAV.add(absolutePathOfImage);
}
return resultIAV;
}
答案 0 :(得分:1)
使用此功能从内部和外部存储中获取所有图像,并且不要忘记添加
您的清单文件中的<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
权限。
int dataColumnIndex;
int bucketColumnIndex;
int displayNameColumnIndex;
String GalleryThumbnail_Path;
String bucket;
String displayName;
int imageCounter = 0;
Thread getImages = new Thread(new Runnable() {
@Override
public void run() {
String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_TAKEN,
MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.BUCKET_DISPLAY_NAME
};
String orderBy = MediaStore.Images.Media.DATE_TAKEN;
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
try {
Cursor cursor = context.getContentResolver().query(uri, columns, null, null, orderBy);
assert cursor != null;
int mCount = cursor.getCount();
for (int i = mCount - 1; i >= 0; i--) {
if (imageCounter <= 370) {
cursor.moveToPosition(i);
dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
bucketColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
displayNameColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME);
GalleryThumbnail_Path = cursor.getString(dataColumnIndex);
bucket = cursor.getString(bucketColumnIndex);
displayName = cursor.getString(displayNameColumnIndex);
myimagesPath.add(GalleryThumbnail_Path);
myStorage.add(bucket + "-->" + displayName);
imageCounter++;
Log.e("Recents " + i + "-->>", "Added to bucket");
} else {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
这也适用于Android 7.1.2(My OnePlus One - 我用于开发)