由于开发了一个音频播放器的APP,需要本地文件打开的时候,选择其他方式打开出现自己的APP 的图标,(目前设置的是音频文件)
1.设置AndroidManifest.xml
<activity .......>
//在你需要启动的activity 下边加入
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="audio/*" />
<data android:scheme="file" />
<data android:scheme="content" />
</intent-filter>
</activity>
2.APP 设置接收数据
val action = intent.action
if (action == Intent.ACTION_VIEW) {
val uri = intent.data
//需要通过uri 获取真实路径
val path = getRealFilePath(this, uri!!)
val index = path.indexOfLast { it == '/' }
//截取的app 名字
val name = path.substring(index + 1, path.length)
//自己逻辑省略....
......
}
/**
* 通过Uri 获取真实路径
*/
private fun getRealFilePath(context: Context, uri: Uri): String {
var path: String = ""
val cursor: Cursor? = context.getContentResolver()
.query(uri, arrayOf<String>(MediaStore.Video.VideoColumns.DATA), null, null, null)
if (null != cursor) {
if (cursor.moveToFirst()) {
val index: Int = cursor.getColumnIndex(MediaStore.Video.VideoColumns.DATA)
if (index > -1) {
path = cursor.getString(index)
}
}
cursor.close()
}
return path
}
版权声明:本文为u012402524原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。