在MediaMetadataRetriever中将Uri发送给setDataSource()时,出现IllegalArgumentException

问题描述 投票:0回答:1

我遇到了类似的问题:-

MediaMetadataRetriever setdatasource IllegalArgumentException

IllegalArgumentException in setDataSource for MediaPlayer

MediaMetadataRetriever setDataSource throws IllegalArgumentException

Log

所有提出的建议我都添加外部存储权限,或者该路径可能错误或无效。setDataSource()方法可以同时使用ContextUri。当我发送它们时,会收到IllegalArgumentException。

这是我发送给Uri的方法:-

static List<String> getMusicNames(Context context,List<Uri> Uris){//get the music names


List<String> musicNames=new ArrayList<String>();


for(Uri uri:Uris){
    MediaMetadataRetriever mData = new MediaMetadataRetriever();
    mData.setDataSource(context, uri);//<<<<<at this line the error
    musicNames.add(mData.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE));


}//returning the music names
return musicNames;

}

我这样发送Uri:-

Arrays.asList(songUri)

可以是多个或一个Uri。

当我调试此程序时,进入方法的Uri:-content://com.android.externalstorage.documents/document/primary%3ADownload%2F01-1085088-Full%20Song-Track%201%20_%20Sakkarathil%20amma.mp3

确定音频文件在那里,并且我在另一个应用程序中将其打开。

Uri value

我从这样的sharedpreferences中获取Uri:-

Uri.parse(pref.getString("gotsong",""))//get the song Uri

我在类似的共享首选项中设置了Uri:-

editor.putString("gotsong", uri.toString());// save the song uri

实际上,如果我没有从共享首选项中检索并直接从文件中获得,也没有例外。

根据oracle IllegalArgumentException

公共类IllegalArgumentException扩展RuntimeException抛出该错误以指示方法已传递了非法或不适当的参数。

这是原始的setDataSource()方法。我已经指出了引发异常的位置:-

public void setDataSource(Context context, Uri uri)
    throws IllegalArgumentException, SecurityException {
    if (uri == null) {
        throw new IllegalArgumentException()//<<<<<<<<<<<<<<<<<<<<<<<<<<<HERE
    }

    String scheme = uri.getScheme();
    if(scheme == null || scheme.equals("file")) {
        setDataSource(uri.getPath());
        return;
    }

    AssetFileDescriptor fd = null;
    try {
        ContentResolver resolver = context.getContentResolver();
        try {
            fd = resolver.openAssetFileDescriptor(uri, "r");
        } catch(FileNotFoundException e) {
            throw new IllegalArgumentException();//<<<<<<<<<<<<<<<<<<<<<<<<<<<HERE
        }
        if (fd == null) {
            throw new IllegalArgumentException();//<<<<<<<<<<<<<<<<<<<<<<<<<<<HERE
        }
        FileDescriptor descriptor = fd.getFileDescriptor();
        if (!descriptor.valid()) {
            throw new IllegalArgumentException();//<<<<<<<<<<<<<<<<<<<<<HERE
        }
        // Note: using getDeclaredLength so that our behavior is the same
        // as previous versions when the content provider is returning
        // a full file.
        if (fd.getDeclaredLength() < 0) {
            setDataSource(descriptor);
        } else {
            setDataSource(descriptor, fd.getStartOffset(), fd.getDeclaredLength());
        }
        return;
    } catch (SecurityException ex) {
    } finally {
        try {
            if (fd != null) {
                fd.close();
            }
        } catch(IOException ioEx) {
        }
    }
    setDataSource(uri.toString());
}

显然,Uri不是null,所以这是其他3个原因之一,其中一个是FileNotFoundException,另一个是我不理解的2个。

我的问题实际上类似于这个问题:-Save uri to sharedPreferences and play with mediaplayer

java android uri media
1个回答
0
投票

我发现问题是,如果Uri被保存在Sharedpreferences中而没有读写Uri的权限,特别是针对音频文件之类的文件的Uri Iam目标,则会出现IllegalArgumentException。因此,该方法适用于例如如果我直接发送文件Uri,如果我将具有文件的文件夹路径保存在共享首选项中,则解压缩Uris并将其发送到方法中;或者,如果我具有在共享首选项中保存file

的权限,然后检索并发送至该方法。
© www.soinside.com 2019 - 2024. All rights reserved.