5 回答

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超11個(gè)贊
我能夠?qū)?MediaStore.MediaColumns.Data 替換為它自己的文件 ID(令人難以置信的是,文件具有 ID)并正確構(gòu)建其 URI,如下所示:
fun getAllShownImagesPath(activity: Activity): MutableList<Uri> {
val uriExternal: Uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
val cursor: Cursor?
val columnIndexID: Int
val listOfAllImages: MutableList<Uri> = mutableListOf()
val projection = arrayOf(MediaStore.Images.Media._ID)
var imageId: Long
cursor = activity.contentResolver.query(uriExternal, projection, null, null, null)
if (cursor != null) {
columnIndexID = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
while (cursor.moveToNext()) {
imageId = cursor.getLong(columnIndexID)
val uriImage = Uri.withAppendedPath(uriExternal, "" + imageId)
listOfAllImages.add(uriImage)
}
cursor.close()
}
return listOfAllImages
}
然后使用 Uri 在您的視圖中構(gòu)建它!

TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊
仍然無法使用獲得的 Uri 加載圖像。?文檔建議使用 openFileDescriptor() ,我這樣做了,然后從中解碼圖像的位圖:
override fun loadImagesFromStorage(): List<AdapterImage> {
? ? val uri: Uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
? ? val cursor: Cursor?
? ? val columnIndexId: Int
? ? val listOfAllImages = mutableListOf<AdapterImage>()
? ? val projection = arrayOf(MediaStore.Images.Media._ID)
? ? cursor = context.contentResolver
? ? ? ? .query( uri, projection, null, null, null)
? ? if ( cursor != null ){
? ? ? ? columnIndexId = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
? ? ? ? while (cursor.moveToNext()){
? ? ? ? ? ? val contentUri = ContentUris.withAppendedId(uri, cursor.getLong(columnIndexId))
? ? ? ? ? ? //here I open FileDescriptor and then decode it into Bitmap
? ? ? ? ? ? var image: Bitmap
? ? ? ? ? ? context.contentResolver.openFileDescriptor(contentUri, "r").use { pfd ->
? ? ? ? ? ? ? ? if( pfd != null ){
? ? ? ? ? ? ? ? ? ? image = BitmapFactory.decodeFileDescriptor(pfd.fileDescriptor)
? ? ? ? ? ? ? ? ? ? listOfAllImages.add(AdapterImage(image))
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? cursor.close()
? ? }
? ? return listOfAllImages
}
PS 我的方法將返回我稍后在應(yīng)用程序中使用的 AdapterImage 對象列表,但此時(shí)您可以將任何需要的東西放在那里

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超9個(gè)贊
我終于通過創(chuàng)建這個(gè)類解決了這個(gè)問題
class FileHelper {
val mediaType = "multipart/form-data".toMediaTypeOrNull()
fun getPartBodyFromUri(context: Context, uri: Uri): MultipartBody.Part {
val realPath = getPathFromURI(context, uri)
val fileImage = createFile(realPath)
val requestBody = createRequestBody(fileImage)
return createPart(fileImage, requestBody)
}
private fun createFile(realPath: String): File {
return File(realPath)
}
private fun createRequestBody(file: File): RequestBody {
return file.asRequestBody(mediaType)
}
private fun createPart(file: File, requestBody: RequestBody): MultipartBody.Part {
return MultipartBody.Part.createFormData("imageFile", file.name, requestBody)
}
private fun getPathFromURI(context: Context, uri: Uri): String {
var realPath = String()
uri.path?.let { path ->
val databaseUri: Uri
val selection: String?
val selectionArgs: Array<String>?
if (path.contains("/document/image:")) { // files selected from "Documents"
databaseUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
selection = "_id=?"
selectionArgs = arrayOf(DocumentsContract.getDocumentId(uri).split(":")[1])
} else { // files selected from all other sources, especially on Samsung devices
databaseUri = uri
selection = null
selectionArgs = null
}
try {
val column = "_data"
val projection = arrayOf(column)
val cursor = context.contentResolver.query(
databaseUri,
projection,
selection,
selectionArgs,
null
)
cursor?.let {
if (it.moveToFirst()) {
val columnIndex = cursor.getColumnIndexOrThrow(column)
realPath = cursor.getString(columnIndex)
}
cursor.close()
}
} catch (e: Exception) {
println(e)
}
}
return realPath
}
}
Media.DATA 已棄用,使用“MediaStore.Images.Media._ID”來獲取正確的列,但無法正常工作,因此我創(chuàng)建了我需要的列
val column = "_data"
val projection = arrayOf(column)
然后我使用 getColumnIndexOrThrow() 方法來獲取正確的索引
val columnIndex = cursor.getColumnIndexOrThrow(column)
realPath = cursor.getString(columnIndex)

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超3個(gè)贊
在爪哇
FileInputStream input = null;
FileOutputStream output = null;
try {
String filePath = new File(getCacheDir(), "tmp").getAbsolutePath();
android.os.ParcelFileDescriptor pfd = getContentResolver ().openFileDescriptor(
sharedFileUri, "r");
if (pfd != null) {
FileDescriptor fd = pfd . getFileDescriptor ();
input = new FileInputStream (fd);
output = new FileOutputStream (filePath);
int read;
byte[] bytes = new byte[4096];
while ((read = input.read(bytes)) != -1) {
output.write(bytes, 0, read);
}
File sharedFile = new File(filePath);
String finalPath = sharedFile.getPath();
}
}catch(Exception ex) {
} finally {
try {
input.close();
output.close();
} catch (Exception ignored) {
}
}

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超4個(gè)贊
從內(nèi)部存儲(chǔ)加載圖像。使用 MediaStore 類讀取包含圖像和視頻的所有文件夾。
并將結(jié)果作為 ArrayList 返回。
private fun getAllShownImagesPath(activity: Activity): ArrayList<Albums> {
val uri: Uri
val cursor: Cursor
var cursorBucket: Cursor
val column_index_data: Int
val column_index_folder_name: Int
val listOfAllImages = ArrayList<String>()
var absolutePathOfImage: String? = null
var albumsList = ArrayList<Albums>()
var album: Albums? = null
val BUCKET_GROUP_BY = "1) GROUP BY 1,(2"
val BUCKET_ORDER_BY = "MAX(datetaken) DESC"
uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI
val projection = arrayOf(MediaStore.Images.ImageColumns.BUCKET_ID,
MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
MediaStore.Images.ImageColumns.DATE_TAKEN,
MediaStore.Images.ImageColumns.DATA)
cursor = activity.contentResolver.query(uri, projection, BUCKET_GROUP_BY, null, BUCKET_ORDER_BY)
if (cursor != null) {
column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA)
column_index_folder_name = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME)
while (cursor.moveToNext()) {
absolutePathOfImage = cursor.getString(column_index_data)
Log.d("title_apps", "bucket name:" + cursor.getString(column_index_data))
val selectionArgs = arrayOf("%" + cursor.getString(column_index_folder_name) + "%")
val selection = MediaStore.Images.Media.DATA + " like ? "
val projectionOnlyBucket = arrayOf(MediaStore.MediaColumns.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME)
cursorBucket = activity.contentResolver.query(uri, projectionOnlyBucket, selection, selectionArgs, null)
Log.d("title_apps", "bucket size:" + cursorBucket.count)
if (absolutePathOfImage != "" && absolutePathOfImage != null) {
listOfAllImages.add(absolutePathOfImage)
albumsList.add(Albums(cursor.getString(column_index_folder_name), absolutePathOfImage, cursorBucket.count, false))
}
}
}
return getListOfVideoFolders(albumsList)
}
// 該函數(shù)負(fù)責(zé)讀取所有文件夾中的所有視頻。
private fun getListOfVideoFolders(albumsList: ArrayList<Albums>): ArrayList<Albums> {
var cursor: Cursor
var cursorBucket: Cursor
var uri: Uri
val BUCKET_GROUP_BY = "1) GROUP BY 1,(2"
val BUCKET_ORDER_BY = "MAX(datetaken) DESC"
val column_index_album_name: Int
val column_index_album_video: Int
uri = android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI
val projection1 = arrayOf(MediaStore.Video.VideoColumns.BUCKET_ID,
MediaStore.Video.VideoColumns.BUCKET_DISPLAY_NAME,
MediaStore.Video.VideoColumns.DATE_TAKEN,
MediaStore.Video.VideoColumns.DATA)
cursor = this.contentResolver.query(uri, projection1, BUCKET_GROUP_BY, null, BUCKET_ORDER_BY)
if (cursor != null) {
column_index_album_name = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.BUCKET_DISPLAY_NAME)
column_index_album_video = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA)
while (cursor.moveToNext()) {
Log.d("title_apps", "bucket video:" + cursor.getString(column_index_album_name))
Log.d("title_apps", "bucket video:" + cursor.getString(column_index_album_video))
val selectionArgs = arrayOf("%" + cursor.getString(column_index_album_name) + "%")
val selection = MediaStore.Video.Media.DATA + " like ? "
val projectionOnlyBucket = arrayOf(MediaStore.MediaColumns.DATA, MediaStore.Video.Media.BUCKET_DISPLAY_NAME)
cursorBucket = this.contentResolver.query(uri, projectionOnlyBucket, selection, selectionArgs, null)
Log.d("title_apps", "bucket size:" + cursorBucket.count)
albumsList.add(Albums(cursor.getString(column_index_album_name), cursor.getString(column_index_album_video), cursorBucket.count, true))
}
}
return albumsList
}
添加回答
舉報(bào)