MediaMetadataRetriever提供RunTimeException

时间:2019-02-26 15:52:46

标签: android runtimeexception mediametadataretriever

在我的public void onActivityResult(int requestCode, int resultCode, Intent data) {中,有一部分像这样:

if (requestCode == 1) {
                    // Make sure the request was successful

                        final Uri videoUri = data.getData();

                    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
//use one of overloaded setDataSource() functions to set your data source
                    retriever.setDataSource(getActivity(), videoUri);
                    String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
                    timeInMillisec = Long.parseLong(time );

                    retriever.release();

在这里,我尝试使用MediaMetadataRetriever来获取视频时长,并将其存储在timeInMillisec中,接下来我要检查timeInMillisec的值是否存在错误:

Toast.makeText(getActivity(), (int) timeInMillisec, Toast.LENGTH_SHORT).show();

问题是,这里有一个例外:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.zub.videoplay, PID: 9399
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { dat=content://com.miui.gallery.open/raw//storage/emulated/0/Download/Air Supply - All Out Of Love.mp4 typ=video/mp4 flg=0x1 }} to activity {com.example.zub.videoplay/com.example.zub.videoplay.HomeActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x37c3f
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4179)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4222)
        at android.app.ActivityThread.-wrap20(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1581)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:163)
        at android.app.ActivityThread.main(ActivityThread.java:6238)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
     Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x37c3f
        at android.content.res.Resources.getText(Resources.java:351)
        at android.content.res.MiuiResources.getText(MiuiResources.java:97)
        at android.widget.Toast.makeText(Toast.java:285)
        at com.example.zub.videoplay.onActivityResult(PostFragment.java:293)
        at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:151)
        at com.example.zub.videoplay.HomeActivity.onActivityResult(HomeActivity.java:586)
        at android.app.Activity.dispatchActivityResult(Activity.java:7132)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4175)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4222) 
        at android.app.ActivityThread.-wrap20(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1581) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:163) 
        at android.app.ActivityThread.main(ActivityThread.java:6238) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 
I/Process: Sending signal. PID: 9399 SIG: 9

我不知道这里出了什么问题。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

这是堆栈跟踪的重要部分:

Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x37c3f
    at android.content.res.Resources.getText(Resources.java:351)
    at android.content.res.MiuiResources.getText(MiuiResources.java:97)
    at android.widget.Toast.makeText(Toast.java:285)
    at com.example.zub.videoplay.onActivityResult(PostFragment.java:293)
    at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:151)
    at com.example.zub.videoplay.HomeActivity.onActivityResult(HomeActivity.java:586)

HomeActivity.onActivityResult中,您似乎试图通过Toast显示不存在的字符串(ID#0x37c3f)。

答案 1 :(得分:0)

问题是这条线

Toast.makeText(getActivity(), (int) timeInMillisec, Toast.LENGTH_SHORT).show();

它将调用Toast类的makeText方法,因为没有ID为timeInMillisec的资源,因此该方法将抛出Resources.NotFoundException并使您的应用崩溃。

解决方案::将代码更改为

Toast.makeText(getActivity(), timeInMillisec + "", Toast.LENGTH_SHORT).show();

Toast.makeText(getActivity(), String.valueOf(timeInMillisec), Toast.LENGTH_SHORT).show();