博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android 媒体的uri,安卓 - 从相对路径+显示名称中获取媒体的URI或ID
阅读量:7035 次
发布时间:2019-06-28

本文共 3906 字,大约阅读时间需要 13 分钟。

我尝试添加音频文件,val values = ContentValues().apply {

put(MediaStore.Audio.Media.RELATIVE_PATH, libraryPart.rootFolderRelativePath) // JDrop/1/1

put(MediaStore.Audio.Media.DISPLAY_NAME, remoteLibraryEntry.getFilename()) //12.mp3

put(MediaStore.Audio.Media.IS_PENDING, 1)

if(mimeType != null)

put(MediaStore.Audio.Media.MIME_TYPE, mimeType) // audio/mpeg3

}

val collection = MediaStore.Audio.Media

.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)

var uri = ctx.contentResolver.insert(collection, values) // returns null for around 300/2000 files consistently

尝试插入新文件时Logcat输出如下。2020-01-24 22:27:33.724 4015-7707/? E/SQLiteDatabase: Error inserting title_key= bucket_display_name=1 owner_package_name=shio.at.jdrop parent=79657 volume_name=external_primary title_resource_uri=null _display_name=12.mp3 mime_type=audio/mpeg3 _data=/storage/emulated/0/Music/JDrop/1/1/12.mp3 title= group_id=1569 artist_id=322 is_pending=1 date_added=1579901253 album_id=2958 primary_directory=Music secondary_directory=JDrop bucket_id=687581593 media_type=2 relative_path=Music/JDrop/1/1/ from {P:30220;U:10165}

android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: files._data (code 2067 SQLITE_CONSTRAINT_UNIQUE)

at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)

at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:879)

at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:790)

at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:88)

at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1639)

at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1494)

at com.android.providers.media.MediaProvider.insertFile(MediaProvider.java:3050)

at com.android.providers.media.MediaProvider.insertInternal(MediaProvider.java:3452)

at com.android.providers.media.MediaProvider.insert(MediaProvider.java:3240)

at android.content.ContentProvider$Transport.insert(ContentProvider.java:325)

at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:164)

at android.os.Binder.execTransactInternal(Binder.java:1032)

at android.os.Binder.execTransact(Binder.java:1005)

files._data表示文件已经存在于MediaStore。JDrop/1/1/12.mp3上没有文件,它只是在MediaStore中,需要删除它,或者为现有OutputStream获取一个MediaStore,

我尝试使用下面的代码在ID中查询MediaStore,但是没有成功,if(uri == null) {

val id: Long = ctx.contentResolver.query(

collection,

arrayOf(BaseColumns._ID),

"${MediaStore.Audio.Media.RELATIVE_PATH}=? AND ${MediaStore.Audio.Media.DISPLAY_NAME}=?",

arrayOf(libraryPart.rootFolderRelativePath, remoteLibraryEntry.getFilename()),

null)?.use {

if (it.moveToNext())

it.getLong(it.getColumnIndex(BaseColumns._ID))

else null

} ?: return false

uri = Uri.withAppendedPath(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id!!.toString())

}

我现在还尝试用硬编码的路径对_data进行查询,没有成功。val id: Long = ctx.contentResolver.query(

collection,

arrayOf(BaseColumns._ID),

"${MediaStore.Audio.Media.DATA}=?",

arrayOf("/storage/emulated/0/Music/JDrop/1/1/12.mp3"),

null)?.use {

if (it.moveToNext())

it.getLong(it.getColumnIndex(BaseColumns._ID))

else null

} ?: return false

获取null并返回false。(查询所有内容并查看返回的内容)

我试图对entier集合执行一个小的测试查询,class TestQueryObject(val id: Long, val relativePath: String, val displayName: String)

val results = mutableListOf()

ctx.contentResolver.query(

collection,

arrayOf(MediaStore.Audio.Media._ID, MediaStore.Audio.Media.RELATIVE_PATH, MediaStore.Audio.Media.DISPLAY_NAME),

null,

null,

null)?.use {

while (it.moveToNext()) {

results.add(TestQueryObject(

id = it.getLong(it.getColumnIndex(MediaStore.Audio.Media._ID)),

relativePath = it.getString(it.getColumnIndex(MediaStore.Audio.Media.RELATIVE_PATH)),

displayName = it.getString(it.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME))

))

}

}

var find12 = results.find { it.displayName =="12.mp3" }

它返回2557个条目的列表,但是find12仍然是空的。

转载地址:http://ltjal.baihongyu.com/

你可能感兴趣的文章
Passcode
查看>>
iTellAFriend
查看>>
BlockAlertsAnd-ActionSheets
查看>>
MKMovingBlockAnimation
查看>>
TapKu Graph
查看>>
XCode's one very useful cmd:po
查看>>
面试需要的基础知识-合并排序数组
查看>>
关于Unity 2018的实体组件系统(ECS)一
查看>>
安卓系统下安装完apk程序后,具体的文件夹位置在哪里呢
查看>>
Echarts---添加渐变功能
查看>>
常用的工具
查看>>
linux 下解压命令大全
查看>>
深入了解 Linux下安装DNS+Sendmail服务
查看>>
安装Redis完整过程
查看>>
python在类中实现swith case功能
查看>>
SpringCloud学习系列之一 ----- 搭建一个高可用的注册中心(Eureka)
查看>>
leetcode Sort List
查看>>
开源分布式存储SeaweedFS
查看>>
Servlet容器原型(二)——一个简单的连接器
查看>>
Quartz和UIKit坐标系
查看>>