Android 10.媒体商店。获取所有图像
Android 10. MediaStore. Getting all images
我尝试获取所有图像。我使用此代码:
val galleryImageUrls = mutableListOf<String>()
val columns = arrayOf(MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID)
val orderBy = MediaStore.Images.Media.DATE_TAKEN
appContext.contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
null, null, "$orderBy DESC"
)?.use { cursor ->
while (cursor.moveToNext()) {
galleryImageUrls.add(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)))//get Image from column index
}
}
如果我使用 compileSdkVersion 28,此代码有效,但它不适用于 compileSdkVersion 29。
你有什么想法我可以解决它吗?或者我应该使用存储访问框架?
This code works if I use compileSdkVersion 28
不会可靠。无法保证 DATA
将保留您可以使用的值。
Do you have any ideas how I can fix it?
val galleryImageUrls = mutableListOf<Uri>()
val columns = arrayOf(MediaStore.Images.Media._ID)
val orderBy = MediaStore.Images.Media.DATE_TAKEN
appContext.contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
null, null, "$orderBy DESC"
)?.use { cursor ->
val idColumn = cursor.getColumnIndex(MediaStore.Images.Media._ID)
while (cursor.moveToNext()) {
val id = cursor.getLong(idColumn)
galleryImageUrls.add(ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id)
}
}
{
ArrayList<Photo> listPhotoPhone = new ArrayList<>();
Cursor cursor;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
// Codigo version de api 29 en adelante
cursor = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Images.Media._ID},
null,
null,
MediaStore.Images.Media.DATE_TAKEN + " DESC"
);
if (null == cursor) {
return listPhotoPhone;
}
if (cursor.moveToFirst()) {
do {
String photoUrl = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media._ID))).toString();
Photo photo = new Photo(photoUrl);
listPhotoPhone.add(photo);
} while (cursor.moveToNext());
}
} else {
cursor = contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null,
null,
null,
MediaStore.Images.Media.DATA + " DESC"
);
if (null == cursor) {
return listPhotoPhone;
}
if (cursor.moveToFirst()) {
do {
String fullPath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
Photo photo = new Photo(fullPath);
listPhotoPhone.add(photo);
} while (cursor.moveToNext());
}
}
cursor.close();
return listPhotoPhone;
}
感谢 Juan Carlos Suarez Marin,但使用此代码仍然一无所获。
logcat
中没有显示
import android.annotation.SuppressLint
import android.content.ContentUris
import android.content.Context
import android.database.Cursor
import android.os.Build
import android.provider.MediaStore
import android.util.Log
import java.util.*
@SuppressLint("Range")
fun queryImageStorage2(context:Context) {
val listPhotoPhone: ArrayList<String> = ArrayList<String>()
var cursor: Cursor
val contentResolver = context.contentResolver
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
// Codigo version de api 29 en adelante
cursor = contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, arrayOf(MediaStore.Images.Media._ID),
null,
null,
MediaStore.Images.Media.DATE_TAKEN + " DESC"
)!!
if (null == cursor) {
// return listPhotoPhone
Log.d("ooo","photoUrl")
}
if (cursor.moveToFirst()) {
do {
val photoUrl = ContentUris.withAppendedId(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media._ID))
).toString()
// val photo = Photo(photoUrl)
listPhotoPhone.add(photoUrl)
Log.d("ooo",photoUrl)
} while (cursor.moveToNext())
} else {
cursor = context.contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null,
null,
null,
MediaStore.Images.Media.DATA + " DESC"
)!!;
if (null == cursor) {
Log.d("ooo","photoUrl")
// return listPhotoPhone;
}
if (cursor.moveToFirst()) {
do {
var fullPath:String = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
// Photo photo = new Photo(fullPath);
// listPhotoPhone.add(photo);
Log.d("ooo",fullPath)
} while (cursor.moveToNext());
}
}
}
}
我尝试获取所有图像。我使用此代码:
val galleryImageUrls = mutableListOf<String>()
val columns = arrayOf(MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID)
val orderBy = MediaStore.Images.Media.DATE_TAKEN
appContext.contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
null, null, "$orderBy DESC"
)?.use { cursor ->
while (cursor.moveToNext()) {
galleryImageUrls.add(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)))//get Image from column index
}
}
如果我使用 compileSdkVersion 28,此代码有效,但它不适用于 compileSdkVersion 29。 你有什么想法我可以解决它吗?或者我应该使用存储访问框架?
This code works if I use compileSdkVersion 28
不会可靠。无法保证 DATA
将保留您可以使用的值。
Do you have any ideas how I can fix it?
val galleryImageUrls = mutableListOf<Uri>()
val columns = arrayOf(MediaStore.Images.Media._ID)
val orderBy = MediaStore.Images.Media.DATE_TAKEN
appContext.contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
null, null, "$orderBy DESC"
)?.use { cursor ->
val idColumn = cursor.getColumnIndex(MediaStore.Images.Media._ID)
while (cursor.moveToNext()) {
val id = cursor.getLong(idColumn)
galleryImageUrls.add(ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id)
}
}
{
ArrayList<Photo> listPhotoPhone = new ArrayList<>();
Cursor cursor;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
// Codigo version de api 29 en adelante
cursor = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Images.Media._ID},
null,
null,
MediaStore.Images.Media.DATE_TAKEN + " DESC"
);
if (null == cursor) {
return listPhotoPhone;
}
if (cursor.moveToFirst()) {
do {
String photoUrl = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media._ID))).toString();
Photo photo = new Photo(photoUrl);
listPhotoPhone.add(photo);
} while (cursor.moveToNext());
}
} else {
cursor = contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null,
null,
null,
MediaStore.Images.Media.DATA + " DESC"
);
if (null == cursor) {
return listPhotoPhone;
}
if (cursor.moveToFirst()) {
do {
String fullPath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
Photo photo = new Photo(fullPath);
listPhotoPhone.add(photo);
} while (cursor.moveToNext());
}
}
cursor.close();
return listPhotoPhone;
}
感谢 Juan Carlos Suarez Marin,但使用此代码仍然一无所获。 logcat
中没有显示import android.annotation.SuppressLint
import android.content.ContentUris
import android.content.Context
import android.database.Cursor
import android.os.Build
import android.provider.MediaStore
import android.util.Log
import java.util.*
@SuppressLint("Range")
fun queryImageStorage2(context:Context) {
val listPhotoPhone: ArrayList<String> = ArrayList<String>()
var cursor: Cursor
val contentResolver = context.contentResolver
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
// Codigo version de api 29 en adelante
cursor = contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, arrayOf(MediaStore.Images.Media._ID),
null,
null,
MediaStore.Images.Media.DATE_TAKEN + " DESC"
)!!
if (null == cursor) {
// return listPhotoPhone
Log.d("ooo","photoUrl")
}
if (cursor.moveToFirst()) {
do {
val photoUrl = ContentUris.withAppendedId(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media._ID))
).toString()
// val photo = Photo(photoUrl)
listPhotoPhone.add(photoUrl)
Log.d("ooo",photoUrl)
} while (cursor.moveToNext())
} else {
cursor = context.contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null,
null,
null,
MediaStore.Images.Media.DATA + " DESC"
)!!;
if (null == cursor) {
Log.d("ooo","photoUrl")
// return listPhotoPhone;
}
if (cursor.moveToFirst()) {
do {
var fullPath:String = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
// Photo photo = new Photo(fullPath);
// listPhotoPhone.add(photo);
Log.d("ooo",fullPath)
} while (cursor.moveToNext());
}
}
}
}