IllegalArgumentException while reading a video file from External Stroage

浪子不回头ぞ 提交于 2019-12-12 02:17:44

问题


I have a problem that I am getting IllegalArgumentException while reading a video file from sdcard. I don't know why? Please suggest me the right solution for the same.

ErrorStack:

11-03 18:56:18.733: ERROR/AndroidRuntime(24192): FATAL EXCEPTION: main
11-03 18:56:18.733: ERROR/AndroidRuntime(24192): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.TestCryptoActivity}: java.lang.IllegalArgumentException: File /mnt/sdcard/E0022505.mp4 contains a path separator
11-03 18:56:18.733: ERROR/AndroidRuntime(24192):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192):     at android.os.Looper.loop(Looper.java:123)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192):     at java.lang.reflect.Method.invokeNative(Native Method)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192):     at java.lang.reflect.Method.invoke(Method.java:521)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192):     at dalvik.system.NativeStart.main(Native Method)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192): Caused by: java.lang.IllegalArgumentException: File /mnt/sdcard/E0022505.mp4 contains a path separator
11-03 18:56:18.733: ERROR/AndroidRuntime(24192):     at android.app.ContextImpl.makeFilename(ContextImpl.java:1602)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192):     at android.app.ContextImpl.openFileInput(ContextImpl.java:399)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192):     at android.content.ContextWrapper.openFileInput(ContextWrapper.java:152)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192):     at com.example.TestCryptoActivity.onCreate(TestCryptoActivity.java:29)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

Code:

    try {
        is = this.openFileInput(Environment.getExternalStorageDirectory()+"/E0022505.mp4");
    } catch (FileNotFoundException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[2097152];
try {
        while ((bytesRead  = is.read(b)) != -1) {
           bos.write(b, 0, bytesRead);
        }
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
byte[] bytes = bos.toByteArray();
try {
        String byteString = new String(bytes,"UTF-8");
        System.out.println("the bytes array of video:"+byteString);
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

回答1:


Context.openFileInput will not allow path separators because it is only to be used for private files in the application's context.

http://developer.android.com/reference/android/content/Context.html#openFileInput(java.lang.String)

Open a private file associated with this Context's application package for reading.

You could open that file another way (File, FileInputStream, etc), since it is on the sdcard, but you can't use openFileInput for that purpose.




回答2:


I think, this method opens a file in the private data area of application. You cannot open any files in subdirectories in this area . So use a FileInputStream or such.

Hope it helps.



来源:https://stackoverflow.com/questions/7995757/illegalargumentexception-while-reading-a-video-file-from-external-stroage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!