我需要将一个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" />
Intent.ACTION_GET_DOCUMENT
?为了确保,请全局搜索一下。 - Akin Okegbile