【发布时间】:2020-11-05 02:35:50
【问题描述】:
我必须将 Uri 转换为字符串,以便我可以序列化该 Uri。
Intent openFileIntent = new Intent(Intent.ACTION_GET_CONTENT);
openFileIntent.addCategory(Intent.CATEGORY_OPENABLE);
openFileIntent.setType("audio/mpeg");
startActivityForResult(openFileIntent, PICK_MP3_FILE);
...然后是活动结果...
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_MP3_FILE)
{
if (resultCode == RESULT_OK)
{
try
{
if (data != null)
{
Uri mp3AudioFile;
if ((mp3AudioFile = data.getData()) != null)
{
myObject.setMp3Path(mp3AudioFile.getPath());
myObject.Save();
}
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
}
我关闭了应用程序并再次打开。当我尝试使用以下命令打开该 Uri 时:
Uri uri = Uri.parse(myObject.getMp3Path();
我收到一个错误:
java.lang.SecurityException: Permission Denial: reading com.android.providers.downloads.DownloadStorageProvider uri content://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Faudio.mp3 from pid=601, uid=10107 requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs
我的清单具有以下权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.STORAGE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
【问题讨论】:
标签: java android android-permissions android-file android-fileprovider